$(document).ready(function(){
	
	/* PNGFIX start *******************************************************************/
		$(document).pngFix();
	/* PNGFIX end *********************************************************************/
	
	/* EXTERNAL LINKS start ***********************************************************/
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});	
	/* EXTERNAL LINKS end *************************************************************/
	
	/* NAVIGATION start ***************************************************************/
	$('#nav ul ul').hide();
	//hover show next level
	$('#nav ul > li').hover(function() { $('ul:first', this).show(); },function() {$('ul:first', this).hide();});
	//add class for flyouts
	if ($('#nav ul ul ul').length > 0) { $('#nav ul ul ul ').parent().addClass('flyout'); }
	$('#nav-mid li:last').addClass('last');
	/* NAVIGATION end ****************************************************************/
	
	/* SIFR***************************************************************************/
	$('h1.title').flash(
		{ src: '/includes/media/font-helveticaNeue-lt-45-light.swf' },
		 { version: 8 },
			function(htmlOptions) {
				htmlOptions.flashvars.css = '*%20%7B%20color%3A%23'+RGBstring($(this).css('color'))+'%7D';
				htmlOptions.flashvars.txt = this.innerHTML;
				this.innerHTML = '<div>'+this.innerHTML+'</div>';
				var $alt = $(this.firstChild);
				htmlOptions.width = $(this).width(); //$alt.width()
				htmlOptions.height = $(this).height(); //$alt.height()
				$alt.addClass('sifr-alt');
				$(this).addClass('sifr-text-alt').prepend($.fn.flash.transform(htmlOptions));
			}
		);
	/* SIFR**************************************************************************/

	/* COMMERCE:img click replace****************************************************/	
	$('.prod-sub-img').click(function() {	$('.prod-image').html($(this).html());	 return false	});
	$('#details div.prod-image .thumb').click(
		function() {
			var img_src = $('img',this).attr('src');
			var img_src_lg = img_src.replace('thm_','');
			$('#main-image img').attr({'src':img_src_lg,'width':180}).removeAttr('height');
			return false
		}
	);
	
	
	
	$('#content .commerce > div').addClass('cms').parent().prepend('<span style="font-size:0;">&nbsp;</span>'); //fix for commerce ...good ol jquery
	/*COMMERCE:img click replace****************************************************/
	
	$('#main .right').css('height',$('#main').height());
	
	/* START Catalog Resource Page */
	$('#page-catalogs .download-category ul').hide();
	$('#page-catalogs .download-category .handle').click(function(){
		var dropdown_shell = $($(this).attr('rel'));
		var dropdown_handle = $(this);
		var dropdown = $($(this).attr('rel')+' ul');
		
		if(dropdown_handle.hasClass('active')){
			dropdown_handle.removeClass('active');
			dropdown.hide();
		}else{
			dropdown_handle.addClass('active');
			dropdown.show().css({ 'visibility':'visible'});
			dropdown_shell.bind("mouseleave",function(){ 
			   dropdown.hide();
			   dropdown_handle.removeClass('active');
			})
		}
	});
	/* END Catalog Resource Page */

	/* CAROUSEL ITEMS */
	 if($("#ad-rotator .carousel").length >0){
    	$("#ad-rotator .carousel .display").jCarouselLite({ btnNext: ".down", btnPrev: ".up", vertical:true,mouseWheel: true,speed: 500,circular: true,visible: 3,scroll: 1
		});
	}
	/* CAROUSEL ITEMS */
	
	/*TRAINING CENTER*/
	$('#training-portal-intro .read-more').click(function(){
		$('#training-portal-intro .more').slideDown();
		$(this).hide();
	});	
	/*pdf/commerce popup*/
	$('#related-materials a,#related-items a,.help a').click(function(){	
		var mywindow = window.open ( $(this).attr('href'), "mywindow","location=1,status=0,scrollbars=1,width=900,height=600");
		return false;
	});	
	
	/*video player*/
	$('#bite-fx a.vid,a.botr-video').click(function(){
		//var url = $(this).attr('src').match(/([\.a-z\/]+)\/([a-z0-9A-Z]+)-/);	var key= url[2];
		var key = $(this).attr('rel');
		if(key !='' && typeof(key) != 'undefined' ){
			var html_build = '<iframe src="/includes/_resource_center_video.cfm?video='+key+'"></iframe>';
		}else{
			var $tr = $(this).parent().parent();
			var html_build = $('.video',$tr).html();
		}
		$('body').append('<div id="popup-player" class="popup"><span class="close"></span><div class="d">'+html_build+'</div></div><div id="popup-bg"></div>');
		//define
		$display = $("#popup-player");
		$popup_bg = $("#popup-bg");
		//center/load
		centerPopup($display,$popup_bg);
		loadPopup($display,$popup_bg);
		//close
		$popup_bg.add('.close',$display).click(function(){	disablePopup($display,$popup_bg); });//Press Escape event!
		$(document).keypress(function(e){ if(e.keyCode==27 && popupStatus==1){disablePopup($display,$popup_bg);} });
		return false;
	});
	
	/* SLIDESHOW start ***************************************************************/
	var slideshow = setInterval( "slideSwitch('#ad-rotator .ad div',500,'Next')", 5000 );	
	
}); //close document.ready


/* SLIDESHOW function start *********************************************************/
function slideSwitch(selector,opacity_speed,direction) {
	var $active = $(selector+'.active');

	if ( $active.length == 0 ) $active = $(selector+':first');	
	
	if(direction == 'Next' || direction == null){ var $next = $active.next().length ? $active.next() : $(selector+':first'); }
	else if(direction == 'Previous') { var $next = $active.prev().length ? $active.prev() : $(selector+':last'); }	

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, opacity_speed, function() {
			 $active.removeClass('active last-active');
	});
}
/* SLIDESHOW function end ***********************************************************/

/* sIFR RGB conversion function start **********************************************/
function RGBstring(str) {
	str1 = str.replace("#","");
	str2 = str1.replace("rgb(","");
	str2 = str2.replace(")","");
	str2 = str2.split(",");
	if(navigator.appName == "Microsoft Internet Explorer"){ return str1; }
	else { return toHex(str2[0])+toHex(str2[1])+toHex(str2[2]); }
}
function toHex(N) {
 if (N==null) return "01";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}
/* sIFR RGB conversion function end *************************************************/

var popupStatus = 0;
function loadPopup($display,$popup_bg){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $popup_bg.css({"opacity": "0.7"});
        $popup_bg.fadeIn("slow");
        $display.show();
        popupStatus = 1;
    }
}
function disablePopup($display,$popup_bg){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $display.fadeOut("slow").remove();
        $popup_bg.fadeOut("slow").remove();
        popupStatus = 0;
    }
}
//centering popup
function centerPopup($display,$popup_bg){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $display.height();
    var popupWidth = $display.width();
    //centering
    $display.css({"position": "absolute","left": windowWidth/2-popupWidth/2,"top": getYX('Y')+100}); //,"top":100
    //only need force for IE6
    $popup_bg.css({"height": windowHeight});
}
eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('k g(a){f b=0,3=0;4(i(9.c)==\'e\'){3=9.c;b=9.h}8 4(1.5&&(1.5.6||1.5.7)){3=1.5.7;b=1.5.6}8 4(1.2&&(1.2.6||1.2.7)){3=1.2.7;b=1.2.6}4(a=="j"){d b}8 4(a=="l"){d 3}}',22,22,'|document|documentElement|scrOfY|if|body|scrollLeft|scrollTop|else|window|||pageYOffset|return|number|var|getYX|pageXOffset|typeof|X|function|Y'.split('|'),0,{}))
