//Welcome to the Harinder Takhar Javascript File
//This is what I've (Yuriy) learned over the summer.
//With help from the internet and Spencer Finnell.

function centerSlideshow()
{
	var windowWidth = jQuery(window).width();
	var windowHeight= jQuery(window).height();
	
	jQuery( '.slideshow' ).css({
		padding : '0 ' + ( ( windowWidth - 960 ) / 2 ) + 'px' + ' 0 ' + ( ( windowWidth - 960 ) / 2 ) + 'px',
		overflow : 'visible',
		width : 'auto'
	});
	
	jQuery( '.slideshow-navigation' ).css({
		margin : '0 ' + ( ( windowWidth - 960 ) / 2 ) + 'px' + ' 0 ' + ( ( windowWidth - 960 ) / 2 ) + 'px'
		});
		
	jQuery( '#subheader_about_tabs' ).css({
		margin : '304px ' + ( ( windowWidth - 960 ) / 2 ) + 'px' + ' 0 ' + ( ( windowWidth - 960 ) / 2 + 10) + 'px'
	});
		
	jQuery( '.left-shadow, .right-shadow' ).css({
		width : ( ( windowWidth - 960 ) / 2 ) + 'px'
		});
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name) {
			return unescape(y);
		}
	}
}

$(document).ready(function()
{
	//Slideshow
	$(function() {
	    $(".slideshow").jCarouselLite({
	        btnNext: ".next",
	        btnPrev: ".prev",
	        auto: 3500,
	        speed: 2000
	    });
	    
	    centerSlideshow();
	    
	    jQuery(window).resize(function() {
	    	centerSlideshow();
		});		    
	});
	
	//Pop-Ups
	$.fn.popOpen = function()
	{
		popID = $(this).attr('rel'); //Get Popup Name
		popURL = $(this).attr('href'); //Get Popup href to define size
		
		//Pull Query & Variables from href URL
		query= popURL.split('?');
		dim= query[1].split('&');
		popWidth = dim[0].split('=')[1]; //Gets the first query string value
		
		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({});
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
		
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
	};
	
	//Cookies
	//var COOKIE_NAME = 'hstakhar_cookie';
	//var options = { path: '/', expires: 21 };
	//var myNum = $.cookie('hstakhar_cookie'); //Get cookie.
	
	var noemail = getCookie("noemail");
	
	var confirm_id = getUrlVars()["id"]; //Get element from URL.
	
	if(confirm_id)
	{
		$('a.poplight3[href=#?w=705]').popOpen();
		confirmEmail(confirm_id);
	}
	else
	{
		if(noemail!=null && noemail!="")
		{
			//$.cookie(COOKIE_NAME, null, options); //TESTING PURPOSES ONLY//
		}
		else
		{
			$('a.poplight[href=#?w=705]').popOpen();
		}
	}
	
	//Close Popups and Fade Layer
	$('a.close, a.return').click(function()
	{ //When clicking on the close or fade layer...
	  	$('#fade , .popup_block').fadeOut(); //fade them both out
		$('#fade').remove();
		//$.cookie(COOKIE_NAME, 'noemail', options);
		setCookie("noemail",noemail,365);
		return false;
	});
	
	$('#email-submit').click(function()
	{
		$('.popup_block').fadeOut(); //fade them both out
		$('a.poplight2[href=#?w=705]').popOpen();
		return false;
	});
	
	$("a.reload").live("click", function()
	{
		$('.popup_block').fadeOut(); //fade them both out
		$('a.poplight[href=#?w=705]').popOpen();
		return false;
	});
	
	$("a.done").live("click", function()
	{
		$('#fade , .popup_block').fadeOut(); //fade them both out
		$('#fade').remove();
		//$.cookie(COOKIE_NAME, 'noemail', options);
		setCookie("noemail",noemail,365);
		return false;
	});
});

//Send user's email to be added to database.
function addEmail(str)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("email-result").innerHTML=xmlhttp.responseText;
			document.getElementById("email-result").style.padding="10px";
		}
	}
	
	xmlhttp.open("GET","../addemail.php?email="+str,true);
	xmlhttp.send();
}

//Send user's confirmation key to be checked with database and confirmed.
function confirmEmail(str)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("email-confirm").innerHTML=xmlhttp.responseText;
		}
	}
	
	xmlhttp.open("GET","../confirm.php?id="+str,true);
	xmlhttp.send();
}

//Send user's request to livesearch.
function showResult(str)
{
	if (str.length==0)
	{
		document.getElementById("livesearch").innerHTML="";
		document.getElementById("livesearch").style.border="0px";
		return;
	}
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
			document.getElementById("livesearch").style.padding="10px";
		}
	}
	xmlhttp.open("GET","../livesearch.php?q="+str,true);
	xmlhttp.send();
}

//Navigation
$(document).ready(function() { 
	$('.nav').hover(function(){
		var distance = $(this).outerHeight();
		$(this).find('.navimg').animate({ top : distance },{queue:false,duration:500});
	}, function(){
		$(this).find('.navimg').animate({ top : '0px' },{queue:false,duration:500});
	});         
});
