var arrowHomeTop = 0;
var arrowHomeLeft = 0;
var hideTime, activeSecondary;
var ddTriggerArray = ["ulNav13", "ulNav14", "ulNav15", "ulNav17", "ulNav18", "ulNav83", "ulNav84", "ulNav85"];
var ddLayerArray = ["applicationsDropDown", "methodsDropDown", "brandsDropDown", "industriesDropDown", "productsDropDown", "paApplicationsDropDown", "paIndustriesDropDown", "paBrandsDropDown"];
var timeOuts= new Array();
var mytabs;

sharepage = function() {
	temploc='http://pac.brandextract.com/staged/About_PAC/Share_This_Page/?myURL=';
	temploc = temploc + window.location.href.replace(/\&/, '%26').replace(/\?/, '%3F');
	window.location.href = temploc;
}

var lastDiv = "";
var showLocation = function(div) {
	if(lastDiv != "") 
		jQuery("#"+lastDiv).hide();
	jQuery("#"+div).fadeIn();
	jQuery('html, body').animate({
			scrollTop: jQuery("#"+div).offset().top
		}, 500, 'swing');							
	lastDiv = div;
}

$.extend({
  getUrlVars: function(){
    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;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

$(function() { 
	
	if (!Array.prototype.indexOf) {
		Array.prototype.indexOf = function(obj, start) {
 			for (var i = (start || 0), j = this.length; i < j; i++) {
        		if (this[i] == obj) { return i; }
    		}
    		return -1;
		}
	}
	
	jQuery.fn.defaultInput = function(params) { 
		if(jQuery(this).val() == '') jQuery(this).val(params.defaultText);
		this.each(function() {
			jQuery(this).bind("focus", function(e){if(jQuery(this).val() == params.defaultText) jQuery(this).val('');});
			jQuery(this).bind("blur", function(e){if(jQuery(this).val() == '') jQuery(this).val(params.defaultText);});
		});
	};
	
	jQuery.fn.pacNav = function(params) { 
		this.each(function() {		
			var arrowTop = jQuery("#secondaryNav").offset().top+31;
			var arrowLeft = jQuery(this).offset().left + (jQuery(this).width()/2)+8;
			if(jQuery(this).hasClass("active")) {
				jQuery("#navArrow").stop().css( { "top": arrowTop, "left": arrowLeft }).show();
				arrowHomeTop = arrowTop;
				arrowHomeLeft = arrowLeft;
			}
			
			if (ddTriggerArray.indexOf(jQuery(this).attr("id")) != -1) {
				jQuery(this).children().css( {'cursor': 'default' });
			}
			
			
			jQuery(this).bind("mouseover", function() { 
				switch(params.type) {
					case 'primary':
						jQuery(this).attr("src",params.on); 
  						break;
					case 'dropdown':
						stopTimeOut();
						break;
					default:
  						stopTimeOut();
						if(jQuery("#navArrow:visible").length == 0) {
								jQuery("#navArrow").css( { top: arrowTop, left: arrowLeft }).show();
							} else {
								jQuery("#navArrow").stop().animate({ "top": arrowTop, "left": arrowLeft }, 100, "linear"); 
						}
						if (ddTriggerArray.indexOf(jQuery(this).attr("id")) != -1) {
							if (activeSecondary != ddLayerArray[ddTriggerArray.indexOf(jQuery(this).attr("id"))]) {
								jQuery("#"+activeSecondary).fadeOut("fast");
							}
							activeSecondary = ddLayerArray[ddTriggerArray.indexOf(jQuery(this).attr("id"))];
							jQuery("#"+ddLayerArray[ddTriggerArray.indexOf(jQuery(this).attr("id"))]).fadeIn("fast");
						}
				}
			 });
			jQuery(this).bind("mouseout", function() { 
				switch(params.type) {
					case 'primary':
						jQuery(this).attr("src",params.off); 
						break;
					default:
						if(arrowHomeTop == 0) {
							timeOuts["mouseOut"] = setTimeout("secondaryOff()",500);
						} else {
							timeOuts["mouseOut"] = setTimeout("secondaryOff()",500);
							
						}
				}
			});
		});
	};
	
	stopTimeOut = function() {
		for(key in timeOuts ){
    		clearTimeout(timeOuts[key]);
  		}
	}
	
	secondaryOff = function() {
		if (arrowHomeTop == 0) {
			jQuery("#navArrow").stop().fadeOut("Fast");
		} else {
			jQuery("#navArrow").stop().animate({ "top":  arrowHomeTop, "left": arrowHomeLeft }, 100, "linear");
		}
		jQuery(".dropDownContainer").stop().fadeOut("Fast");
		jQuery("#"+activeSecondary).fadeOut("fast");
		
	}
	
	activateRollovers = function() {
		// This nonsense is here because sometimes browsers try and run this bit before elements have their offset top and left defined
		if (jQuery("#secondaryNav>ul>li:eq(0)").offset().left != jQuery("#secondaryNav>ul>li:eq(1)").offset().left) {
			jQuery("#primaryLab").pacNav({ off: '/images/lab_instruments.png', on: '/images/lab_instruments_over.png', type: 'primary' });
			jQuery("#primaryProcess").pacNav({ off: '/images/process_analytics.png', on: '/images/process_analytics_over.png', type: 'primary' });
			jQuery("#primaryParts").pacNav({ off: '/images/parts_store.png', on: '/images/parts_store_over.png', type: 'primary' });
			jQuery("#primarySupport").pacNav({ off: '/images/customer_support.png', on: '/images/customer_support_over.png', type: 'primary' });
			jQuery("#primaryNews").pacNav({ off: '/images/news_and_events.png', on: '/images/news_and_events_over.png', type: 'primary' });
			jQuery("#primaryAbout").pacNav({ off: '/images/about_pac.png', on: '/images/about_pac_over.png', type: 'primary' });
			jQuery("#secondaryNav>ul>li").pacNav({type: 'secondary' });
			jQuery(".dropDownContainer").pacNav({type: 'dropdown' });
			jQuery("#zoomableImage").each( function() {
				myLeft = jQuery(this).offset().left + (jQuery(this).width());
				myTop = jQuery(this).offset().top + (jQuery(this).height());
				jQuery("#lightboxArrow").offset({ top: myTop, left: myLeft });
			});
		} else {
			setTimeout("activateRollovers()", 100);
		}
	};
	
	activateRollovers();
	mytabs = jQuery("#tabbedContent > div.tabs").tabs("#tabbedContent > div.panes > div");
	jQuery("#footerSlideshow > div.tabs").tabs("#footerSlideshow > div.panes > div", { rotate: true}).slideshow({autoplay:true, autopause:true });
	jQuery(".dropDownParent").tabs(".dropDownChild > div", {initialIndex: 1000, event:'mouseover', onBeforeClick:function() { stopTimeOut() }});
	jQuery(".timeOutKiller").bind("mouseover", function() {
		stopTimeOut();
	});
	jQuery(".accordion").tabs(".accordion div.pane", {tabs: 'h2', effect: 'slide', initialIndex: -1});
	jQuery(".courseAccordion div.tab").bind('click', function(){ 
        if (jQuery(this).hasClass("current")) {
        	jQuery(this).next().hide(); 
			jQuery(this).removeClass("current");
		}
    });
	jQuery(".courseAccordion").tabs(".courseAccordion div.pane", {tabs: 'div.tab', effect: 'slide'});
	jQuery(".backButton").bind("click", function() {
		history.go(-1);
	});
	jQuery("#k").defaultInput({ defaultText: "Search this site..."});
	jQuery("#txtFirst").defaultInput({ defaultText: "First Name"});
	jQuery("#txtLast").defaultInput({ defaultText: "Last Name"});
	jQuery("#txtEmail").defaultInput({ defaultText: "Email"});
	jQuery("#moreTestimonies").bind("click", function() {
		jQuery("#Tab4").trigger("click");
	});
	jQuery("#txtRetypeEmail").defaultInput({ defaultText: "Retype Email"});
	jQuery(".toolTipTrigger").tooltip({position:'center left', offset:[0, -5]});
	/*jQuery(window).bind("resize", function () {
		window.location.href = window.location.href;
	});*/
	jQuery("img[rel]").overlay({expose: { 
        color: '#000', 
        loadSpeed: 200, 
        opacity: 0.7 
    }});
	
	jQuery("#scrollToCat").bind("click", function() {
		jQuery('html, body').animate({
			scrollTop: $(".accordion").offset().top
		}, 500, 'swing');											  
	});
	
	jQuery("#newslettersignup").validate({
		rules: {txtFirst: {required: true},txtLast: {required: true},txtEmail: {required: true,email: true},txtRetypeEmail: {required: true,email: true}},
		messages: {txtFirst: {required: ''},txtLast: {required: ''},txtEmail: {required: '', email: ''},txtRetypeEmail: {required: '', email: ''}}
	});
	
	jQuery("#sharePage").validate({
		rules: {sender: {required: true},senderemail: {required: true,email: true},collName: {required: true},colleagueemail: {required: true,email: true}},
		messages: {sender: {required: ''},senderemail: {required: '',email: ''},collName: {required: ''},colleagueemail: {required: '',email: ''}}
	});
	
	jQuery("#requestQuote").validate({
		rules: {name: {required: true},email: {required: true,email: true},retypeEmail: {required: true,email: true, equalTo: '#email'},phone: {required: true},company: {required: true},title: {required: true},model: {required: true}},
		messages: {name: {required: ''},email: {required: '',email: ''},retypeEmail: {required: '',email: ''},phone: {required: ''},company: {required: ''},title: {required: ''},model: {required: ''}}
	});
	
	jQuery("#requestService").validate({
		rules: {name: {required: true},email: {required: true,email: true},retypeEmail: {required: true,email: true, equalTo: '#email'},phone: {required: true},company: {required: true},title: {required: true},country: {required: true}},
		messages: {name: {required: ''},email: {required: '',email: ''},retypeEmail: {required: '',email: ''},phone: {required: ''},company: {required: ''},title: {required: ''},model: {required: ''},country: {required: ''}}
	});
	
});



/*
* Tadas Juozapaitis ( kasp3rito@gmail.com )
*/

(function($){
$.fn.vTicker = function(options) {
	var defaults = {
		speed: 700,
		pause: 4000,
		showItems: 2,
		animation: '',
		mousePause: true
	};

	var options = $.extend(defaults, options);

	moveUp = function(obj, height){
		obj = obj.children('ul');
    	first = obj.children('li:first').clone(true);
		
    	obj.animate({top: '-=' + height + 'px'}, options.speed, function() {
        	$(this).children('li:first').remove();
        	$(this).css('top', '0px');
        });
		
		if(options.animation == 'fade')
		{
			obj.children('li:first').fadeOut(options.speed);
			obj.children('li:last').hide().fadeIn(options.speed);
		}

    	first.appendTo(obj);
	};
	
	return this.each(function() {
		obj = $(this);
		maxHeight = 0;

		obj.css({overflow: 'hidden', position: 'relative'})
			.children('ul').css({position: 'absolute', margin: 0, padding: 0})
			.children('li').css({margin: 0, padding: 0});

		obj.children('ul').children('li').each(function(){
			if($(this).height() > maxHeight)
			{
				maxHeight = $(this).height();
			}
		});

		obj.children('ul').children('li').each(function(){
			$(this).height(maxHeight);
		});

		obj.height(maxHeight * options.showItems);
		
    	interval = setInterval('moveUp(obj, maxHeight)', options.pause);
		
		if(options.mousePause)
		{
			obj.bind("mouseenter",function(){
				clearInterval(interval);
			}).bind("mouseleave",function(){
				interval = setInterval('moveUp(obj, maxHeight)', options.pause);
			});
		}
	});
};
})(jQuery);
