/*------------------------------------------
	jQuery:
		DOM ready events
--------------------------------------------*/

// for home2009 START -->
// for home2009 START | from here to line 115, the rest is the same
var newAvid = new  newAvidLib();
$(document).ready(function(){
     if (  $("*").index($('#cpt_tabs #news-slide ')[0]) != -1 ) {  newAvid.home(); }
});
function newAvidLib()  {
    this.home =  function homeFn() {
	//home2009	
	$('#cpt_tabs #news-slide a.more').hover(
			function(){
				$(this).find('img').css('top','-11px')
				},
			function(){
				$(this).find('img').css('top','0px')
				}
		);
		/* --------- */	
		/* home news-slide */
		var newsDelay = 5000;
		var newsCount = 0;
		var newsLenght = $('#news-slide ul li').length;
		var newsContainer = $('#news-slide ul li.current');
		var newsInitVal = newsContainer.html();
		
		function doNextNews(){
			newsContainer.fadeOut(250, function(){
				newsCount++;
				if(newsCount == newsLenght){
					newsCount = 0;
						newsContainer.html(newsInitVal)
					} else {
						newsContainer.html($('#news-slide ul li').eq(newsCount).clone().html())
						}
				newsContainer.fadeIn(350);
			});
		}
		$('#news-slide, #news-slide .more').hover(
			function(){
				clearInterval(newsTimer);
				},
			function(){
				clearInterval(newsTimer);
				doAuto();
				}
		)
		$('#news-slide .more').click(function(){
			clearInterval(newsTimer);
			doNextNews();
			return false;
		})
		var newsTimer = 0;
		function doAuto(){
			newsTimer = setInterval(function(){doNextNews(true);},newsDelay)	
			}
		doAuto();
		
		/* --------- */					   
		/* home tabs */
		
		/* initial height fix for the #tabFooter div */
		var initH = $('#tabFooter').find('div.current').height() + 21;
		$('#tabFooter').animate({height: ''+initH+'px'},250);
		
		/* each tab functions */
		if($.browser.msie){
			//alert($.browser.version);
			var time4fade = 0;
		}else{
			var time4fade = 'slow';
			}
		$('#tabList ul li').each(function(e){
			$(this).hover(
				function(){
					if( $(this).attr('class')!='current' ){
						$('#tabList ul li.current').removeClass('current');
						$(this).addClass('current');
	
						var pos = $(this).position().left;
						pos = pos +0;
						$(this).css('cursor','pointer');
						
						$('#tabFooter').find('div.current').fadeOut(time4fade);
						//$('#tabFooter').find('div.current').animate({opacity:0},250);
						
						$('#cpt_tabs #tabList').stop().animate({backgroundPosition: pos+'px 0'},350, function(){
							$('#tabFooter').find('div.current').removeClass('current');
							
							$('#tabFooter').find('div').eq(e).addClass('current').fadeIn(time4fade);
							//$('#tabFooter').find('div').eq(e).addClass('current').animate({opacity:1},250);
							
							//var newH = $('#tabFooter').find('div').eq(e).height() + 21;
							//$('#tabFooter').animate({height: ''+newH+'px'},250);
							})
						
					}else{
						
						};
					
					},
				function(){
					
					}
				)
		})
	//home2009
	}
}
// for home2009 only END
// for home2009 only END -->

$(document).ready(function() {
	// apply overlabel to search form
	//$("#search label").labelOver('over');

	// open external links in new windows
	// - requires class="externalLink"
	$("a.externalLink").click(function() {
		window.open(this.href);
		return false;
	})

	// add zebra striping table row class
	$("table.zebra tbody tr:odd").addClass("stripe");

	// fix background image flickr on links in IE6
	try { document.execCommand("BackgroundImageCache", false, true); }
	catch(err) {}

	// loop through news items on home page
	/*
	$('.scroll').cycle({ 
	    fx:     'fade', 
	    speed:  'fast', 
	    timeout: 0, 
	    next:   '.next', 
	    prev:   '.prev' 
	});
	*/
	// CHROME SELECT LISTS 
	/*
	$('#country').selectbox();
	$('#products').selectbox();
	*/

	// BRAND SHOWCASE
	var menuSpeed = 200;
	var pointerSpeed = 100;
	// user clicks close button
	$("#brandShowcase .pod a.close").click(function() {
		var curWin = $(this).attr('class');
		var pointer = $(this).attr('rel');
		$("#brandShowcase .menu ul li a").removeClass('active');
		$('#brandShowcase #pointer').animate({left:"-24px"},pointerSpeed,function() {
			$('#brandShowcase .pod').animate({top:"183px"}, menuSpeed, function() {
				$('#brandShowcase #pos0').animate({top:"0"}, menuSpeed);
			});
		});
		return false;
	});
	// user clicks menu item
	$("#brandShowcase .menu li a").click(function() {
		$("#brandShowcase .menu ul li a").removeClass('active');
		var curWin = $(this).attr('class');
		var pointer = $(this).attr('rel');
		if ($("#brandShowcase #"+curWin).css("top") == null) {
			alert(curWin);
			$('#brandShowcase .pod').animate({top:"183px"}, menuSpeed, function() {
				$('#brandShowcase #pointer').animate({left:"-24px"},pointerSpeed, function() {						
					$('#brandShowcase #pos0').animate({top:"0"}, menuSpeed);
				});
			});				
		} else {
			$('#brandShowcase #pointer').animate({left:pointer},pointerSpeed,function() {
				$("#brandShowcase .menu ul li a."+curWin).addClass('active');
				$('#brandShowcase .pod').animate({top:"183px"}, menuSpeed, function() {
					$('#brandShowcase #'+curWin).animate({top:"0"}, menuSpeed);
				});
			});
		}
		return false;
	});

	
});

/*------------------------------------------
	jQuery:
		Window load Events
--------------------------------------------*/
$(window).load(function() {});


/*------------------------------------------
	jQuery Plugins
--------------------------------------------*/
//	Label Over by Remy Sharp : Apply label over input
jQuery.fn.labelOver = function(overClass) {
	return this.each(function() {
		var label = jQuery(this);
		var f = label.attr('for');
		if (f) {
			var input = jQuery('#' + f);
			this.hide = function() {
				label.css({ textIndent: -10000 })
			}
			this.show = function() {
				if (input.val() == '') label.css({ textIndent: 0 })
			}
			// handlers
			input.focus(this.hide);
			input.blur(this.show);
			label.addClass(overClass).click(function(){ input.focus() });
			if (input.val() != '') this.hide(); 
		}
	});
}

//	vJustify by Michael Futreal: Justify Element Heights
jQuery.fn.vjustify=function() {
	var maxHeight=0;
	this.each(function() {
		if (this.offsetHeight>maxHeight) { maxHeight=this.offsetHeight; }
	});
	this.each(function() {
		$(this).height(maxHeight + "px");
		if (this.offsetHeight>maxHeight) {
			$(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
		}
    });
};



/***************** RKK Funcitons *************************/

function ParaOver(tdObj) {
    var i = 1, tempObj;
    for (i = 1; i <= 5; i++) {
        tempObj = "service" + i; ;
        document.getElementById(tempObj).className = "service";
        document.getElementById("text" + tempObj).display = "hidden";
        //alert(eval(tempObj));
        //eval(tempObj).className = "service";
        //eval("service" + i).className = "service";
    }
    tdObj.className = "service_active";


    /* Fade Effect */
    var TimeToFade = 1000.0;

    var element = document.getElementById("text" + tdObj);
    if (element == null)
        return;

    if (element.FadeState == null) {
        if (element.style.opacity == null
            || element.style.opacity == ''
            || element.style.opacity == '1') {
            element.FadeState = 2;
        }
        else {
            element.FadeState = -2;
        }
    }

    if (element.FadeState == 1 || element.FadeState == -1) {
        element.FadeState = element.FadeState == 1 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
    }
    else {
        element.FadeState = element.FadeState == 2 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade;
        setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
    }
    
}

function animateFade(lastTick, eid) {
    var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;

    var element = document.getElementById(eid);

    if (element.FadeTimeLeft <= elapsedTicks) {
        element.style.opacity = element.FadeState == 1 ? '1' : '0';
        element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
        element.FadeState = element.FadeState == 1 ? 2 : -2;
        return;
    }

    element.FadeTimeLeft -= elapsedTicks;
    var newOpVal = element.FadeTimeLeft / TimeToFade;
    if (element.FadeState == 1)
        newOpVal = 1 - newOpVal;

    element.style.opacity = newOpVal;
    element.style.filter = 'alpha(opacity = ' + (newOpVal * 100) + ')';

    setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

/*** Functions by RKK ***/

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}
