/**
 * Some global settings and functions.
 */
var popClosingDelay = 3000;
var popHistory = new Array();	// Browse history
var popClosing, busy = false;
var galleryIndex;
var closeTimeout;	// window.setTimeout's object
var xmlHttp;	// Ajax object

$(document).ready(function() {
	// Global Ajax settings
	jQuery.ajaxSetup({
		timeout : 90000,
		error : function(xmlHttpRequest, textStatus, errorThrown) {
			xmlHttpRequest.abort();
			alert('Sorry, '+textStatus+'. Please try again later.');
		}
	});
	// Ajax loading icon
	$('#ajaxLoading').ajaxStart(function() {
		$(this).show();
	
		busy = true;
	});
	$('#ajaxLoading').ajaxStop(function() {
		$(this).hide();
		$('#overLay').hide();
		busy = false;
	});
	// Close the pop
	$('a.popCloseBtn').live('click', function() {
		popClose(false);
		return false;
	});
/*
	// Anchors to change the content of fancybox
	$('a.popAjaxLink').live('click', function() {
		if(typeof closeTimeout != 'undefined') {
			window.clearTimeout(closeTimeout);
		}
		if( ! busy) {
			popShowHref($(this).attr('href'));
		}
		return false;
	});
*/
	// Back button
	$('a.popBackLink').live('click', function() {
	history.go(-1);
		return false;
	});
	

	
	// Converts an anchor to submit button
	$('a.submitBtn').live('click', function() {
		
		subjectForm = $(this).parents('form');
		if(subjectForm.length == 1) {
			subjectForm.submit();
		}
		return false;
	});

	// Open a pop-up box?
	if(typeof popUpTo != 'undefined') {
		popShowHref(popUpTo);
	}
});
/*
// Send an Ajax request to refresh user's status section
function refreshUserStat() {
	$.getJSON(baseUrl+'/user/userstat',  
		function(user) {
			if(typeof user.userId != 'undefined') {
				userName = user.userFirstName != '' ? 
						user.userFirstName : user.userEmail;
				userStatHtml = 'Welcome, <a class="popAjaxLink" href="'+baseUrl+'/user/viewprofile">'+userName+'</a>!';
				if(user.isSrilankan !=1 && user.isSrilankan !=2){
					
				//signInStatHtml = '<a href="'+baseUrl+'/user/editprofile" class="WhiteLink popAjaxLink" title="edit profile">EDIT PROFILE</a><img src="'+baseUrl+'/images/loginbar_line.gif" style="margin:-3px 10px 0;" /><a href="'+baseUrl+'/user/leaderboard" class="WhiteLink popAjaxLink" title="login"><img src="'+baseUrl+'/images/leaderboard.gif" /> LEADERBOARD</a><img src="'+baseUrl+'/images/loginbar_line.gif" style="margin:-3px 10px 0;" /><a class="WhiteLink popAjaxLink" title="logout" href="'+baseUrl+'/user/signout">LOGOUT</a>';
				signInStatHtml = '<a href="'+baseUrl+'/user/editprofile" class="WhiteLink popAjaxLink" title="edit profile">EDIT PROFILE</a><img src="'+baseUrl+'/images/loginbar_line.gif" style="margin:-3px 10px 0;" /><a class="WhiteLink popAjaxLink" title="logout" href="'+baseUrl+'/user/signout">LOGOUT</a>';
				$('#champion').hide();
				}else{
				//signInStatHtml = '<a href="'+baseUrl+'/user/leaderboard" class="WhiteLink popAjaxLink" title="login"><img src="'+baseUrl+'/images/leaderboard.gif" /> LEADERBOARD</a><img src="'+baseUrl+'/images/loginbar_line.gif" style="margin:-3px 10px 0;" /><a class="WhiteLink popAjaxLink" title="logout" href="'+baseUrl+'/user/signout">LOGOUT</a>';	
				signInStatHtml = '<a class="WhiteLink popAjaxLink" title="logout" href="'+baseUrl+'/user/signout">LOGOUT</a>';	
				$('#champion').show();
				}
				$('#facebook').hide();
			} else {
				signInStatHtml = '<a class="WhiteLink popAjaxLink" title="login" href="'+baseUrl+'/user/signin">LOGIN</a>';
				//signInStatHtml += '<img style="margin: -1px 10px 0pt;" src="'+baseUrl+'/images/loginbar_line.gif"/>';
				//signInStatHtml += '<a href="'+baseUrl+'/user/leaderboard" class="WhiteLink popAjaxLink" title="login"><img src="'+baseUrl+'/images/leaderboard.gif" /> LEADERBOARD</a>'
				signInStatHtml += '<img style="margin: -1px 10px 0pt;" src="'+baseUrl+'/images/loginbar_line.gif"/>';
				signInStatHtml += '<a class="WhiteLink popAjaxLink" title="REGISTER" href="'+baseUrl+'/user/signup">REGISTER</a>';
				userStatHtml = '';
				//userStatHtml = 'Login with facebook  <span class="black">or</span> ';
                //facebookHtml = ' <fb:login-button onlogin="loginWithFb();" class="FConnect"><img src="http://wiki.developers.facebook.com/images/a/ac/Connect_light_medium_short.gif" alt="facebook connect" title="facebook connect"/></fb:login-button>'
				$('#facebook').show();
				$('#RES_ID_fb_login_image').attr('src',baseUrl+'/images/facebooklogin.gif')
				$('#champion').hide();
				
			}
			$('#signInStat').html(signInStatHtml);
			$('#userStat').html(userStatHtml);
		}
	);
}
*/
// Closes the pop box
function popClose(isDelay) {
	if(xmlHttp) {
		xmlHttp.abort();
	}
	if(isDelay == true) {
		closeTimeout = window.setTimeout(popWindowClose, popClosingDelay);
	} else {
		if(typeof closeTimeout != 'undefined') {
			window.clearTimeout(closeTimeout);
		}
		popWindowClose();
	}
	addMenuActivated(0);
	clearPopHistory();
//	hideOverlay();
	return false;
}

function removeMenuActivated() {
	$('#Menu li').each(function() {
		$(this).find('a').removeClass('Activated');
	});
}
// Hides the pop box
function popWindowClose() {
	$('#popUpBox').hide();
	$('#popUpBox').html('<!-- Pop Up Box -->');
}

function clearPopHistory() {
	popHistory = new Array();
}
var isSameHref = false;
function popShowHref(href) {
	//isSameHref = popHistory[popHistory.length-1] == href;
	//pushPopHistory(href);
	popTo(href);
}

function pushPopHistory(href) {
	if(popHistory[popHistory.length-1] != href) {
		popHistory.push(href);
	}
}

function getPopBackHref() {
	index = popHistory.length-2;
	return (typeof popHistory[index] == 'undefined') ? null : popHistory[index];
}

function popTo(href) {
	//xmlHttp = $.get(href, popSuccess);
	document.location = href;
}

function popSuccess(data, isFadeIn) {
	isFadeIn = typeof isFadeIn != 'undefined' ? isFadeIn : true;
	$('#popUpBox').html(data);
	if(window.screen.height <= 800) {	// When the screen's resolution width is 1024 or less
		$('#popUpBox div.Pop-upBox').removeClass('Pop-upBox').addClass('Pop-upBox_Small');
		if($('#popUpBox div.Pop-upBox_Small').css('background-image').indexOf('bg_pop_up_content.png') > 0) {
			$('#popUpBox div.Pop-upBox_Small').css('background-image', 'url('+baseUrl+'/images/bg_pop_up_content_small.png'+')');
		}
	}
	if( ! isFadeIn) {
		$('#popUpMain').show();
	}
	$('#popUpBox').show();
	showPopMain();
//	showOverlay();
}

function showOverlay() {
	$('div#overLay').show();
}

function hideOverlay() {
	$('div#overLay').hide();
}

function showPopMain() {
	if($('#popUpMain').css('display') == 'none') {
		if( ! isSameHref) {
			$('#popUpMain').queue(function() {
				$(this).fadeIn();
				$(this).dequeue();
			});
		} else {
			$('#popUpMain').show();
		}
	}
}

function addMenuActivated(id) {
	removeMenuActivated();
	$('#Menu li').eq(id).find('a').addClass('Activated');
}


//function getViewportSize() {
//	var myWidth = 0, myHeight = 0;
//	if(typeof(window.innerWidth ) == 'number' ) {//Non-IE
//		myWidth = window.innerWidth;
//		myHeight = window.innerHeight;
//	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { //IE 6
//		myWidth = document.documentElement.clientWidth;
//		myHeight = document.documentElement.clientHeight;
//	}
//	return [myWidth, myHeight];
//}
function joinwin() {
	popShowHref(baseUrl+'index.php/user/joinwin');
}

