//reset current page
function resetPage() {
	 var loc = window.location;
	 var classQ = $.query.get('bw');
	 fadeLoad(loc + '');	
}
//End reset
//
//Append Proper Animation Link
function animText () {
		if ($.cookie("noAnim")==null) {
			$('.footContent .aCenter').append(' - <a href="#" title="This will refresh the current page" onclick="javascript:toggleAnim();">Turn Off Animation</a>');
		}
		else {
			$('.footContent .aCenter').append(' - <a href="#" title="This will refresh the current page" onclick="javascript:toggleAnim();">Turn On Animation</a>');
		}
}
//Toggle Animation
function toggleAnim() {
	if ($.cookie("noAnim")==null){
		$.cookie("noAnim", "off", { expires: 365 });
		//$('.footContent .aCenter').html('&copy; 2011 Betterweather Inc - <a href="#" onclick="javascript:toggleAnim();">Turn On Animation</a>');
		resetPage();
	}
	else {
		$.cookie("noAnim", null);
		//$('.footContent .aCenter').html('&copy; 2011 Betterweather Inc - <a href="#" onclick="javascript:toggleAnim();">Turn Off Animation</a>');
		resetPage();
	}
}
//End Toggle Animation
//Tabular Elements
function loadTabs(queryS) {
		// setup ul.tabs to work as tabs for each div directly under div.panes
		$("ul.tabs").tabs("div.panes > div");
		//navigation
		$("#topNav").tabs("#topNav div.pane", {tabs: 'h2', effect: 'slide', initialIndex: 0});
		//General Tab Affect
		$(".pane").tabs("div.panes > div", {tabs: 'a', effect: 'fade', fadeOutSpeed: 0, fadeInSpeed: 500, initialIndex: queryS});
		$(".aboutNames").tabs("div.panes > div", {tabs: 'h6', effect: 'fade', fadeOutSpeed: 0, fadeInSpeed: 500, initialIndex: queryS});
}
//End Tabular Elements
//
//Handle Content Links to New windows
function fadeLoad(url){
		$('#bodyContent').fadeOut(250,function(){
				window.location.href = url;
		});
}
function openIt(myURL) { 
		(window.open(myURL,'newWin')); 
}
//
//Lightning 
function lightFlash() {
		//Random Lightning
		var numRand1 = Math.floor(Math.random()*1000) + 1000;//between 1 and 2 seconds
		var numRand2 = Math.floor(Math.random()*2500) + 1000;//between 2.5 and 3.5 seconds
		var numRand3 = Math.floor(Math.random()*4250) + 1000;//between 4.25 and 5.25 seconds
		var numRand4 = Math.floor(Math.random()*5750) + 1000;//between 5.75 and 6.75 seconds
		var numRand5 = Math.floor(Math.random()*7000) + 1000;//between 7 and 8 seconds
		var numRandT = Math.floor(Math.random()*5) + 1;      //between 1 and 5 flashes
	    window.setTimeout(function(){
		  $('#rainLightning').css({'visibility':'visible'}).animate({opacity: 0,duration: 250},250, function(){$('#rainLightning').css({'visibility':'hidden'}).animate({opacity: 1});});		  
	    }, numRand1);
	    window.setTimeout(function(){
		  $('#rainLightning').css({'visibility':'visible'}).animate({opacity: 0,duration: 250},250, function(){$('#rainLightning').css({'visibility':'hidden'}).animate({opacity: 1});});  
	    }, numRand2);
	    window.setTimeout(function(){
		  $('#rainLightning').css({'visibility':'visible'}).animate({opacity: 0,duration: 250},250, function(){$('#rainLightning').css({'visibility':'hidden'}).animate({opacity: 1});});
		  
	    }, numRand3);
	    if (numRandT > 3) {
		   window.setTimeout(function(){
			  $('#rainLightning').css({'visibility':'visible'}).animate({opacity: 0,duration: 250},250, function(){$('#rainLightning').css({'visibility':'hidden'}).animate({opacity: 1});});			  
		   }, numRand4);
	    }
	    if (numRandT > 4) {
		   window.setTimeout(function(){
			  $('#rainLightning').css({'visibility':'visible'}).animate({opacity: 0,duration: 250},250, function(){$('#rainLightning').css({'visibility':'hidden'}).animate({opacity: 1});});		  
		   }, numRand5);
	    }
}
//end lightning
//Snowfall
var SineWaveFront = function() {
		this.css = function(p) {
		  var s = Math.sin(p*30)		
		  var x = s * 15 + 0 
		  var y = -205 //- p * 300
		  var o = ((s+2)/4+0.1)
		  return {top: y + "px", left: x + "px", opacity: 1}
		} 
};
var SineWaveBack = function() {
		this.css = function(p) {
		  var s = Math.sin(p*20)		
		  var x = s * 10 + 0 
		  var y = -205 //- p * 300
		  var o = ((s+2)/4+0.1)
		  return {top: y + "px", left: x + "px", opacity: 1}
		} 
};
//End Snowfall
//
//Show Logo
function showLogo() {
		$('#logo').css({'visibility':'visible'}).animate({
			opacity: 1,
			}, 1000
		);
}
//End Show Logo
//
//Remove Environments
//
function removeGrayEnv(loadEnv) {
		$('#foreClouds').animate({
				  opacity: 0,
				  left: '+=150',
				  top: '+=-160',
				}, 750, function() {					
						  switch(loadEnv){
						  case "w": loadWorkEnv("home");
						  break;
						  case "a": loadAboutEnv("home");
						  break;
						  case "g": loadGrayEnv("home");
						  break;
						  case "c": loadContactEnv("home");
						  break;
						  case "d": loadDownloadEnv("home");
						  break;
					}																		
					$('#midClouds').delay(100).animate({
							 opacity: 0,
							 left: '+=-200',
							 top: '+=-130',
						  }, 750, function() {
							  if (loadEnv != "w" && loadEnv != "d" && loadEnv != "c"){
								  $('#backClouds').animate({
								  opacity: 0,
								}, 750);					//load proper env													
							 }
					});
		});									
}
function removeWorkEnv(loadEnv) {
		if (loadEnv != "g" && loadEnv != "d" && loadEnv != "c"){	
			$('#backClouds').animate({opacity: 0},1000);
		}
			$('#workSun').animate({
				top: '+=-160',
			},750,function(){
				$('#workBirds').animate({
					top: '+=-135',
				},500, function(){
					$('#workMidClouds').animate({
						top: '+=-80',
						duration: 1000
					}, 500, function(){
							  //load proper env
							  switch(loadEnv){
							  case "w": loadWorkEnv("work");
							  break;
							  case "a": loadAboutEnv("work");
							  break;
							  case "g": loadGrayEnv("work");
							  break;
							  case "c": loadContactEnv("work");
							  break;
							  case "d": loadDownloadEnv("work");
							  break;
						  }										
					});
				});
			});						
}
function removeAboutEnv(loadEnv) {
		$('#nSmallClouds, #nBackClouds').animate({
			opacity: 0
			}, 750
		);
		$('#moon img').animate({
			height: '0px',
			width: '0px'
		}, 750, function(){				
			$('#nLargeClouds').animate({
				top: '+=-135px'
			}, 500, function(){ 
			//load proper env
				switch(loadEnv){
					case "w": loadWorkEnv("about");
					break;
					case "a": loadAboutEnv("about");
					break;
					case "g": loadGrayEnv("about");
					break;
					case "c": loadContactEnv("about");
					break;
					case "d": loadDownloadEnv("about");
					break;
				}
			
			});
			
		});					
}
function removeContactEnv(loadEnv) {
		if (loadEnv!="g" && loadEnv!="w" && loadEnv!="d"){
			$('#backClouds').animate({opacity: 0},750).css({'visibility':'hidden'});
		}
		$('#snowflakeFront').animate({opacity: 0},1500,function(){
				$('#snowflakeBack').animate({opacity: 0}, 750, function(){
					$('#snowForeClouds').animate({top: '-=135'},750, function(){
						//load proper env
						switch(loadEnv){
							case "w": loadWorkEnv("contact");
							break;
							case "a": loadAboutEnv("contact");
							break;
							case "g": loadGrayEnv("contact");
							break;
							case "c": loadContactEnv("contact");
							break;
							case "d": loadDownloadEnv("contact");
							break;
						}	
					});
				});				
		});	
}
function removeDownloadEnv(loadEnv) {
		if (loadEnv!="g" && loadEnv!="w" && loadEnv!="c"){
			$('#backClouds').animate({opacity: 0},750).css({'visibility':'hidden'});
		}		
		$('#rainDropsFront').delay(500).animate({top: '-=205'},750,
			function(){
				$('#rainDropsBack').animate({top: "-=200"},600,function(){
						$('#rainLeftCloud').animate({left: '+=75'}, 300, function(){});
						$('#rainRightCloud').delay(200).animate({left: '-=75'},300, function(){
							$('#rainLargeCloud').animate({top: '-=130'},400,function(){
								//load proper env
								switch(loadEnv){
									case "w": loadWorkEnv("down");
									break;
									case "a": loadAboutEnv("down");
									break;
									case "g": loadGrayEnv("down");
									break;
									case "c": loadContactEnv("down");
									break;
									case "d": loadDownloadEnv("down");
									break;
								}							
							});
						});
						
				});
				
		});
}
//
//End Remove Environments
//
//Add Environments
//
function loadGrayEnv(fromEnv){
		if (fromEnv=="about"||fromEnv=="home"||fromEnv!="work"||fromEnv!="contact"){
			$('#backClouds').css({'visibility':'visible'}).animate({opacity: 1},1000,function(){
				// perform cloud nimation
				$('#midClouds').animate({
					top: '+=120',
					}, 800, function() {
					  $('#foreClouds').animate({
						top: '+=160',
						}, 800, function() {		  
							showLogo();
					  });
				});	
			});//only animate if not already available
		}
		else {
			// perform cloud nimation
				$('#midClouds').animate({
					top: '+=120',
					}, 800, function() {
					  $('#foreClouds').animate({
						top: '+=160',
						}, 800, function() {		  
							showLogo();
					  });
				});
		}
		$('.mntns').delay(1500).animate({
					opacity: 1,
				  }, 3000, function() {}
		);
	
}
function loadWorkEnv(fromEnv) {
		if (fromEnv!="contact"){
			$('#backClouds').css({'visibility':'visible'}).animate({opacity: 1},3000);
		}
			$('#workMidClouds').animate({
				top: '+=80',
			},1000,function(){
				$('.items, .lGradient, .rGradient').delay(1250).animate({opacity: 1},1000,function(){});//SHOW SCROLLER	
				$('#workSun').animate({
					top: '+=140',
				},2000, function(){
					$('.rPanel').delay(1500).animate({opacity: 1},1500,function(){});
					$('#workBirds').animate(
						{top: '+=135'},
						{easing: 'easeOutBounce', duration: 1000},1000);
					showLogo();
				});
			});
}
function loadAboutEnv(fromEnv) {
		$('#nBackClouds').animate({
				opacity: 1},7000
		);	
		$('#nSmallClouds').animate({
				opacity: 1
				}, 2000, function(){}
		);
		$('#moon img').animate({
			height: '160px',
			width: '160px'
			}, 1250, function(){
				$('#nLargeClouds').animate({
					top: '+=135px'
					}, 750, function(){
							showLogo();
						}
				);
				$('.items, .lGradient, .rGradient').delay(250).animate({opacity: 1},1000);
		});
}
function loadContactEnv(fromEnv) {
		if (fromEnv=="about"){
			$('#backClouds').css({'visibility':'visible'}).animate({opacity: 1}, 1000);
		}
		$('#snowForeClouds').delay(500).animate({top: '+=135'}, 750, function(){
			$('#logo').animate({opacity: 0},1)
			$('#contactUs').delay(900).fadeIn(3000);
			$('#snowflakeBack').animate({path: new SineWaveBack, top: '+=205'}, 7000,function(){showLogo();});
			$('#snowflakeFront').delay(3750).animate({top: '+=205'},{easing: 'easeOutBounce', duration: 1500},1500);
		});		
}
function loadDownloadEnv(fromEnv) {
		if (fromEnv=="about"){
			$('#backClouds').css({'visibility':'visible'}).animate({opacity: 1}, 3000);
		}
		$('#rainLargeCloud').css({'visibility':'visible'});				
		$('#rainLargeCloud').animate({top: '+=130',duration: 500},500,function(){ 
			$('#rainLeftCloud').animate({left: '-=75'}, 750, function(){});
			$('#rainRightCloud').delay(500).animate({left: '+=75'},750, function(){
												lightFlash();
												$('.items, .lGradient, .rGradient, .panes div').delay(500).animate({opacity: 1},1000);//SHOW SCROLLER	
											});
			 window.setTimeout(function(){showLogo();},4500);
		 });
		$('#rainDropsBack').delay(3000).animate({top: '+=200'},{easing: 'easeInSine',duration: 3500}, function(){});
		$('#rainDropsFront').delay(5500).animate({top: '+=205'},{easing: 'easeOutBounce', duration: 1000});
}
//
//End Load Environments
//
//Cookie Function
//
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
//
//End Cookie 
