/*
 * Global Variables 
 */
var cfs = 0;
var slider = 0;
var sub_slide =0;
var slide_tracker =0;
var bgs =0;
var ms =0;
var em =0;
var accord_i = 0;
var lag_close = 0;
var center_lag_close = 0;
var onpage = true;
var gsvars = getVars();
window.onblur = function(){
	onpage = false;
}
window.onfocus = function(){
	onpage = true;
}
/*
 * Window onload Functions
 */
window.onload = function() {
	var wh = getWHofWindow();
	myWidth = wh.width;
	myHeight = wh.height;
	  // Highlight left tab corner when hover over "home"
	  $("#home_tab")[0].onmouseover = function () {
		  $("#tab_left_corner")[0].style.background = "url('/wp-content/themes/alphagraphics/images/1600/header/left_main_nav_corner.png') no-repeat 0 0";
	  }
	  // Return to original on mouseout
	  $("#home_tab")[0].onmouseout = function () {
		  $("#tab_left_corner")[0].style.background = "url('/wp-content/themes/alphagraphics/images/1600/header/left_main_nav_corner_highlighted.png') no-repeat 0 0";
	  }
	  
	  // Hide messages after 3 seconds
	  setTimeout(function(){
		  $(".message").each(function(){
			  $(this).animate({height:"0px"},500,function(){
				  $(this).hide();
			  });
		  });
	  },3000);
	  
	  // Bind onclick to show and hide dropdown menu onclick
	  		$('#red_top_right_button').click(function(){
	  			if($(this).hasClass('loco_br')){
	  				redirectForCountrySearch2('United Kingdom');
	  			}else if($(this).hasClass('loco_po')){
	  				window.location = "/location/webregion/brasil";
	  			}else{
		  			$('#agonline').hide();
		  			var f = $("#find_center");
		  			showDropDown(this,f,false);
	  			}
	  		});
	  		
	  // Bind onclick to show and hide dropdown menu onclick
	  		$('#black_top_right_button').click(function(){
	  			$('#find_center').hide();
	  			var f = $("#agonline");
	  			showDropDown(this,f,false);
	  		});
	  		
	  		var f = $("#agonline");
  			showDropDown($('#black_top_right_button')[0],f,true);
	  		
	  		// Bind onclick to li elements in custom content section of category
			// pages for open/close animations
	  		$('.read_more').click(function(){
	  			var d = $(this).children('div');
	  			if(typeof d[0] != 'undefined'){
	  				if($(d[0]).height() == 0){
		  				d[0].style.height = 'auto';
		  				var h = $(d[0]).height();
		  				d[0].style.height = '0px';
		  				h+=20;
	  				}else{
	  					var h = 0;
	  				}
	  				$(d[0]).stop(true,true).animate({height:h+'px'},{
	  				  duration: 1000,
	  				  step: function(now, fx) {$('.red_button').each(function(){this.className = this.className;	});}
	  				});
	  			}
	  		});
	  	// Bind onclick to li elements in custom content section of category
		// pages for open/close animations
	  	if(window.location.pathname != '/site_map')
	  		$('#custom_block li').click(function(e){
	  			if(e.target.tagName != "LI")
	  				return false;
	  			var oha = $(this).attr('org_height');
	  			var oh = $(this).height();
	  			if(typeof oha == "undefined"){
	  				$(this).attr('org_height',oh);
	  				oha = oh;
	  			}
	  			if(oha >= $(this).height()){
		  			this.style.height = "auto";
		  			var h = $(this).height();
		  			this.style.height = oh+"px";
		  			$(this).animate({height:h+"px"},1000);
		  			$(this).attr('org_height',oh);
		  			$(this).addClass('ul_open');
	  			}else{
	  				$(this).animate({height:oha+"px"},1000);
	  				$(this).removeClass('ul_open');
	  			}
	  		});
	  	// ONLY runs on home page
		  if(window.location.pathname == '/'){
				var bn = Math.floor($("#big_image")[0].scrollLeft/1775);
				var bm = $("#banner_slider div")[bn];
				var ca = new customAni(bm);
			  // Start Banner Transitions
			  	startSlider();
			  // Start Pointer Focus Rotations
			  	$($('.pointer')[0]).animate({opacity:1},1000,function(){
			  		$($('.pointer')[1]).animate({opacity:1},1000,function(){
			  			$($('.pointer')[2]).animate({opacity:1},1000,function(){
			  				setTimeout(function(){
			  					$($('.pointer')[0]).animate({opacity:.4},1000);
			  					$($('.pointer')[1]).animate({opacity:.4},1000,function(){
			  						startPointerRotation();
			  						$($('.pointer')[2]).animate({opacity:.4},1000);
			  					});
			  				},2000);
					  	});
				  	});
			  	});
			  // Bind onclick function to unfocused "Dots"
			  	$(".greybox_dot_off").click(function(){moveByDot(this);});
			  // Bind mouseover to hidden TD grid over banner for nugging
				// effect
//				  $("#banner_view_frame td").mouseover(function(){
//					  var col = ($(this).parent("tr").children().index(this))+1;
//					  var row = ($(this).parent("tr").parent("tbody").children().index($(this).parent("tr")))+1;
//					  var movement = 20;
//					  ml = Math.ceil((((10-col)/10)*movement)-20);
//					  mt = Math.ceil((((6-row)/6)*movement)-20);
//					  var bn = Math.floor($("#big_image")[0].scrollLeft/1775);
//					  var bm = $("#banner_slider div")[bn];
//					  ca.e = bm;
//					  ca.stop();
//					  ca.nudgeFloat(ml,mt,1250);
//					  $(bm).stop(true, true).animate({backgroundPosition: ml+"px "+mt+"px"},500); 
// bm.style.backgroundPosition = ml+"px "+mt+"px";
//				  });
			// Make Header Text Fly in from left to right
				  $("#middle_slider2").each(function(){
					  var e = this;
					  em = $("#middle_slider1").width()+10;
					  ms = setInterval(function(){
						  flyIn(e,20);
					  },50);
				  });
			// Bind onclick to pointers for Scissor Open Visulation
				  $(".pointer").click(function(){
					  clearInterval(slider);
					  var e = $("#"+this.id+"_dropper");
					  $(e).css({
						"width":"0px",	  
						"height":"0px",	  
						"display":"block",
						"left": (this.offsetLeft+25)+"px",
						"top": this.offsetTop+"px"
					  });
					  var o = this;
					  var pi = setInterval(function(){
						  scissorOpen(e[0],15,o.offsetLeft-213,238,86,pi);
					  },50);
				  });
			// Bind onclick to droppers for Scissor Open Visulation
				  $(".dropper").click(function(){
					  var e = this;
					  var n = e.offsetLeft+184;
					  var pi = setInterval(function(){
						  scissorClose(e,15,n,238,86,pi);
					  },50);
				  });
			// Bind onclick for "Home Box" transitions
				  $(".home_box").click(function(){selectHomeBox(this)});
			// Bind onclick to "Three Kings" read more buttons for proper
			// open/closing
				  $(".king_rm").click(function(){
					  if($(this).hasClass('king_open')){
						  $(this).removeClass('king_open');
						  var n = -30;
					  }else{
						  $(this).addClass('king_open');
						  var n = 30;
					  }
					  slideKingRM($(this).parent('div'),n,this)
				  });
			// Align "Three Kings" content holders for proper display
				  var e = $(".king_floater_holder")
				  var pos = findPos( $("#middle_king_body")[0]);
				  e[0].style.left = (pos.left-3)+"px";
				  e[0].style.top = (pos.top-21)+"px";
				  var pos = findPos( $("#right_king_body")[0]);
				  e[1].style.left = (pos.left+10)+"px";
				  e[1].style.top = (pos.top-21)+"px";
			// Align "Content" content holders for proper display
				  $(".social_floater_holder").each(function(){
					  var pos = findPos($("#social_box_text")[0]);
					  this.style.left = pos.left+"px";
					  this.style.top = pos.top+"px";
					  if($(this).hasClass('social_on')){
						  $(this).show();
					  }
				  });
			// Bind onclick to "Social" icons for proper transistion between
			// social types
				  $("#social_icons img").click(function(){
					  
					  var arrow = $('#socialbox_arrow_down');
					  arrow.animate({'left':Math.ceil(this.offsetLeft+(($(this).width()-10)/2))+"px"},500);
					  var class_in = $(this).attr('class_in');
					  slideto = 60;
					  var e = $(".social_open");
					  if(typeof e[0] != "undefined"){
						  var sib = $(e).siblings();
						  $(sib[0]).animate({height:"60px"},1000,function(){
							  $($(e).parent('div').siblings()[0]).hide();
							  e[0].innerHTML = gsvars[17];
							  $(e).removeClass("social_open");
							  $(sib[0]).removeClass('social_border');
							  $(e).parent('div').removeClass('social_box_backer_w_image');
						  });
					  }
					  $(".social_on").fadeOut(1000,function(){
						  $("."+class_in).fadeIn(1000);
						  $(".social_on").removeClass('social_on');
						  $("."+class_in).addClass('social_on');
					  });
				  	});
			// Bind onclick to "Social" read more buttons for proper
			// open/closing of content
				  $(".socialbox_read_more").click(function(){
					  var sib = $(this).siblings();
					  if($(this).hasClass('social_open')){
						  var e = this;
						  $(sib[0]).animate({height:"60px"},1000,function(){
							  $($(e).parent('div').siblings()[0]).hide();
							  e.innerHTML = gsvars[17];
							  $(e).removeClass("social_open");
							  $(sib[0]).removeClass('social_border');
							  $(e).parent('div').removeClass('social_box_backer_w_image');
						  });
					  }else{
						  sib[0].style.height ="auto";
						  var slideto = $(sib[0]).height();
						  slideto = slideto > 260 && !$(this).parent().parent().hasClass('facebook') ? 260 : slideto;
						  sib[0].style.height ="60px";
						  $($(this).parent('div').siblings()[0]).show();
						  $(sib[0]).addClass('social_border');
						  $(this).parent('div').addClass('social_box_backer_w_image');
						  var e = this;
						  $(sib[0]).animate({height:slideto+"px"},1000,function(){
							  e.innerHTML = gsvars[59];
							  $(e).addClass("social_open");
						  });
					  }
				  });
		  }
		 
		  $('#ny_zipcode').keydown(function(e){
			  if(e.keyCode == 13)
				  getLatLongByZipCode(this.value);
		  });
		  $('.lc_expand').each(function(){
			  var p = $(this).prev();
			  p[0].style.width = "0px";
		  });
		  $('.lc_expand').click(function(){
				var p = $(this).prev();
				if(p.width() == 0){
					this.id = this.id+"_close"
					p[0].style.width = "auto";
					var w = p.width();
					p[0].style.width = "0px";
					p.animate({'padding-left':"10px",'padding-right':"10px",width:w+"px"},1000,function(){
						p[0].style.width = w+"px";
					});
				}else{
					var pi = this;
					p.animate({'padding-left':"0px",'padding-right':"0px",width:"0px"},1000,function(){
								pi.id = pi.id.replace("_close","");
						});
				}
			});
		  // Set "buffers" for proper page layout
		  bufferSet();
		  
		  // Check to see if pages needs to be scaled and scale if needed - MOSTLY
		  // REMOVED
		  // TODO: Make sure this function is needed and trim down / remove
		  checkForScale();
}

/*
 * Window on Resize Functoins
 */
window.onresize = function(){
	 // Set "buffers" for proper page layout
		bufferSet();
	// Cleartimeout of checkforscale function, to avoid mass amounts of firing
	// on resizing
		clearTimeout(cfs);
	// setTimeout of checkforscale function, to avoid mass amounts of firing on
	// resizing
	// Check to see if pages needs to be scaled and scale if needed - MOSTLY
	// REMOVED
	// TODO: Make sure this function is needed and trim down / remove
		cfs = setTimeout('checkForScale()',500);
}

/*
 * Specific function for a horiziontal accordian on the category pages param e:
 * HTML Element (object) - Accord title clicked (this)
 */
function accordian(e){
	if(!$(e).hasClass('red') && accord_i == 0){
		var cur_mark = $('.red');
		var img_to_open = $(e).next();
		var img_to_close = $(cur_mark).next();
		var m = $(img_to_close).width();
		var time = 500;
		var cm = Math.floor(m/(time/40)); 
		$(img_to_open)[0].style.width = "0px";
		$(img_to_open).show();
		$(e).addClass('red');
		$(cur_mark).removeClass('red');
		if($(e).hasClass('last'))
			$("#accordian")[0].style.backgroundColor = "transparent";
		accord_i = setInterval(function(){
			$(img_to_open)[0].style.width = ($(img_to_open).width()+cm)+"px";
			if(($(img_to_close).width()-cm) > 0){
				$(img_to_close)[0].style.width = ($(img_to_close).width()-cm)+"px";
			}else{
				$(img_to_close)[0].style.width = "0px";
			}
			if($(img_to_open).width() >= m){
				clearInterval(accord_i);
				if($(cur_mark).hasClass('last'))
					$("#accordian")[0].style.backgroundColor = "#333333";
				accord_i = 0;
				$(img_to_open)[0].style.width = m+"px"; 
				$(img_to_close)[0].style.width = "0px";
				$(img_to_close).show();
			}
		},40);
	}
}

/*
 * General function for toggling dropdown menu for element Also moves element
 * centered under clicked element Will not display off left egde of page @param
 * e: HTML Element (object) - Menu element clicked for dropdown display (this)
 * @param f: HTML Element (object) - Dropdown Container
 */
function showDropDown(e,f,bypass){
	myWidth = $(window).width()+$(window).scrollLeft();
	if((!bypass && $(f).css('display') == 'none') || (bypass && window.location.search.indexOf("message") > 0)){
		var fw = $(f).width();
		var w = $(e).width();
		var h = $(e).height();
		var p = parseInt($(e).css('paddingTop'));
		var pos = findPos(e);
		var new_left =  pos.left-((fw-w)/2);
		var old_left = new_left;
		new_left = new_left > (myWidth-fw)-20? (myWidth-fw)-20: new_left;
		$(f)[0].style.left = new_left+"px";
		$(f)[0].style.top = (pos.top+h+p)+"px";
		if(f[0].id == "find_center"){
			a_pos = old_left != new_left?((old_left-new_left)+84)+"px": "124px";
			$("#red_arr")[0].style.left = a_pos;
		}else if(f[0].id == "agonline"){
			a_pos = old_left != new_left?((old_left-new_left)+84)+"px": "124px";
			$("#black_arr")[0].style.left = a_pos;
		}
		$(f).show();
	}else{
		$(f).hide();
	}
}

/*
 * Function Used to Return style of an element DEPRECATED: Use jQuery .css()
 * extension instead @param el : HTML Element (object) @param cssprop: CSS
 * property name (string)
 */
function getStyle(el, cssprop) {
    if (el.currentStyle) {
            return el.currentStyle[cssprop];
    } else if (document.defaultView && document.defaultView.getComputedStyle) {
            return document.defaultView.getComputedStyle(el, "")[cssprop];
    } else {
            return el.style[cssprop];
    }
}   

/*
 * Randomly increases then decreases opacity of pointer element FOR VISUALTION
 */
function startPointerRotation(){
	runSPAni();
	setInterval(function(){
		runSPAni();
	},3000);
}

function runSPAni(){
	var rnd = Math.floor(Math.random()* $('.pointer').length);
	var p = $('.pointer')[rnd];
	$(p).animate({opacity:1},1000,function(){
		setTimeout(function(){
			$(p).animate({opacity:.4},1000);
		},2000);
	});
}

/*
 * Controls expanding and collasping of the "Three Kings" Elements on the home
 * page @ param e: HTML ELement (object) - Read More Button Parent @ param m:
 * Movement Every Cycle (integer) @ param o: HTML ELement (object) - Read More
 * Button
 */
function slideKingRM(e,m,o){
	var exp = $(e).siblings()[0];
	var abs = $(exp).children()[0];
	if(m > 0){
		abs.style.height = "auto";
		var expandto = $(abs).height();
		abs.style.height = "54px";
		var i = setInterval(function(){
			abs.style.height = ($(abs).height()+m)+"px";
			exp.style.height = ($(abs).height()+m)+"px";
			if($(abs).height() > 184){
				$($(e).parent('div').siblings()[0]).show();
				$($(e).parent('div').siblings()[1]).show();
				$($(e).parent('div')).addClass('king_text_backer');
				$($(e).parent('div').parent('div')).addClass('king_width_expander');
			}
			if($(abs).height() > expandto){
				abs.style.height = expandto+"px";
				exp.style.height = expandto+"px";
				o.innerHTML = gsvars[59];
				clearInterval(i);
			}
		},50);
	}else{
		var i = setInterval(function(){
			abs.style.height = ($(abs).height()+m)+"px";
			exp.style.height = ($(abs).height()+m)+"px";
			if($(abs).height() < 184){
				$($(e).parent('div').siblings()[0]).hide();
				$($(e).parent('div').siblings()[1]).hide();
				$($(e).parent('div')).removeClass('king_text_backer');
				$($(e).parent('div').parent('div')).removeClass('king_width_expander');
			}
			if($(abs).height() <= 54){
				abs.style.height = "54px";
				exp.style.height = "54px";
				o.innerHTML = gsvars[17];
				clearInterval(i);
			}
		},50);
	}
}

/*
 * Controls Movement of Black Arrow Background and AJAX in new content for "Home
 * Box" on front page onclick @ param e: HTML ELement (object) - this FOR
 * VISUALTION
 */
function selectHomeBox(e){
	var m = e.offsetTop-$(e).parent('div')[0].offsetTop;
	var n = e.id.replace("box1_text","");
	$.ajax({
		  type: "POST",
		  url: "/ajax/gethomeboxdata",
		  data:"offset="+n,
		  dataType: 'html',
		  success: function(data){
				if(data != null){
					$("#box1_content").fadeOut(100,function(){
						this.innerHTML = data;
						var st = m;
						var sl = -2;
						$("#box1_text").animate({backgroundPosition: sl+"px "+st+"px"},500);
						$(this).fadeIn(100);
					  	$(".dyknum").click(function(){
					  		var n = parseInt(this.innerHTML);
					  		$(".dksel").removeClass('dksel');
					  		$(this).addClass('dksel');
					  		var scroll = (n-1)*140;
					  		scrollTo($('#dyk_slider')[0],0,scroll);
					  	});
					});
				}
		  }
		});
	$(".home_box_selected").removeClass("home_box_selected").addClass("home_box");
	$(e).removeClass("home_box");
	$(e).addClass("home_box_selected");
	$(e).unbind("click");
	$(".home_box").unbind("click");
	 $(".home_box").click(function(){selectHomeBox(this)});
}

function scrollTo(e,x,y){
	var cx = e.scrollLeft;
	var cy = e.scrollTop;
	var mx = x -cx;
	var my = y - cy;
	var mpi = my != 0?my/8:mx/15;
	var i = setInterval(function(){
		var done = 0;
		if(mpi > 0){
			if(e.scrollTop+mpi >= y){
				e.scrollTop = y;
				done++;
			}else{
				e.scrollTop = e.scrollTop+mpi; 
			}
			if(e.scrollLeft+mpi >= x){
				e.scrollLeft = x; 
				done++;
			}else{
				e.scrollLeft = e.scrollLeft+mpi; 
			}
		}else{
			if(e.scrollTop+mpi <= y){
				e.scrollTop = y; 
				done++;
			}else{
				e.scrollTop = e.scrollTop+mpi; 
			}
			if(e.scrollLeft+mpi <= x){
				e.scrollLeft = x; 
				done++;
			}else{
				e.scrollLeft = e.scrollLeft+mpi; 
			}
		}
		if(done >= 2){
			clearInterval(i);
		}
	},40);
}
/*
 * DEPRECATED USE jQuery Animate General Function to Slide Background Position
 * on Y-axis only @ param e: HTML ELement (object) - Background to slide @ param
 * inc: Movement per cycle (integer) @ param to: Movement end point (integer) @
 * param i: JS Interval (interval resource) USED FOR VISULATION
 */
function slideBackground(e,inc,to,i){
	var bgp = $(e).css('backgroundPosition').split(" ");
	var sl = parseInt(bgp[0]);
	var st = parseInt(bgp[1]);
	var nt = st+inc;
	if(nt > to && inc > 0){
		nt = to;
		$(e).css('backgroundPosition',sl+"px "+nt+"px")
		clearInterval(i);
	}else if(nt < to && inc < 0){
		nt = to;
		$(e).css('backgroundPosition',sl+"px "+nt+"px")
		clearInterval(i);
	}
	 $(e).css('backgroundPosition',sl+"px "+nt+"px")
}
/*
 * General Function for causing an element to scissor close animation @ param e:
 * HTML Element (object) - element to animate @ param m: Movement per cycle
 * (integer) @ param o: Ending Offset Left Position (integer) @ param w:
 * Original Width of Element (integer) @ param h: Original Height of Element
 * (integer) @ param pi: JS Inteval (interval resource)
 */
function scissorClose(e,m,o,w,h,pi){
	var r = h/m;
	var mw = Math.ceil(w/r);
	var done =0;
	e.style.width = (e.offsetWidth-mw)+"px";
	e.style.height = (e.offsetHeight-m)+"px";
	e.style.left = (e.offsetLeft+mw)+"px";
	if(e.offsetHeight-m <= 0){
		e.style.height = "0px";
		done++;
	}
	if(e.offsetWidth-mw <= 0){
		e.style.width = "0px";
		done++;
	}
	if(e.offsetLeft > o){
		e.style.left = o+"px";
	}
	if(done == 2){
		clearInterval(pi);
	}
}
/*
 * General Function for causing an element to scissor open animation @ param e:
 * HTML Element (object) - element to animate @ param m: Movement per cycle
 * (integer) @ param o: Ending Offset Left Position (integer) @ param w: Ending
 * Width of Element (integer) @ param h: Ending Height of Element (integer) @
 * param pi: JS Inteval (interval resource)
 */
function scissorOpen(e,m,o,w,h,pi){
	var r = h/m;
	var mw = Math.ceil(w/r);
	var done =0;
	e.style.width = (e.offsetWidth+mw)+"px";
	e.style.height = (e.offsetHeight+m)+"px";
	e.style.left = (e.offsetLeft-mw)+"px";
	if(e.offsetHeight > h){
		e.style.height = h+"px";
		done++;
	}
	if(e.offsetWidth > w){
		e.style.width = w+"px";
		done++;
	}
	if(e.offsetLeft < o){
		e.style.left = o+"px";
	}
	if(done == 2){
		clearInterval(pi);
	}
}

/*
 * Specfic function for front page sliding in text from the left to right @param
 * e: HTML Element (object) - First element to slide in, it is then hardcoded
 * the next one to slide in @param n: Movement per cycle (integer) TODO: Make
 * function use callbacks instead of hardcoding in next slider
 */
function flyIn(e,n){
	e.style.left = (parseInt(e.offsetLeft)+n)+"px";
	if(parseInt(e.offsetLeft) > em){
		e.style.left = em+"px";
		em = 5;
		clearInterval(ms);
		if(e.id != "middle_slider1"){
			 $("#middle_slider1").each(function(){
				  var e = this;
				  ms = setInterval(function(){
					  flyIn(e,20);
				  },50);
			  });
		}
	}
}

/*
 * General function for sliding both x and y axis of background image the same
 * amount each cycle ClearsInterval if it is already started and restarts
 * Attempt at smother background image nugging - WORK IN PROGRESS @ param e:
 * HTML Element (object) - Element to act on @ param l: Maxium
 * background-position left (integer) @ param t: Maxium background-position top
 * (integer) @ param m: Movement Per Cycle (integer)
 */
function backgroundSlide(e,l,t,m){
	clearInterval(bgs);
	bgs = setInterval(function(){
		var done = 0;
		var bgp = e.style.backgroundPosition.split(" ");
		var sl = parseInt(bgp[0]);
		var st = parseInt(bgp[1]);
		var nl = sl+m;
		var nt = st+m;
		if(nt > t){
			nt = t;
			done++;
		}
		if(nl > l){
			nl = l;
			done++;
		}
		e.style.backgroundPosition = nl+"px "+nt+"px";
		if(done == 2)
			clearInterval(bgs);
	},50);
}

/*
 * Specific Funciton to move Home Page Banner to correct slide on Dot Click @
 * param e: HTML Element (object) - Element "Dot" clicked (this)
 */
function moveByDot(e){
	  clearInterval(slider);
	  var on = $(".greybox_dot")[0];
	  $(on).addClass('greybox_dot_off');
	  $(on).removeClass('greybox_dot');
	  $(".greybox_dot_off").unbind('click');
	  $(".greybox_dot_off").click(function(){moveByDot(this);});
	  var tmp_id = e.id.replace("gd_","");
		$(e).addClass('greybox_dot');
		$(e).removeClass('greybox_dot_off');
		$(e).unbind('click');
		faderOut(tmp_id-1);
		if(typeof $("#big_image")[0] == 'undefined'){
			var bi = $("#big_image_cl")[0];
			var bij = $("#big_image_cl");
			var nbi = 1080;
		}else{
			var bi = $("#big_image")[0];
			var bij = $("#big_image");
			var nbi = 1775;
		}
		scrollTo(bi,((nbi-bij.width())/2)+((tmp_id-1)*nbi),0);
}

/*
 * Specific Function to Adjust "Buffers" on pages need for dynamic display
 */
function bufferSet(){
	var wh = getWHofWindow();
	myWidth = wh.width;
	myHeight = wh.height;
	
	//Move AG Online onto page
	/*if(document.body.scrollWidth > $(window).width()){
		$('#top_right_buttons')[0].style.marginRight = (document.body.scrollWidth-$(window).width()-22)+"px";
	}else{
		$('#top_right_buttons')[0].style.marginRight = "0px";
	}*/
	// Adjust jsbuffer class to proper width
		$(".jsbuffer").each(function(){
			if(document.body.scrollWidth > $(window).width()){
				this.style.width = Math.ceil(document.body.scrollWidth-$(window).width())+"px";
//				this.style.right =  Math.ceil((document.body.scrollWidth-1100)/2)+"px";
				this.style.right = "0px";
			}else{
				this.style.width =  Math.ceil((document.body.scrollWidth-1100)/2)+"px";
				this.style.right = "0px";
			}
		});
	
	// ONLY used for home page
	if(window.location.pathname == "/"){
		// Adjust jsbuffer2 class to proper width
// $(".jsbuffer2").each(function(){
// this.style.width = ((myWidth-$("#big_image").width())/2)+"px";
// });
		// Adjust jsbgmove class to proper background-position
// $(".jsbgmove").each(function(){
// this.style.backgroundPosition =
// (((myWidth-$("#big_image").width())/2)-109)+"px, 0";
// });
		// Setting min width for processing
			myWidth =  (myWidth < 1115)?1115:myWidth;
			
		// Centering banner
//			$("#big_image")[0].scrollLeft = ((1775-myWidth)/2);
//			$("#big_image")[0].scrollTop = ((520-437)/2);
		
		// Aligning Shadow Box above banner
// $("#image_bx")[0].style.left = ((myWidth - 1100)/2)-
// ((myWidth-$("#big_image").width())/2)+"px";
	
		// Aligning pointers above banner
// $(".pointer").each(function(){
// this.style.right = (parseInt($(this).attr('org_right'))+ ((myWidth -
// 1100)/2)- ((myWidth-$("#big_image").width())/2))+"px";
// });
		
		// Aligning social content boxes
		  $(".social_floater_holder").each(function(){
			  var pos = findPos($("#social_box_text")[0]);
			  this.style.left = pos.left+"px";
			  this.style.top = pos.top+"px";
		  });
		  
		// Aligning "three kings" content boxes
		  var e = $(".king_floater_holder")
		  var pos = findPos( $("#middle_king_body")[0]);
		  e[0].style.left = (pos.left-3)+"px";
		  e[0].style.top = (pos.top-21)+"px";
		 $(e[0]).show();
		  var pos = findPos( $("#right_king_body")[0]);
		  e[1].style.left = (pos.left+10)+"px";
		  e[1].style.top = (pos.top-21)+"px";
		  $(e[1]).show();
	}
}

/*
 * Specific function to change dynamic elements on page resize
 */
function checkForScale(){
	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+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    // IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	
	// Adjust jsbuffer2 class to proper width
		$(".jsbuffer2").each(function(){
			this.style.width = ((myWidth-$("#big_image").width())/2)+"px";
		});
	// Adjust jsbgmove class to proper background-position
		$(".jsbgmove").each(function(){
			this.style.backgroundPosition = ((((myWidth-$("#big_image").width())/2)-109)+$("#big_image")[0].scrollLeft)+"px, 0";
		});

// myWidth = (myWidth < 1024)?1024: (myWidth > 1280)?1600:myWidth;
	
	// Scaling removed hardcoded for proper images/css
// myWidth =1600;
	
	/*
	 * Sets width of page in session using AJAX ONSUCCESS: ScalePage - REMOVED
	 * Changed Static left corner tab backgorund - REMOVED
	 */
// $.ajax({
// type: "POST",
// url: "/fisher/session/",
// data:"res="+myWidth,
// dataType: 'json',
// success: function(data){
// if(data.res != null){
// scalePage(data.res);
// $("#tab_left_corner")[0].style.background =
// "url('../images/"+myWidth+"/header/left_main_nav_corner_highlighted.png')
// no-repeat 0 0";
// }
// }
// });
	if(window.location.pathname == '/'){
	if(typeof $("#big_image")[0] == 'undefined'){
		var bi = $("#big_image_cl")[0];
		var bij = $("#big_image_cl");
		var nbi = 1080;
	}else{
		var bi = $("#big_image")[0];
		var bij = $("#big_image");
		var nbi = 1775;
	}
	var on = $(".greybox_dot")[0];
	var tmp_id = on.id.replace("gd_","");
//	scrollTo(bi,((nbi-bij.width())/2)+((tmp_id-1)*nbi),0)
	bi.scrollLeft = (((nbi-bij.width())/2)+((tmp_id-1)*nbi));
	}
}
/*
 * General Function changes out all images and css tags for new resolution @
 * param res: Width of window (integer)
 */
function scalePage(res){
	$('link').each(function(){
	    if(this.href.search(/\/[0-9]+\//)){
	        var tmp = this.href.replace(/\/[0-9]+\//,'/'+res+'/');
	        this.href = tmp;
	    }
	});
    $('img').each(function(){
	    if(this.src.search(/\/[0-9]+\//)){
	        var tmp = this.src.replace(/\/[0-9]+\//,'/'+res+'/');
	        this.src = tmp;
	    }
	});
}

/*
 * Function called on load to start interval for banner to slide every 10
 * seconds
 */
function startSlider(){
	slider = setInterval("slide()",10000);
}

/*
 * Handler for each slide transition
 */
function slide(){
	if(onpage){
		slidetracker = 0;
		if(typeof $("#big_image")[0] == 'undefined'){
			var bi = $("#big_image_cl")[0];
			var bij = $("#big_image_cl");
			var bsd =  $("#banner_slider_cl div");
			var nbi = 1080;
			var bgp = "-160px -20px";
		}else{
			var bi = $("#big_image")[0];
			var bij = $("#big_image");
			var bsd =  $("#banner_slider_cl div");
			var nbi = 1775;
			var bgp = "-20px -20px";
		}
		// Resets background position of last slide from the nugging effect
			var bn = Math.floor(bi.scrollLeft/nbi);
			if(typeof bsd[bn-1] != "undefined"){
				var bm = bsd[bn-1];
				bm.style.backgroundPosition = bgp;
			}
		// Checks to see if last slide, if so resets to front
			if(bi.scrollLeft +nbi >= bi.scrollWidth){
				clearInterval(slider);
				bi.scrollLeft = 0;
				return false;
			}
		// Change over to correct "Dot"
			var on = $(".greybox_dot")[0];
			$(on).addClass('greybox_dot_off');
			$(on).removeClass('greybox_dot');
			var tmp_id = on.id.replace("gd_","");
			var next = $("#gd_"+(parseInt(tmp_id)+1))[0];
			if(typeof next == "undefined"){
				next =  $("#gd_1")[0];
				tmp_id = 0;
				clearInterval(slider);
			}
			$(next).unbind('click');
			$(next).addClass('greybox_dot');
			$(next).removeClass('greybox_dot_off');
			$(".greybox_dot_off").unbind('click');
			$(".greybox_dot_off").click(function(){moveByDot(this);});
		// Call to helper function in intervals to cause movement
			scrollTo(bi,((nbi-bij.width())/2)+((tmp_id)*nbi),0);
		// Call to helper function to fadeOut old content
			faderOut(tmp_id);
	}
}

/*
 * Helper function for slide() Moves view div using scrollLeft @param n: Movment
 * per cycle (integer)
 */
function movement(n){
	if(typeof $("#big_image")[0] == 'undefined'){
		var bi = $("#big_image_cl")[0];
		var nbi = 1080;
	}else{
		var bi = $("#big_image")[0];
		var nbi = 1775;
	}
	var start = bi.scrollLeft;
	var moveto = start+n;
	slidetracker += n;
	if(slidetracker > nbi){
		moveto -= slidetracker-nbi;
		clearInterval(sub_slide);
	}
	 bi.scrollLeft = moveto;
}

function printStackTrace() {
	  var callstack = [];
	  var isCallstackPopulated = false;
	  try {
	    i.dont.exist+=0; //doesn't exist- that's the point
	  } catch(e) {
	    if (e.stack) { //Firefox
	      var lines = e.stack.split('\n');
	      for (var i=0, len=lines.length; i<len; i++) {
	        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
	          callstack.push(lines[i]);
	        }
	      }
	      //Remove call to printStackTrace()
	      callstack.shift();
	      isCallstackPopulated = true;
	    }
	    else if (window.opera && e.message) { //Opera
	      var lines = e.message.split('\n');
	      for (var i=0, len=lines.length; i<len; i++) {
	        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
	          var entry = lines[i];
	          //Append next line also since it has the file info
	          if (lines[i+1]) {
	            entry += ' at ' + lines[i+1];
	            i++;
	          }
	          callstack.push(entry);
	        }
	      }
	      //Remove call to printStackTrace()
	      callstack.shift();
	      isCallstackPopulated = true;
	    }
	  }
	  if (!isCallstackPopulated) { //IE and Safari
	    var currentFunction = arguments.callee.caller;
	    while (currentFunction) {
	      var fn = currentFunction.toString();
	      var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf('')) || 'anonymous';
	      callstack.push(fname);
	      currentFunction = currentFunction.caller;
	    }
	  }
	  output(callstack);
	}
	 
	function output(arr) {
	  //Optput however you want
	  alert(arr.join('\n\n'));
	}
	


/*
 * General function to find position of nested element in proportion to the
 * whole page @param obj: HTML Element (object) - Element to locate return:
 * (object) left - offset left in relation to whole page, top - offset top in
 * relation to whole page
 */
function findPos(obj) {
	var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return {left:curleft,top:curtop};
} 
function getVars(){
	var vars = 0; 
	$.ajax({
		type: "POST",
		url: "/ajax/getvars",
		dataType: 'json',
		async:false,
		complete: function(data){
			if (data) {
				vars = jQuery.parseJSON(data.responseText);
			}
		}
	});	
	return vars;
}

/*
 * Specific function to change and display main header navigation Uses AJAX to
 * call in new content ONSUCCESS filled nav holder with new content aligns nav
 * holder properly to element clicked displays nav holder @param e: HTML element
 * (object) - navigation button moused over (this) @param c_id: Category ID in
 * Database (integer)
 */
function showNav(e,c_id){
	$.ajax({
		  type: "POST",
		  url: "/ajax/getnavdata.php",
		  data:"c_id="+c_id,
		  dataType: 'html',
		  success: function(data){
				if(data != ''){
					$("#nav_list_holder")[0].innerHTML = data;
					if(lag_close == 0){
						$("#nav_floater_holder").show();
						$("#nav_floater_holder")[0].style.left = "0px";
						var pos = findPos(e);
						var h = $(e).height();
						var w = $(e).width();
						var p = parseInt($(e).css("padding-top"));
						var fw = $("#nav_floater_holder").width();
						var hw = $("#header").width();
						var hpos = findPos($("#header")[0]);
						$("#nav_floater_holder")[0].style.top = (pos.top+h+p)+"px";
						var new_left = pos.left-((fw-w)/2);
						var right_f = new_left+fw;
						var right_b = hpos.left+hw;
						var wh = getWHofWindow();
						new_left = right_f+20 > wh.width? new_left - (right_f+20-wh.width):new_left;
						$("#nav_floater_holder")[0].style.left = new_left+"px"
						var aw = $("#nav_top_arrow").width();
						new_left = ((fw-aw)/2);
						new_left = right_f+20 > wh.width? new_left + (right_f+20-wh.width):new_left;
						$("#nav_top_arrow")[0].style.left = new_left+"px";
					}else{
						lag_close--;
					}
				}
		  }
		});
}

function showCenterNav(e,name,c_id,cb){
	$.ajax({
		  type: "POST",
		  url: "/ajax/getcenternavdata.php",
		  data:"name="+name+"&c_id="+c_id+"&cb="+cb,
		  dataType: 'html',
		  success: function(data){
				if(data != ''){
					$("#center_nav_list_holder")[0].innerHTML = data;
					if(center_lag_close == 0){
						$("#center_nav_floater_holder").show();
						$("#center_nav_floater_holder")[0].style.left = "0px";
						var pos = findPos(e);
						var h = $(e).height();
						var w = $(e).width();
						var p = parseInt($(e).css("padding-top"));
						var p2 = parseInt($(e).css("padding-bottom"));
						var fw = $("#center_nav_floater_holder").width();
						var hw = $("#header").width();
						var hpos = findPos($("#header")[0]);
						$("#center_nav_floater_holder")[0].style.top = (pos.top+h+p+p2)+"px";
						var new_left = pos.left-((fw-w)/2);
						var right_f = new_left+fw;
						var right_b = hpos.left+hw;
						var wh = getWHofWindow();
						new_left = right_f+20 > wh.width? new_left - (right_f+20-wh.width):new_left;
						$("#center_nav_floater_holder")[0].style.left = new_left+"px"
						var aw = $("#center_nav_top_arrow").width();
						new_left = ((fw-aw)/2);
						new_left = right_f > right_b? new_left + (right_f-right_b):new_left;
						$("#center_nav_top_arrow")[0].style.left = new_left+"px";
					}else{
						center_lag_close--;
					}
				}else{
					center_lag_close--;
				}
		  }
		});
}
/*
 * General function to see if one element is a child of another @param parent:
 * HTML Element (object) - Element to check if child is under @param child: HTML
 * Element (object) - Element to check if is under parent
 */
function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}

/*
 * General function to check MouseOut to make sure it is a proper mouse out and
 * not a mouse out onto a child of the element in RESULTS: helps avoid
 * unexpected mouseout calls Calls JS_function passed to if mouseout is proper
 * @param element: HTML Element (object) - Element firing mouseout event (this)
 * @param event: JS Event (object) - Event fired on mouseout (event) @param
 * JS_function: JS Callback function (function) - I.E. if the function is
 * myFunction() send myFunction NOT "myFunction" - not a string @param
 * js_params: JS Object (object) - object to pass to callback function - you can
 * leave undefined
 */
function safeOnMouseOut(element, event, JS_function, js_params) {
	var current_mouse_target = null;
	if( event.toElement ) {				
		current_mouse_target 			 = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target 			 = event.relatedTarget;
	}
	if(typeof js_params == 'undefinded') {
		js_params = {};
	}
	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
		JS_function(js_params);
	}
}

/*
 * General function to check MouseOut to make sure it is not transitioning to
 * element that you would not like to cause the mouseout event RESULTS: helps
 * prevent mouseout event fire on transition to allowed element Calls
 * JS_function passed to if mouseout is proper @param target_element: HTML
 * Element (object) - Element firing mouseout event (this) @param event: JS
 * Event (object) - Event fired on mouseout (event) @param JS_function: JS
 * Callback function (function) - I.E. if the function is myFunction() send
 * myFunction NOT "myFunction" - not a string
 */
function safeOnMouseTransition(target_element, event, JS_function){
	var current_mouse_target = null;
	if( event.toElement ) {				
		current_mouse_target 			 = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target 			 = event.relatedTarget;
	}
	
	if(current_mouse_target != target_element && !is_child_of(target_element,current_mouse_target)){
		JS_function();
	}
}

/*
 * Specific funciton to close main navigation
 */
function closeNav(){
	if($('#nav_floater_holder')[0].style.display == 'none')
		lag_close++;
	else
		$('#nav_floater_holder').hide();
}

function closeCenterNav(){
	if($('#center_nav_floater_holder')[0].style.display == 'none')
		center_lag_close++;
	else
		$('#center_nav_floater_holder').hide();
}

/*
 * General Funciton to display shadowbox in center of page fixed after pulling
 * content via AJAX param url: URL (string) - AJAX url to call for data param
 * json: jQuery data string (string) - Needs to be formatted as a post i.e.
 * example=stuff,example2=more stuff
 */
function shadowBox(url,json){
	var sb = $("#shadow_box_holder");
	var con = $("#shadow_box_mid");
	var fs = $("#full_shadow");
	h = document.body.parentNode.scrollHeight; 
	w = document.body.parentNode.scrollWidth;
	var wh = getWHofWindow();
	fs[0].style.height = h+"px";
	fs[0].style.width = w+"px";
	$.ajax({
		  type: "POST",
		  url: url,
		  data:json,
		  dataType: 'html',
		  success: function(data){
				if(data != null){
					con[0].innerHTML = data;
					sb[0].style.top = "120px";
					sb[0].style.left = ((w-$(sb).width())/2)+"px";
					fs.fadeIn(1000);
					sb.fadeIn(1000);
				}
		  }
		});
}

/*
 * Specifc function to close shadowbox
 */
function closeShadowBox(){
	var sb = $("#shadow_box_holder");
	var fs = $("#full_shadow");
	fs.fadeOut(1000);
	sb.fadeOut(1000);
}

/*
 * General function to get the width and height of current window - Cross
 * Browser
 */
function getWHofWindow(){
	return {width:$(document.body).width()!=null?$(document.body).width(): $(window).width(),height:$(document.body).height()!=null?$(document.body).height(): $(window).height()};
}



function displayShadow(){
	$("#full_shadow")[0].style.height = $(document.body)[0].scrollHeight + "px";
	$("#full_shadow")[0].style.display = "block";
}

function customAni(e){
	this.nfinterval = 0;
	this.e = e; 
	this.stop = function(){
		clearInterval(this.nfinterval);
	}
	this.nudgeFloat = function(l,t,s){
		var m = 50;
		var n = this.e.style.backgroundPosition.split("px");
		var ml =  l - parseInt(n[0]);
		var mt = t - parseInt(n[1]);
		var c = s/m;
		var p = Math.ceil(c/3);
		var u = 0;
		var myMoves = new Array();
		var i = 0;
		for(i = c;i >= 0;i--){
			myMoves[i] =  (i*(i+1))/2
			u += myMoves[i];
		}
		var mul = ml/u;
		var mut = mt/u;
		i = c;
		var subthis = this;
		this.nfinterval = setInterval(function(){
			n = subthis.e.style.backgroundPosition.split("px");
			subthis.e.style.backgroundPosition = (parseInt(n[0])+(mul*myMoves[i]))+"px "+ (parseInt(n[1])+(mut*myMoves[i]))+"px";
			i--;
			if(typeof myMoves[i] == 'undefined')
				subthis.stop();
		},m);
	}
}

