
// iFrame for iPhone
function iScroll(elem)
{    
    if(navigator.userAgent.indexOf("iPhone") < 0)
        return;
    
    var el = document.getElementById(elem);
    	
	var position = 0;
	var maxScroll = 0;
	
	el.style.webkitTransform = 'translate3d(0, ' + position + 'px, 0)';
	
	
	el.style.webkitTransitionDuration = '0';

	if( el.offsetHeight<el.parentNode.clientHeight )
	    maxScroll = 0;
    else		
	    maxScroll = el.parentNode.clientHeight - el.offsetHeight;

	el.style.webkitTransitionTimingFunction = 'cubic-bezier(0, 0, 0.2, 1)';
	this.acceleration = 0.009;
}
		
// Used for stars showing
// id - mouseOver object (1 to 5), id1-id5 - 5 stars objects

var starMouseOver = function (id, id1, id2, id3, id4, id5, ImagePath)
{
	$(id1).attr("src", ImagePath + "starG.gif");
	if ($(id).attr("id") == $(id1).attr("id"))
		return;
	$(id2).attr("src", ImagePath + "starG.gif");
	if ($(id).attr("id") == $(id2).attr("id"))
		return;
	$(id3).attr("src", ImagePath + "starG.gif");
	if ($(id).attr("id") == $(id3).attr("id"))
		return;
	$(id4).attr("src", ImagePath + "starG.gif");
	if ($(id).attr("id") == $(id4).attr("id"))
		return;
	$(id5).attr("src", ImagePath + "starG.gif");
	if ($(id).attr("id") == $(id5).attr("id"))
		return;
}
// id - mouseOut object (1 to 5), id1-id5 - 5 stars objects 
var starMouseOut = function (id, id1, id2, id3, id4, id5, ImagePath)
{
	$(id1).attr("src", ImagePath + "starW.gif");
	$(id2).attr("src", ImagePath + "starW.gif");
	$(id3).attr("src", ImagePath + "starW.gif");
	$(id4).attr("src", ImagePath + "starW.gif");
	$(id5).attr("src", ImagePath + "starW.gif");
}

/*   does not work as object is CLONED rather tahn sent by reference
// id - mouseOut object (1 to 5), id1-id5 - 5 stars objects 
function starMouseOverAndOut (id, id1, id2, id3, id4, id5, ImagePath)
{
	$("#id").mouseover(function() { starMouseOver(id, id1, id2, id3, id4, id5, ImagePath); } )
	$("#id").mouseout(function()  { starMouseOut (id, id1, id2, id3, id4, id5, ImagePath); } )
}
// functions sets mouse On/Off to show selected stars from current to all below 
function allStarsMouseOverAndOut (id1, id2, id3, id4, id5, ImagePath)
{
	starMouseOverAndOut (id1, id1, id2, id3, id4, id5, ImagePath);
	starMouseOverAndOut (id2, id1, id2, id3, id4, id5, ImagePath);
	starMouseOverAndOut (id3, id1, id2, id3, id4, id5, ImagePath);
	starMouseOverAndOut (id4, id1, id2, id3, id4, id5, ImagePath);
	starMouseOverAndOut (id5, id1, id2, id3, id4, id5, ImagePath);
}
*/


// Resultbox. Used in validationResults.ascx
window.onload = function() 
{
    InitResultboxes();    
}

// Used in validationResults.ascx
function InitResultboxes()
{ 
    $("div.alert").each(function()
    {        
        if ($(this).children("span").html() == "")
            $(this).hide();
        else
        {       
            $(this).show();     
            $(this).children("div").css("height",$(this).children("span").height()+10);
        }    
        $(this).children("span").css("width", $(this).width()-195);
    });
}

// Used in validationResults.ascx
function writeAlert(id, message)
{
    writeAlert(id, message, false);
}

// Used in validationResults.ascx
function writeAlert(id, message, result)
{    
    $("#" + id).hide();
    if (message != "" && message != null)
    {
        $("#" + id + " > span").html(message);    
        $("#" + id).show();        
        $("#" + id + " > span").css("width", $("#" + id).width()-195);               
        $("#" + id + " > div").css("height",$("#" + id + " > span").height()+2);
    }
    $("#" + id).removeClass("ok");
        
    if (result)
    {
        $("#" + id + " > div").html("");
        $("#" + id).addClass("ok");
        $("#" + id + " > span").css("width", $("#" + id).width()-195);               
    }
    else
    {
        $("#" + id + " > div").html("Please be careful:");    
    } 
    
    //$("#" + id + " > span").css("height", $("#" + id).height());
}

// for popup window with indicator of new mail received
function loadMailPopup(path) {
    $.get(path + "/Refresh.aspx", 
        { async: true },
        function(text) {
        try {
            eval(text);
        }
        catch (e) { } 
    });
}

// Checking on correct email
String.prototype.isCorrectAsEmail = function() 
{
	var regex = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i
	return regex.test( this);
}

// Checking on correct email
String.prototype.isContainEmail = function() {
    var posCaret = this.indexOf('@');
    if (posCaret < 0)
        posCaret = this.indexOf('at');
    var posDot = this.indexOf('@', posCaret);
    if (posDot < 0)
        posDot = this.indexOf('dot', posCaret);
    if (posCaret >= 0 && posDot >= 0) return true;

    var posUrl = this.indexOf('htt');
    if (posUrl < 0)
        posUrl = this.indexOf('.com');
    if (posUrl < 0)
        posUrl = this.indexOf('.org');
    if (posUrl < 0)
        posUrl = this.indexOf('com ');

    if (posUrl >= 0) return true;
    
    return false;
}
	
function GoToHome() 
{
	//if (window.opener) {
	//	window.opener.location='/Member/Home.aspx';
	//	self.close();
	//	window.opener.focus();
	//} else {
		document.location='/Member/Home.aspx';
	//}
}


// member/profile/uc_adult_update.ascx
//should be used in new look if pos
function OpenHelp(pageNm, section, appPath) {
	var url = appPath+ "/help/member/" + pageNm;
	if (section != "") {		
		url = appPath+ "/help/member/" + pageNm + "#" + section;
	}
	window.open(url, 'help','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=500'); 
	
	return false;
}

// Main.Master, menu.xml
var chatWin;
function OpenChat(url) {	
	if (chatWin && (! chatWin.closed)) {
		chatWin.focus();
    } else {
        try {
            // If global variable member_state is exists and member state is incomplete = redirect to profile
            if (member_state == 0) {
                window.location.href = appPath + "/Member/Profile/ViewProfile.aspx";
                return;
            }
        } catch (e) { }
        // if state is not incomplete or variable not exists - opening normal chat window
		chatWin = window.open(url,'chat'); 	
	}
}

// used by help controls all in /help/
function PopUpEnquiry(msg, localAppPath) {
	if (msg == null)
		msg = "";
	if (localAppPath == null)
		localAppPath = "";
	if (appPath != null)
	    localAppPath = appPath;
	var width = 550;
	var height = 300;
	var url = appPath + "/Application/Support.aspx?msg=" + msg;
	var win = window.open(url,'EnquiryForm');
	win.focus();
}

//uc_galleryexternal
function mediaOpen(appPath, mediaId, passportId, showDeleted) {
	
	if (showDeleted == null)
		showDeleted = 0;
		
	if((!mediaWin) || (mediaWin.closed == true)) {
		var mediaWin=window.open(appPath + "/Member/Media/MediaItemNew.aspx?selectedMediaId=" + mediaId + "&selectedPId=" + passportId + "&showDeleted=" + showDeleted,"media","toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=490");
		mediaWin.focus();
	} else {
		mediaWin.location.href = url;
		mediaWin.focus();
	}
}


var openWinHndl;
function OpenWin(url, name, features) {
	if((!openWinHndl) || (openWinHndl.closed == true)) {
		openWinHndl = window.open(url, name, features);
	} else {	
		openWinHndl.location = url;
		openWinHndl.focus();	
	}
}

var commonFeatures = "toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=680,height=530";
function profileOpen(id,appPath) {
	var url = appPath + "/Member/Profile/ViewProfile.aspx?id=" + id;
	document.location = url;
}

function profileOpenHome(id,appPath) {
	var url = appPath + "/Member/Profile/ViewProfile.aspx?id=" + id + "&homepage=1";
	OpenWin(url, "profile", commonFeatures);
}

// member/profile/uc_adult_update.ascx
var winCCbill;
function OpenCancelCCBill() {
	var url = 'https://support.ccbill.com/?client_accnum=927070&client_subacc=0000';
	OpenWin(url, "Cancel", commonFeatures);
}

//member\webmaster\uc_full_header.ascx
function Logout(appPath) {
	if (top.location != self.location) {
		top.location.href = appPath + '/login.aspx?logout=true';
		//top.location.href = appPath + '/Logout.aspx';
	} else {
		//window.location.href = appPath + '/Logout.aspx';
		window.location.href = appPath + '/login.aspx?logout=true';
	} 
}


//controls\SiteNews.ascx
function siteOpen(appPath, id){
    if ((!OpenWindow) || (OpenWindow.closed == true)) {
        var OpenWindow = window.open(appPath + "/member/SiteNewsItem.aspx?Id=" + id,"SiteNews","toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=630,height=490");
        OpenWindow.focus();
    } else {
        OpenWindow.location.href = url;
        OpenWindow.focus();
    }
}
 
//member\mail\uc_sendmessage.ascx
function insertEmoticon(strEmoticon, clientID){
	var emotTag = "("+strEmoticon+")";
	if(clientID == ''){
		insertAtCaret(document.getElementById('message'), emotTag);
		document.getElementById('message').focus();
		storeCaret(document.getElementById('message'))
	}else{
		insertAtCaret(document.getElementById(clientID+'_message'), emotTag);
		document.getElementById(clientID+'_message').focus();
		storeCaret(document.getElementById(clientID+'_message'))
	}
	
}

function insertTag(strEmoticon, clientID){
	var emotTag = "["+strEmoticon+"]";
	if(clientID == ''){
		insertAtCaret(document.getElementById('message'), emotTag);
		document.getElementById('message').focus();
		storeCaret(document.getElementById('message'))
	}else{
		insertAtCaret(document.getElementById(clientID), emotTag);
		document.getElementById(clientID).focus();
		storeCaret(document.getElementById(clientID))
	}
	
}

//member\mail\uc_sendmessage.ascx
//store cursor position of text box
function storeCaret (textArea){
	if(document.selection){
		if (textArea.createTextRange)
			textArea.caretPos = document.selection.createRange().duplicate();
	}
}

//member\mail\uc_sendmessage.ascx
//insert text in text area at cursor position
function insertAtCaret (textArea, text){
	if (textArea.createTextRange && textArea.caretPos){
		var caretPos = textArea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	} else {
		textArea.value += text;
	}
}    

function clearAllChoice(gridId)
{
	var datagrid = document.getElementById(gridId);
	var checks = datagrid.getElementsByTagName("input");
	for (var i=0;i<checks.length;i++)
	{
		if (checks[i].type == "checkbox" && !checks[i].disabled)
		{
			checks[i].checked = false;
		}
	}
	
}

function setAllChoice(containerId,setValue, idPrefix)
{
	var datagrid = document.getElementById(containerId);
	var checks = datagrid.getElementsByTagName("input");
	for (var i=0;i<checks.length;i++)
	{
		if (checks[i].type == "checkbox" && !checks[i].disabled && checks[i].id.indexOf(idPrefix) > 0)
		{
			checks[i].checked = setValue;
		}
	}
	
}

function selectOne(owner)
{
	if (owner.checked)
	{
		var checks = owner.parentNode.parentNode.getElementsByTagName("input");
		for (var i=0;i<checks.length;i++)
		{
			if (checks[i].type == "checkbox" && checks[i] != owner)
			{
				checks[i].checked = false;
			}
		}
	}
}

// for popup window with indicator of new mail received
function setPaymentAttempt(path, offerId, paymentSystemId, daysCount) 
{
	
	try
	{
		var query = "?offerId=" + offerId + "&paymentSystemId=" + paymentSystemId + "&daysCount=" + daysCount;
	
		var req = window.XMLHttpRequest? 
			new XMLHttpRequest() : 
			new ActiveXObject("Microsoft.XMLHTTP");
		req.onreadystatechange = function() 
		{
			if (req.readyState == 4) 
			{
				try
				{
					eval(req.responseText);
				}
				catch (ex)
				{
				}
			}
		}
		req.open("GET",path + "/Upgrade/PaymentAttempt.aspx" + query , true);
		req.send(null);
  }
  catch(exception) {}
}


// common functions
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function loadLike(appPath, objectId, objectTypeId, personLocale, peopleLocale) 
{
	
	var __id = "_" + objectId + "_" + objectTypeId;
	
	$(".id_like_button"+ __id).css('display', 'none');
	$(".id_like_img"+ __id).css('display', 'none');
	$(".id_like_count"+ __id).css('display', 'none');

	$.ajax({
			url: appPath + "/controls/like/Like.aspx?id=" + objectId + "&type=" + objectTypeId + "&info=likeStats",
			async: true,
			cache: false,
			success: function(data) {
				var likes = data;
				$.ajax({
					url: appPath + "/controls/like/Like.aspx?id=" + objectId + "&type=" + objectTypeId + "&info=isAlreadyLike",
					async: true,
					cache: false,
					success: function(data) {
						var ifLiked = data;
						if (ifLiked == "1")
						{
							$(".id_like_button"+ __id).css('display', 'none');
							$(".id_like_img"+ __id).css('display', 'inline');
						}
						else 
						{
							$(".id_like_button"+ __id).css('display', 'inline');
							$(".id_like_img"+ __id).css('display', 'none');
						}
						
						if (likes == "0")
						{
							$(".id_like_count"+ __id).css('display', 'none');
							$(".id_like_img"+ __id).css('display', 'none');
						}
						else if (likes == "1")
						{
							$(".id_like_count"+ __id).css('display', 'inline');
							$(".id_like_count"+ __id).html(likes + ' ' + personLocale);
						}
						else
						{
							$(".id_like_count"+ __id).css('display', 'inline');
							$(".id_like_count"+ __id).html(likes + ' ' + peopleLocale);
						}
					}
				})

			}
		   });
}

function clickLike(appPath, objectId, objectTypeId, personLocale, peopleLocale, item) {
    
    var __id = "_" + objectId + "_" + objectTypeId;

    $.ajax({
        url: appPath + "/controls/like/Like.aspx?id=" + objectId + "&type=" + objectTypeId + "&info=click",
        async: true,
        cache: false,
        success: function(data) {
			var likes = data;
            if (item != null)
			{
				$("#one_new_point_added").css("top", ($(item).offset().top-2) + "px");
				$("#one_new_point_added").css("left", ($(item).offset().left + 140) + "px");
				$("#one_new_point_added").blink(0);
			}
		    
			loadLike(appPath, objectId, objectTypeId, personLocale, peopleLocale);
		    
			if (item != null)
				window.setTimeout("$('#one_new_point_added').fadeOut(400)", 2000);
        }
    })
}

(function($) {
    $.fn.extend({
        // recursive function makes blink alert windows
        // item - jQuery item that should be blinked
        // blinkCount - blink cycle count
        blink: function(blinkCount) {
            function blink(item, blinkCount) {
                $(item).fadeOut(200,
                    function() {
                        $(item).fadeIn(200,
                            function() {
                                if (blinkCount > 0) {
                                    blinkCount--;
                                    blink(item, blinkCount)
                                }
                            });
                    });
            };
            return this.each(function() {

                var item = $(this);

                blink(item, blinkCount);
            });
        }
    });
})(jQuery);

(function($){
    var hash = window.location.hash;
    var handlers  = [];
    var opt = {};

    $.extend({
    anchorHandler: {
            apply: function() {
                /*$.map(handlers, function(handler){
                    var match = hash.match(handler.r) && hash.match(handler.r)[0] || false;
                    if (match)  { handler.cb.apply($('a[href*='+match+']').get(0), [handler.r, hash || '']); }
                });*/
                return $.anchorHandler;
            },
            add: function(regexp, callback, options) {
                var opt  = $.extend({handleClick: true, preserveHash: true}, options);
                if (opt.handleClick) { 
                    $('a[href*=#]').each(function(i, a){
                        if (a.href.match(regexp)) {
                            $(a).bind('click.anchorHandler', function(){
                                if (opt.preserveHash) { window.location.hash = a.hash; }
                                return callback.apply(this, [regexp, a.href]);
                                });
                        }
                    }); 
                }
        handlers.push({r: regexp, cb: callback});
        $($.anchorHandler.apply);
        return $.anchorHandler;
        }
    }
});
})(jQuery);

