function sizePopup(w,h){
	var headerHeight = jQuery('#main-wrap').height();
	var popupHeight  = jQuery('#popup-wrapper').height();
	var freeScreenSpace = h - 46;
	
	if(jQuery('#popup-wrapper').hasClass('micro')){
		if(h < 700 && h > 600){
			var remainder = (freeScreenSpace - popupHeight)/2;
			jQuery('#popup-wrapper').css('margin-top',remainder + 'px');
			jQuery('#popup-wrapper').css('margin-bottom',remainder + 'px');
		}else if(h >= 700){
			var remainder = (h - 600)/2; 
			jQuery('#popup-wrapper').css('margin-top',remainder + 'px');
			jQuery('#popup-wrapper').css('margin-bottom',remainder + 'px');
		
		}else if( h < (headerHeight + popupHeight)){
			jQuery('#popup-wrapper').css('margin-top','0px');
			jQuery('#popup-wrapper').css('margin-bottom','0px');
		}
	}else{
		if(h >= (headerHeight + popupHeight)){
			var remainder = (freeScreenSpace - popupHeight)/2;
			jQuery('#popup-wrapper').css('margin-top',remainder + 'px');
			jQuery('#popup-wrapper').css('margin-bottom',remainder + 'px');
		}else if( h < (headerHeight + popupHeight)){
			jQuery('#popup-wrapper').css('margin-top','0px');
			jQuery('#popup-wrapper').css('margin-bottom','0px');
		}
	}
}
function sizeLayout(){
		var w = jQuery(window).width();
		var h = jQuery(window).height();
		var headerHeight = jQuery('#main-wrap').height();
		var popupHeight  = jQuery('#popup-wrapper').height();
		var searchWidth = jQuery('#top-search').width();
		var navWidth = w - 198 - searchWidth;
		var freeScreenSpace = h - 46;
		jQuery('#nav-top').css({'width': navWidth});
		jQuery('#mini-scroll-container').css({width: jQuery(window).width()},500);
		jQuery('#background').css({
					'height': h 		
		});
		var logoOffset = ((h/2) - (126/2));
		if(logoOffset > 0){ 
		  jQuery("#logo").css({
			 'padding-top': logoOffset + 'px'
		}
		);
		jQuery("#background-load").css({
					'padding-top': logoOffset + 'px'
		});
	}
		jQuery("#bg").css({
						'height': h  + "px",
						'width' : w + "px",
						'position' : 'fixed',
						'z-index'  : -1000
		});
		
		
		sizePopup(w,h);
		jQuery('#scroll-container').animate({
				width: jQuery(window).width()
			},200);
	
}
function deepLink(){
	var e = "";
	jQuery.address.init(function(e) {
			
                // Initializes the plugin
            jQuery('.nav').address();
			//if(jQuery.address.value() == "//i
                
            }).wrap(false).strict(false).crawlable(true).history(true).change(function(event) {
                // Selects the proper navigation link
                jQuery('.nav a').each(function() {
                    
                });
				
                // Handles response
                var handler = function(data) {
                //alert(data.pathNames);
                	if(location.pathname  + event.value == "/fever-1-17"){
						jQuery("#popup-wrapper").removeClass("visible");
					    jQuery("#popup-wrapper").fadeOut();
						jQuery("#background").fadeOut(function(){
							jQuery('#nav-bar').animate({
								'bottom':'50'
								});
						});
					}
                	var json = "";
                	if(data.substr(0,1) != "<"){
                	
                		json = JSON.parse(data);
                	
                	};
					var path = location.pathname  + event.value;
					
					jQuery('.nav').css('color','#fff');
					jQuery('#'+event.value.split("/")[0]+'-link').css('color','#009AD5');
					if(event.value == ""){
						jQuery(document).bgStretcher.play();
					}else{
						jQuery(document).bgStretcher.pause();
					}
					if(path.substring(0,8) == "/contact"){
						jQuery('#nav-bar').animate({
								'bottom':'0'
						});
						$
						jQuery("#popup-wrapper").removeClass("latest in-house client-work team");
						jQuery('#popup-wrapper').html(json['content']);
						jQuery('#popup-wrapper').fadeIn();
						jQuery('#cancel-contact').hover(function(){jQuery(this).attr('src','../images/cancel_on.png')}, function(){jQuery(this).attr('src','../images/cancel.png')})
						jQuery('#send-contact').hover(function(){jQuery(this).attr('src','../images/send_on.png')}, function(){jQuery(this).attr('src','../images/send.png')})
						jQuery('#scroll-container').css({
				 					height: (515 + 15) + "px",
				 					width: jQuery(window).width()
							 },500);
					    var v = jQuery("#contact-form").validate({
							submitHandler: function(form) {
								jQuery(form).ajaxSubmit({
									success:function(responseText, statusText, xhr, $form){
												if(responseText=='GOOD'){
													jQuery('#contact-form').fadeOut(500,function(){
															jQuery(this).remove();
															jQuery('#scroll-container').append('<div id="thanks" ><img id="tu-img" src="../images/thanku.png"/></div><div id="close-btn"><a href="/" ><img src="images/close.png"/></a></div>');
													
													
													});
												
												}
								
										}
									}
								);
							},
							errorPlacement: function(error, element) {
								error.appendTo(element.prev());
							}
						});
							 
						//loadP('contact/fever');
					}
					else if(path.substring(0,7) == "/search"){
						loadP('search&q='+path.substring(8,path.length),data,path);
					}else if(event.value != ""){
						loadP(event.value.split("/")[0],data,path);
					}
					
					if(location.pathname  + event.value == "/fever-1-17/"){
						jQuery("#popup-wrapper").removeClass("visible");
					    jQuery("#popup-wrapper").fadeOut();
						jQuery("#background").fadeOut();
						jQuery('#nav-bar').animate({
								'bottom':'50'
						});

					}
					if(json['section'] == "about"){
						jQuery('#nav-bar').animate({
								'bottom':'0'
					});
						jQuery("#scroll-container").feverSlide('showAbout');
					}else if(json['section'] == "micro"){
						
						if(json['page'] == "outbreaks"){
							jQuery("#scroll-container").feverSlide('showUser', json['profile'], json['content'], 'outbreak', json['mini-slider']);
						
						}else{
							var p = json['pagenation'];
							jQuery("#scroll-container").feverSlide('showUser', json['profile'], json['content'],'nothin', p, json['pages'], json['mini-slider']);
  						}
						jQuery('.page-li.'+json['profile']).click(function(){
							var p = parseInt(jQuery(this).attr('id'));
							whichPage(p,json['profile']);

						});
						
						jQuery('#nav-bar').animate({
								'bottom':'0'
						});
						
					}
					
                   // jQuery('.content').html(jQuery('.content', data).html()).show();
                  //  jQuery.address.title(/>([^<]*)<\/title/.exec(data)[1]);
                };
	
                // Loads the page content and inserts it into the content area
				var path = location.pathname  + event.value;

				loadingScreen(path);
                jQuery.ajax({
                    url: path,
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
						jQuery('#background-load').fadeOut();
                        handler(XMLHttpRequest.responseText);
                    },
                    success: function(data, textStatus, XMLHttpRequest) {
                        handler(data);
                    }
                });
            })
}
function cA(val, item){
	if(! item.hasClass('dragging')){
		jQuery.address.value(val);
	}
}
function search(){
	jQuery.address.value("search/"+jQuery('#search-input:input').val());

}
function setupBG(arr){
	jQuery(document).bgStretcher({images: arr, imageWidth: 1000, imageHeight: 600, nextSlideDelay: 7000});
}
function loadingScreen(path){
	if(path.substring(0,6) == "/fever"){
		checkPath("fever");
	}else if(path.substring(0,8) == "/strains" || path.substring(0,26) == "/test/fever-1-17//strains/"){
		checkPath("strains");
	}else if(path.substring(0,8) == "/methods"){
		checkPath("methods");
	}else if(path.substring(0,10) == "/outbreaks"){
		checkPath('outbreaks');
	}else if(path.substring(0,8) == "/contact"){
		//checkPath('contact');
	}
	else if(path.substring(0,9) == "/symptoms"){
		checkPath("symptoms");
	}
	else{
		jQuery("#background-load").css({'display':'none'});
		
	}
}
function loadSubnav(nav){
	jQuery('.secondary').css('display','none');
	jQuery('#'+nav+'-secondary').fadeIn();
}
function checkPath(path){
	if(!jQuery('#popup-wrapper').hasClass(path)){
	 	jQuery('#background-load').fadeIn(function(){
	 		jQuery(this).css({'background-color': 'transparent'});
	 	});
	 	
	}
}

//sends ajax call to load the slider with approp info
function loadP(v,d,ep){
	
	
	//if the popup contains the class we don't need to make an ajax call
	loadSubnav(v);
    if(!jQuery('#popup-wrapper').hasClass(v)){
    
	 jQuery.ajax({
		   url: 'ajax/slide-data.php?page='+v,
		   success : function(data){
			    if(!jQuery('#popup-wrapper').hasClass('visible')){
					jQuery('#popup-wrapper').fadeIn();
					
				}
				 var slideData = JSON.parse(data);
				 
				 var json = JSON.parse(d);
			     jQuery('#background-load').fadeOut();
		 		 jQuery('#popup-wrapper').html(slideData['slide']); 
				 jQuery('#popup-wrapper').removeClass();
				 jQuery('#popup-wrapper').addClass(v+" visible "+json['profile']);
				 if(slideData['nav']['type'] == "links"){
				 		
				 		jQuery('#top-links').html(slideData['nav']['data']).addClass('link-opt');
				 		
				 }else if(slideData['nav']['type'] == "scroll"){
				 			if(jQuery('#top-links').hasClass('scroll-opt')){
				 					jQuery('.tweet').remove();
				 					jQuery('#top-links').removeClass('scroll-opt');
				 			
				 			}
				 			jQuery('#top-links').html(slideData['nav']['data']).addClass('scroll-opt');
				 		if(jQuery('#topNavControl').hasClass('closed')){
				 			jQuery('#top-links').css({'display': 'none'});
				 		
				 		}
				 		setupNewsScroll();
				 }			 
			 	 jQuery('#scroll-container').feverSlide({
   						 startPanel  : 1,   // start with this panel
   						 width       : jQuery(window).width(), // overall width of movingBoxes
						 imageheight : 258, //height of slider image
						 imagewidth  : 167,
						 name : v //width of slider image
  				 }); 
  				 
  			
  			jQuery('.page-li').hover(
  				function(e){
  					jQuery(this).css({'background': '#000000','color':jQuery(this).css('background-color')});

  				},
  				function(){
  					jQuery(this).css({'background-color': jQuery(this).css('color'),'color': '#000000'});

  			});
  			
				
				if(json['section'] == "about"){
						jQuery("#scroll-container").feverSlide('showAbout');
						jQuery('#nav-bar').animate({
							'bottom':'0'
						});
						jQuery('#mini-popup-wrapper').remove();
						
					}
					else if(json['section'] == "micro"){
					var p = json['pagenation'];
						jQuery("#scroll-container").feverSlide('showUser',json['profile'], json['content'],'nothin', p, json['pages'], json['mini-slider']);
						jQuery('#nav-bar').animate({
							'bottom':'0'
						});
						jQuery('.page-li.'+json['profile']).click(function(){

							var p = parseInt(jQuery(this).attr('id'));
							whichPage(p,json['profile']);
			
						});
					}
					if(ep == "/fever-1-17/"){
					    jQuery("#popup-wrapper").fadeOut();
						jQuery("#background").fadeOut();
						jQuery('#nav-bar').animate({
								'bottom':'50'
						});

					}
					sizeLayout();
		   }
		   
		   
	   });
	 }
}
function whichPage(p,s){
	if(jQuery('#'+s+'-page-'+p).hasClass('visible')){
					return;
				}
				else{
					jQuery('.page-wrap').each(function(){
						if(jQuery(this).hasClass('visible')){
							jQuery(this).fadeOut(function(){
								jQuery(this).removeClass('visible');
								jQuery('#'+s+'-page-'+p).fadeIn(function(){
									jQuery(this).addClass('visible');
				
								});
							
							
							});
						}
					
					});
					
				}

}
function init(){
	jQuery('#topNavControl').click(function(){
	
		controlTopNav();
	
	});
	deepLink();
	jQuery('.nav-button').click(function(){
			jQuery('#nav-bar').animate({
							'bottom':'0px'
			});
	
	});
	jQuery("#background").click(function(){
		jQuery('#nav-bar').animate({
								'bottom':'50'
				});
	
	});
	var w = jQuery(window).width();;
	var h = jQuery(window).height();
	var searchWidth = jQuery('#top-search').width();
	
	var navWidth = w - 198 - searchWidth;
	jQuery('#nav-top').css({'width': navWidth});
	var logoOffset = ((h/2) - (126/2));
	var widgetWidth = 1200;
	jQuery('#background').css({
					'height': h + 'px'
		
	});
	if(logoOffset > 0){
		jQuery("#logo").css({
					'padding-top': logoOffset + 'px'
		}
		);
		jQuery("#background-load").css({
					'padding-top': logoOffset + 'px'
		});
	}
	jQuery("#bg").css({
						'height': h + "px",
						'width' : w + "px",
						'position' : "fixed",
						'z-index'  : -1000
	});
	var images = new Array();
	//refrences to the images for the backgrounds
	if(w > 0 && h > 0){
	var imageArray = new Array(
							"http://www.fevercreative.com/images/1.JPG",
							 "http://www.fevercreative.com/images/2.JPG",
							 "http://www.fevercreative.com/images/3.JPG",
							 "http://www.fevercreative.com/images/4.JPG",
							 "http://www.fevercreative.com/images/5.JPG",
							 "http://www.fevercreative.com/images/6.JPG",
							  "http://www.fevercreative.com/images/7.JPG"
							
							);
	}
	else if( w > 0 && h > 0){
	var imageArray = new Array(
							"http://www.fevercreative.com/images/1.JPG",
							 "http://www.fevercreative.com/images/2.JPG",
							 "http://www.fevercreative.com/images/3.JPG",
							 "http://www.fevercreative.com/images/4.JPG",
							 "http://www.fevercreative.com/images/5.JPG",
							 "http://www.fevercreative.com/images/6.JPG",
							  "http://www.fevercreative.com/images/7.JPG"
							 
							);
	
	}
	//setupBG(imageArray);
	//loadBackgroundAndTimer();
	resize();
	
	 var oldX  = 0;

	 jQuery('#scroll-container').feverSlide({
   		 startPanel  : 1,   // start with this panel
   		 width       : w, // overall width of movingBoxes
		 imageheight : 250, //height of slider image
		 imagewidth  : 167 //width of slider image
   });
   
   
 
   /*
jQuery('#menu-item-about').click(function(){
	  
	   jQuery("#background").css({"opacity" : "0.8"})
                            .fadeIn("slow");

			jQuery('#popup-wrapper').fadeIn();
			jQuery('#scroll-container').css({
										'border-top-style': 'solid',
										'border-top-width': '1px',
										'border-top-color': jQuery('#menu-item-about').css('color')
			});
			///////////////////////
	  jQuery('#scroll-container').css({
							'border-bottom-style': 'solid',
							'border-bottom-width': '1px',
							'border-bottom-color': jQuery('#menu-item-about').css('color'),
			});
	jQuery("#background").click(function(){
            jQuery("#background").fadeOut();
            jQuery("#popup-wrapper").fadeOut(function(){
            
				jQuery('#popup-wrapper').removeClass('visible');
				
				
			});
			jQuery(".user-complete-view").hide();
			jQuery('.boxgrid').show();
			jQuery('.arrow').show();
			jQuery('#scroll-container').css({
				 	height: (250 + 15) + "px"
			 },500);
			jQuery('#image-scroll-list').draggable('option','disabled',false);
			jQuery.address.value(""); 
        });

		
	});
*/
	sizeLayout();
	var c = window.setInterval("sizeLayout()", 2000);
}
function closeBackground(){
	
	
}
function createBGOBJ(url, burl){
	background = new Object();
	background.url = url;
	background.burl = burl;
	return background;
}
var t;
function fadeNews(len, cur){
	var current = cur;
	if(len > 0 && cur <= len){
		jQuery('#t-' + cur).fadeIn(300,function(){
		
			t = setTimeout(function(){
				jQuery('#t-' + cur).fadeOut(400,function(){
					current ++;
					if(current > len){
						current = 1;
					}
					fadeNews(len, current);
				});
			}, 3000);
		
		});
	}else if(cur < len){
		/*
cur = 1;
		alert('changing cur to ' + cur);
		fadeNews(len,cur);
*/
	}

}
function setupNewsScroll(){
	clearTimeout(t);
	var txtCount = 0;
	jQuery('.tweet').each(function(){
		txtCount++;
	
	});
	
	if(txtCount > 0 ){
		fadeNews(txtCount, 1);
	}
	/*
jQuery("div#scrollingText").smoothDivScroll({autoScroll: "always", autoScrollDirection: "endlessloopright", autoScrollStep: 1, autoScrollInterval: 15});
	jQuery("div#scrollingText").bind("mouseover", function(){
				jQuery("div#scrollingText").smoothDivScroll("stopAutoScroll");
			});
			
			jQuery("div#scrollingText").bind("mouseout", function(){
				jQuery("div#scrollingText").smoothDivScroll("startAutoScroll");
			});
*/
			
}
function controlTopNav(){
	var control = jQuery('#topNavControl');
	if(control.hasClass('closed')){
		jQuery('#top-search').fadeIn();
		jQuery('#top-links').fadeIn();
		jQuery('#top-nav').animate({
			'height': '60px'
		},300,function(){
			control.removeClass('closed').addClass('open').attr('src','images/arrow_up.png').hover(function(){
				control.attr('src','images/arrowON_up.png');
			},function(){
				control.attr('src','images/arrow_up.png');
			});
			
		
		});
	}else if(control.hasClass('open')){
		jQuery('#top-links').fadeOut();
		jQuery('#top-search').fadeOut(function(){
			//jQuery(this).css({'display':'none'});
			//jQuery('#top-expand-wrapper').css({'margin-top': '-25px'});
			
		});
		jQuery('#top-nav').animate({
			'height': '5px'
		},300,function(){
			control.removeClass('open').addClass('closed').attr('src','images/arrow_down.png').hover(function(){
				control.attr('src','images/arrowON_down.png');
			},function(){
				control.attr('src','images/arrow_down.png');
			});
			
		
		});
	}

}

function loadBackgroundAndTimer(){
	
	//preload the images
	for (var i = 0; i < imageArray.length; i++)
		{
		image = new Image();
		image.src = imageArray[i];
		images.push(image);
		}
		
	var imageNum = 0;
	// set a timer to iterate through our array of images
	window.setInterval(function(){
				jQuery('#bg').attr({
							'src': images[imageNum].src
				});
				
				imageNum++;
				if(imageNum >= images.length){imageNum = 0;}
	},5000);
}
//this is a binded event which is called when a window is resized, it re-calibrates the background based on the dimension 
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=600,height=400,scrollbars=yes');
return false;
}
function resize(){
	
	jQuery(window).resize(function(){
		var w = jQuery(window).width();
		var h = jQuery(window).height();
		var searchWidth = jQuery('#top-search').width();
		sizeLayout();
		
		var navWidth = w - 198 - searchWidth;
		jQuery('#nav-top').css({'width': navWidth});
	
		jQuery('#background').css({
					'height': h + 'px'		
		});
		var logoOffset = ((h/2) - (126/2));
		if(logoOffset > 0){
		  jQuery("#logo").css({
			 'padding-top': logoOffset + 'px'
		}
		);
		jQuery("#background-load").css({
					'padding-top': logoOffset + 'px'
		});
	}
		jQuery("#bg").css({
						'height': h  + "px",
						'width' : w + "px",
						'position' : 'fixed',
						'z-index'  : -1000
	});
	});
	
	
}
