//navigation clicked
var attributes = {};
var flashvars = {};
var params = { allowScriptAccess: "always", wmode: "transparent" };
params.allowfullscreen = "true";

var windowHeight = 0;
var windowWidth = 0;
var setvidHeight = 540;
var setvidWidth = 960;
var overlayTopMargin = 120;
var diffWidth = 0;
var overlayLeftMargin = 0;			
var overlayClosed = true;		
			
$(document).ready(function(){
	$('#navigation ul li').click(function(){
		href = $(this).attr('href');
		window.location = href;
	})
		
	var page = $('#page_name').val();
	
	var paramOverlay = $('#popLoginBox').overlay({ mask: { color: '#313131', loadSpeed: 200, opacity: 0.9 }, close: "#cancelCustStream"	});
	
	var moreInfoOverlay = $('#moreInfo').overlay({ mask: { color: '#313131', loadSpeed: 200, opacity: 0.9 }, close: ".btnClose", top: 'center' });
	
	var bloOverlay = $('.overlay').overlay(
		{ mask: { color: '#000', opacity: 0.85 }, 
		top: 120, 
		closeOnClick: false, 
		closeOnEsc: false
	});
	
	$(window).resize(function() { 
		resizeOverlay( true );
		
		$("#popupVideo").css("top", overlayTopMargin);
		$("#popupVideo").css("left", overlayLeftMargin);
	});
	
	if ( $("#popupVideo").length != 0 ) {
		resizeOverlay( false );
		
		var vidOverlay = $('#popupVideo').overlay({ mask: { color: '#000', opacity: 0.85 }, closeOnClick: false, closeOnEsc: false, close: "#closeVid" });
		
		//swfobject.embedSWF("http://www.youtube.com/v/tSaxLbuHF6E?fs=1&amp;hl=en_US&amp;enablejsapi=1&amp;version=3&rel=0", "xsplitVid", "960", "540", "9.0.0", null, null, params);
	}
	
	$("#popLogin, .pupBoxIn, .arelog").click(function() { 
		
		$('#popLoginBox').overlay().load();
		$('#xsusername').focus();
		
		return false;
	});
	
	$(".btnClose").click(function() { 
		$('#moreInfo').overlay().close();
	});
	
	$("#btnDefLogin").click(function() { 
		return checkEmail( $('#xsusername').val() );
	} );
	
	$("#btnLogin, #btnRelogin").click(function() { 
		var btnType = $(this).attr( "id" );
		if ( btnType == "btnRelogin" )
			var email = $("#reusername").val();
		else
			var email = $("#xsusername").val();
		
		if ( checkEmail( email ) ) { 
			var postid = $("#post_id").val();
			var redirectionPage = $("#redirectionPage").val();
			if ( btnType == "btnRelogin" ) {
				$( "#loginReloader" ).show();
				
				var username = $("#reusername").val();
				var password = $("#repassword").val();
				var xsform = "Relogin";
			}
			else {
				$( "#loginLoader" ).show();
				
				var username = $("#xsusername").val();
				var password = $("#xspassword").val();
				var xsform = "Login";
			}
			
			var query = "username=" + username + "&password=" + password + "&req=" + xsform;
			
			$.ajax({
				type: "POST",
				url: "../includes/ajaxLogin.php",
				data: query,
				success: function( msg ) {
					if ( msg == "Logged" )
					{
						if ( $.trim(postid) == "forum" )
							window.location = '..' + current_url_string;
						else if ( $.trim(redirectionPage) == "home" )
							window.location = '../';
						else if ( $.trim(redirectionPage) == "whatscooking" )
							window.location = '../whats_cooking.php';
						else if ( $.trim(redirectionPage) == "issues" )
							window.location = '../issues.php';
						else if ( $.trim(redirectionPage) == "signup" )
							window.location = '../signup.php';
						else if ( $.trim(redirectionPage) == "shop" )
							window.location = '../shop.php';
						else if ( ($.trim(postid) != "") && ($.trim(postid) > 0) )
							window.location = '../blog.php?post_id=' + $.trim(postid);
						else
							window.location = '../blog.php';
					} else {
						if ( btnType == "btnRelogin" ) {
							if ( msg == "SUCCESS" ) {
								relogCount = 1;
								$("#statusMessage").html( "Re-login successfull." );
							}
							else {
								relogCount = 0;
								$("#statusMessage").html( "Re-login failed." );
							}
						}
						else
							$("#betaCrewLoginMsg").html( msg );
					} 
					$( "#loginLoader" ).hide();
					$( "#loginReloader" ).hide();
				}
			});
		}
		
		return false;
	});
	
	$("#a_frlogout, .pupBoxOut").click(function() { 
		var postid = $("#post_id").val();
		var redirectionPage = $("#redirectionPage").val();
		var query = "p=logout";
		
		$.ajax({
			type: "POST",
			url: "../includes/ajaxLogin.php",
			data: query,
			success: function( msg ) {
				if ( msg == "Out" ) {
					if ( $.trim(postid) == "forum" )
						window.location = '../forum/';
					else if ( $.trim(redirectionPage) == "home" )
						window.location = '../';
					else if ( $.trim(redirectionPage) == "whatscooking" )
						window.location = '../whats_cooking.php';
					else if ( $.trim(redirectionPage) == "issues" )
						window.location = '../issues.php';
					else if ( $.trim(redirectionPage) == "signup" )
						window.location = '../signup.php';
					else if ( $.trim(redirectionPage) == "shop" )
						window.location = '../shop.php';
					else if ( ($.trim(postid) != "") && ($.trim(postid) > 0) )
						window.location = '../blog.php?post_id=' + $.trim(postid);
					else
						window.location = '../blog.php';
				} else {
					//$("#betaCrewLoginMsg").html(msg);
				} 
			}
		});
		
		return false;
	});
	
	$("#a_frlogin").click(function() { 
		
		$("#ol_relog").hide();
		$("#ol_form").show();
		
		$('#popLoginBox').overlay().load();
		$('#xsusername').focus();
		
		return false;
	});
	
	/* Learn More links */
	$(".addtnlinfo").click( function() { 
		$(".message .dpcontent, .message h2").hide();
		if ( $(this).attr("id") == "morePersonal" ) {
			$("#h2personal, #ppersonal").show();
		} else { 
			$("#h2premium, #ppremium").show();
		}
		
		$('#moreInfo').overlay().load();
		
		return false;
	} );
	
	/* License Terms Checkboxes */
	$("#chkpersonal, #chkpremium").change( function() { 
		var submitID = "buyPersonal";
		if ( $(this).attr("id") == "chkpremium" ) { 
			submitID = "buyPremium";
		}
		
		if ( $(this).attr("checked") ) { 
			$( "#" + submitID ).removeClass("disabled");
		} else { 
			$( "#" + submitID ).addClass("disabled");
		}
	} );
	
	/* Paypal Submit Buttons */
	$("#buyPersonal, #buyPremium").click( function() { 
		$(".message .dpcontent, .message h2").hide();
		if ( $(this).hasClass("disabled") ) {
			$("#h2Alert, #pAlert").show();
			
			$('#moreInfo').overlay().load();
			
			return false;
		} else { 
			/* Set appropriate hosted_button_id's */
			if ( $(this).attr("id") == "buyPersonal" ) { 
				$( "#hosted_button_id_personal" ).val( xpersonalID[ $('input[name=radiopersonal]:checked').val() ] );
			} else { 
				$( "#hosted_button_id_premium" ).val( xpremiumID[ $('input[name=radiopremium]:checked').val() ] );
			}
			
			/* Submit the form */
		}
	} );
	$("#buyPersonalXlog, #buyPremiumXlog").click( function() { 
		$('#moreInfo').overlay().load();
		
		return false;
	} );
		/* Check if checkbox is already checked on page load -> enable the buttons if so */
		if ( $( "#chkpersonal" ).attr( "checked" ) ) { 
			$( "#buyPersonal" ).removeClass( "disabled" );
		}
		if ( $( "#chkpremium" ).attr( "checked" ) ) { 
			$( "#buyPremium" ).removeClass( "disabled" );
		}
	
	/* Bulk License Offer popup buttons */
	$( "#popup_bulklicense_offer .yes" ).click(function() { 
		$('#popup_bulklicense_offer .middle .error_message').hide();
		if ( !blprocess ) {
			blprocess = true;
			$('#popup_bulklicense_offer .middle .error_message').html( "Processing request..." );
			$('#popup_bulklicense_offer .middle .error_message').show();
			
			$.ajax({
				type: 'POST',
				url: 'view/bulklicense/assets/ajaxBulkLicense.php',
				data: 'action=acceptOffer&bll=' + bll + '&blu=' + blu + '&blm=' + blm,
				success: function( response ) {
					blprocess = false;
					
					if ( response == 'OK' ) {											
						$('#popup_bulklicense_offer .button_holder .maybe').click();
					} else {
						$('#popup_bulklicense_offer .middle .error_message').html( response );
					}
				}
			});
		}
	});
	$( "#popup_bulklicense_offer .no" ).click(function() { 
		$('#popup_bulklicense_offer .middle .error_message').hide();
		if ( !blprocess ) {
			blprocess = true;
			
			$('#popup_bulklicense_offer .middle .error_message').html( "Processing request..." );
			$('#popup_bulklicense_offer .middle .error_message').show();
			
			$.ajax({
				type: 'POST',
				url: 'view/bulklicense/assets/ajaxBulkLicense.php',
				data: 'action=rejectOffer&bll=' + bll + '&blu=' + blu + '&blm=' + blm,
				success: function( response ) {
					blprocess = false;
					
					if ( response == 'OK' ) {											
						$('#popup_bulklicense_offer .button_holder .maybe').click();
					} else {
						$('#popup_bulklicense_offer .middle .error_message').html( response );
					}
				}
			});
		}
	});
	$( "#popup_bulklicense_offer .maybe" ).click(function() { 
		$('#popup_bulklicense_offer').overlay().close();
	});
	$( "#popup_bulklicense_offer .ok" ).click(function() { 
		$('#popup_bulklicense_offer .middle .error_message').hide();
		if ( !blprocess ) {
			blprocess = true;
			
			$('#popup_bulklicense_offer .middle .error_message').html( "Processing request..." );
			$('#popup_bulklicense_offer .middle .error_message').show();
			
			$.ajax({
				type: 'POST',
				url: 'view/bulklicense/assets/ajaxBulkLicense.php',
				data: 'action=expiredOffer&bll=' + bll + '&blu=' + blu + '&blm=' + blm,
				success: function( response ) {
					blprocess = false;
					
					if ( response == 'OK' ) {											
						$('#popup_bulklicense_offer').overlay().close();
					} else {
						$('#popup_bulklicense_offer .middle .error_message').html( response );
					}
				}
			});
		}
	});
	
	/* Popup login */
	$("#btnLoginTr").click( function() {
		if ( !checkEmail( $('#usernameli').val() ) ) { 
			$("#errorLoginMsg").html("Please make sure to enter a valid email address.");
			return false;
		}
		
		if ( ($.trim( $('#passwordli').val() )).length < 4 ) {
			$("#errorLoginMsg").html("Please enter a password.");
			return false;
		}
	} );
		/* Check if checkbox is already checked on page load -> enable the buttons if so */
		if ( $( "#chkpersonal" ).attr( "checked" ) ) { 
			$( "#buyPersonal" ).removeClass( "disabled" );
		}
		if ( $( "#chkpremium" ).attr( "checked" ) ) { 
			$( "#buyPremium" ).removeClass( "disabled" );
		}
	
	/* License Info radio buttons */
	$( ".drb, .drc" ).click( function() {
		if ( $( this ).hasClass( "drb" ) ) {
			$( ".drb" ).removeClass( "checked" );
			$( this ).children( "input[name=radiopersonal]:radio" ).attr( "checked", true);
		}
		else {
			$( ".drc" ).removeClass( "checked" );
			$( this ).children( "input[name=radiopremium]:radio" ).attr( "checked", true);
		}
		
		$( this ).addClass( "checked" );
	} );
	
	/**/
	/* if ( typeof tryCookie != 'undefined' ) { 
		var logquery = "p=auto";
		
		$.ajax({
			type: "POST",
			url: "../includes/loginCheck.php",
			data: logquery,
			success: function( msg ) {
				if ( msg == "Logged" )
					window.location = '..' + current_url_string;
			}
		});
	} */
	
	/* Google translate button */
	$(".goog-te-ftab-link").addClass("googleTranslateBtn");
	$(".goog-te-ftab-link").hover(function() { 
		alert("here");
	});
	
	/* View flash video in an overlay */
	$("#viewOverview").click(function() { 
		resizeOverlay( false );
		overlayClosed = false;
		
		swfobject.embedSWF("http://www.youtube.com/v/tSaxLbuHF6E?fs=1&amp;hl=en_US&amp;enablejsapi=1&amp;version=3&rel=0", "xsplitVid", setvidWidth, setvidHeight, "9.0.0", null, null, params);
		
		$("#popupVideo").overlay().load();
		
		return false;
	});
	
	$("#closeVid").click(function() { 
		overlayClosed = true;
		
		$("#popupVideo").overlay().close();
		pause();
		
		return false;
	}); 
})

function validateComment()
{
	var comment = document.getElementById('txt_post_comment').value;
	
	comment=comment.replace(/^\s+|\s+$/g, '');
	comment=comment.replace(/\s+/g,' ');
	
	if(comment == '' || comment == '<Add your comment here>')
	{
		alert('Please supply a valid comment.');
		return false;
	}
	return true;
}

function focusCommentBox()
{
	var comment = document.getElementById('txt_post_comment');
	
	if(comment.value == '<Add your comment here>')
		comment.value = ''; //clear the comment box
}

function lostFocusCommentBox()
{
	var comment = document.getElementById('txt_post_comment');
	
	if(comment.value == '')
		comment.value = '<Add your comment here>';
}

function focusSearchBox()
{
	var searchString = document.getElementById('txtSearch');
	if(searchString.value == 'Search issues...')
		searchString.value = '';
}

function lostFocusSearchBox()
{
	var searchString = document.getElementById('txtSearch');
	
	if(searchString.value == '')
		searchString.value = 'Search issues...';
}

function checkEmail( email )
{
	if(validateEmailPattern(email)) {
		$("#betaCrewLoginMsg").html("");
		return true;
	} else {
		$("#betaCrewLoginMsg").html("Please make sure to enter a valid email address.");
		return false;
	}
}

function validateEmailPattern(elementValue)
{
    var emailPattern = /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue);
}

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("xsplitVid");
}

function pause() { 
	ytplayer.pauseVideo();
}

function resizeOverlay( boolResizeOverlay ) { 
	windowHeight = $(window).height();
	windowWidth = $(window).width();
	overlayTopMargin = 120;
	
	if ( !boolResizeOverlay || overlayClosed ) {
		if ( ( windowWidth < 940 ) || ( windowHeight < 600 ) ) { 
			setvidWidth = 640;
			setvidHeight = 480;
		} else { 
			setvidHeight = 540;
			setvidWidth = 960;
		}
		
		$(".popupBox").width( setvidWidth );
	}

	diffHeight = windowHeight - setvidHeight;
	if ( ( diffHeight > 0 ) && ( diffHeight/2 < overlayTopMargin ) ) { 
		overlayTopMargin = diffHeight/2;
	} else if ( diffHeight < 0 ) { 
		overlayTopMargin = 10;
	}
	
	diffWidth = windowWidth - setvidWidth;
	overlayLeftMargin = diffWidth/2;
	if ( overlayLeftMargin <= 0 ) { 
		overlayLeftMargin = 0;
	}
}
