// JavaScript Document

 	$j = jQuery.noConflict();
// Use jQuery via jQuery(...)
	 
$j(document).ready( function () {
	
		//header animaiton					  
	/*$j('#mediaBar').cycle({
		fx: 'scrollHorz',
		timeout: 0,
		prev: '#mediaBefore',
		next: '#mediaAfter'
	});*/	
	
	if(checkBrowser() == true){
		$j('#mediaBar').empty();
		$j('#mediaBefore').empty();
		$j('#mediaAfter').empty();
		$j('#mediaBar').append('<div class="replace"><a href="http://www.shopwps.com/" target=""><img src="'+URL+'/images/marquee.png"/></a></div>');
		
	}
	
	css_browser_selector(navigator.userAgent);
	
	

	
	//excerpt and content switch
	$j(".fullContent").hide();
	
	//hide attachment inside excerpt
	$j(".excerpt div[id^='attachment']").hide();
	
	//delete the blogroll class in product page//
	$j('.productWindow .linkcat .xoxo').removeClass('blogroll');
	
	//hide the title of bookmarks
	$j('.linkcat span').remove();
	$j('.blogroll img').after("<br />");
	$j('.blogroll li:last').css('background-image', 'none');

	//when click readmore button and title
	//$j(".readMore").click(function(e){ entryExCon($j(this)); });
	$j(".readMore").click(function(){ entryExCon($j(this)); });
	$j(".moreClip").click(function(){ entryExCon($j(this)); });
	$j(".morePic").click(function(){ entryExCon($j(this)); });
	
	//$j(".titleLink").click(function(){ entryExCon($j(this)); });
	
	
	$j('#wrapper').mousemove(function(e){			
		var sideHeight = $j(".sideContent").height();
		$j(".sidebarBorder").height(sideHeight);	
		return true;
	});
	
	
	/*feature jscrousel activate */
	$j(".mediaBar").jCarouselLite({
	    visible: 1,
	    speed: 400,
	    btnGo:[".mediaMenuBar .0", ".mediaMenuBar .1", ".mediaMenuBar .2"]
	    
	});

	$j('.mediaMenuBar li').click(function(){
		$j('.mediaMenuBar li').removeClass('clicked');
		$j(this).addClass('clicked');
	});
	
	/*$j('.videoLink').fancybox({
		'frameWidth':973,
		'frameHeight':575,
		'hideOnOverlayClick' : false,
		'hideOnContentClick' : false,
		'autoScale' : true
	});*/
	

	
	/* comments */
	$j(".commentText p:first-child").prepend('&#8220;&nbsp;');
	$j(".commentText p:last-child").append('&nbsp;&#8221;');
	$j(".commentText p:first-child").css('text-indent', '-12px');
	
	/*header functions*/
	
	/*language Selector */
	$j('#lang li').hover(
			function() {
				$j(this).children('ul').css({'visibility':'visible'}).animate({
					height: "500px"
				}, 300);
			},
			function() {
				$j(this).children('ul').css("visibility", "hidden").animate({
					height: "0px"
				}, 100);
			}
		);
	
	/*Adding hover class to li*/
	$j('#puma_header #nav li:not(.main)').hover(
			function() {
				$j(this).children('a').css('color' ,'#c4a484');
			},
			function() {
				$j(this).children('a').css('color' ,'#000');
			}
		);
	
	$j('#puma_header #nav li ul li').hover(
			function() {
				$j(this).children('a').css('color' ,'#c4a484');
			},
			function() {
				$j(this).children('a').css('color' ,'#bbb');
			}
		);
	
	$j('#lang li ul').hover(
			function() {
				$j('#langLiA').addClass("hover");
				$j('#langLiA span').css('color','#fff');
			},
			function() {
				$j('#langLiA').removeClass("hover");
				$j('#langLiA span').css('color','#000');
			}				
							
	);
	
	$j('#langLiA').hover(
			function() {
				$j('#langLiA').addClass("hover");
				$j('#langLiA span').css('color','#fff');
			},
			function() {
				$j('#langLiA').removeClass("hover");
				$j('#langLiA span').css('color','#000');
			}				
							
	);
	
	
	$j('.sport a').hover(
			function() {
				$j('#sportA').addClass("hover");
				
			},
			function() {
				$j('#sportA').removeClass("hover");
			}				
							
	);
	
	$j('.main').hover(
			function() {
				$j(this).addClass('mainHover');
			},
			function() {
				$j(this).removeClass('mainHover');
			}										
	);
	
	
	$j('#puma_header #nav .nodrop a').hover(function(){	
			$j(this).addClass('hover_nodrop');
		},
		function(){
			$j(this).removeClass('hover_nodrop');
	});
	
	
	$j("#puma_header ul").each(
			function() { 
				$j(this).find("li:last").addClass('last');
			}
	);
	
	$j('.productWindow .linkcat .li a img').after('<br class="clearfloat" />');
	
	/* mailing list */
	
	/*$j("#puma_main_subscribe").submit(
			function() {	
				if (this.email.value.split("@").length > 1) {
						email = escape(this.email.value);
					} else {
						email = ""	
					}
				dest = 'http://test.puma.com/test/resources/mailing_list.jsp?css=' + escape(this.css.value) + '&site=' + this.site.value + '&email=' + email + "&TB_iframe=true&height=500&width=450";
				tb_show('Post preview', dest, false);
				return false;
			}
		);*/
	
	/* nav li animaiton */
	$j('#nav li').hover(
			function() {
				$j(this).children('ul').css({'visibility': 'visible'}).animate({
					height: "500px"
				}, 500);
			},
			function() {
				$j(this).children('ul').css("visibility", "hidden").animate({
					height: "0px"
				}, 100);
			}
		);
	
	/* remove the blogroll class of products page item */
	
	
	/*product list*/
	
	$j('.product_list li').click(
			function() {
				theLink = $j(this).find("a");
				if (theLink.attr("class") == "thickbox") {
					tb_show("Product Modal",theLink.attr("href"),false);
				} else {
					document.location = theLink.attr("href");
				};
			}
		);
	
	$j('a[rel="popout"]').each(function(){
			if(this.href != null && $j(this).attr('rel') == 'popout' ){ this.target = '_blank';  }
	});
	
	$j('#puma_main_subscribe_email, #s').focus(function(){ puma_forms.manage_default_input(this) });
	
	if($j('#catalog_header')){
			$j('#catalog_header div.foo:first').hover(function(){
				$j(this).children('div').children('.NFSelectRight').css({ 'background-position' : '100% -56px', 'color' : '#fff' })
				$j(this).children('div').children('.NFSelectLeft').css({ 'background-position' : '100% -56px' })
			},function(){
				$j(this).children('div').children('.NFSelectRight').css({ 'background-position' : '100% 0', 'color' : '#666' })
				$j(this).children('div').children('.NFSelectLeft').css({ 'background-position' : '100% 0' })
			});
		}


	
	/* manage default input */
	
	var sideHeight = $j('.sideContent').height();
	$j(".sidebarBorder").height(sideHeight);
	
	checkPics();
	//checkSideHeight();
	featureListPosition();
	checkGalleryExcerpt();
	checkVideoExcerpt();
	checkAllContentImg();
	
	checkFeature();
	//checkEntryHeight();
	
	
	//paginate products tiems
	var currentPage = 0;
	var itemsEachPage = 16;
	seeMoreorLess(itemsEachPage, currentPage);
	featurePaginate(itemsEachPage, currentPage);
	
	  //when clicking see more button
	$j('.product-next').click(function(){
		currentPage++;
		seeMoreorLess(itemsEachPage, currentPage);
		featurePaginate(itemsEachPage, currentPage);
	});
	
	 $j("a#fancyFooter1").fancybox({ 
			'frameWidth':450,
			'frameHeight':400,
			'overlayShow': true,
			'hideOnContentClick': false 
	});
	 
	 $j("a.fancybox2").fancybox({ 
			'frameWidth':575,
			'frameHeight':550,
			'overlayShow': true,
			'hideOnContentClick': false 
	});
	 
	 $j("a.puma_main_subscribe_submit").fancybox({
			'frameWidth':450,
			'frameHeight':500,
			'overlayShow': true,
			'hideOnContentClick': false 						  
	});


});


//expand and close entry
function entryExCon(entry){
		
		var contentDiv = entry.parent().parent().parent().children(".entry-content").children(".fullContent");							   
	    var excerptDiv = entry.parent().parent().parent().children(".entry-content").children(".excerpt");
		var readmoreBut;
		
		var className = entry.parent().parent().parent().children(".entry-content").children(".entry-meta").children('div:first').attr('class');
	
		if(className == 'readMore'){	
			readmoreBut = entry.parent().parent().parent().children(".entry-content").children(".entry-meta").children(".readMore").children(".readlink");
		}else if( className == 'moreClip' ){
			readmoreBut = entry.parent().parent().parent().children(".entry-content").children(".entry-meta").children(".moreClip").children(".readlink");
		}else if(className == 'morePic'){	
			readmoreBut = entry.parent().parent().parent().children(".entry-content").children(".entry-meta").children(".morePic").children(".readlink");
		}
		
		if(contentDiv.is(":hidden")){
			
			excerptDiv.hide();
			
			readmoreBut.children("img").attr('src', URL+'/images/closethis.jpg');
			contentDiv.show();
			
				//get meta back to position
			contentDiv.parent().children(".entry-meta").css({'float':'left','margin-top':'-3px'});
				//adjust border height
			//checkEntryHeight();
		}else{
			contentDiv.hide();
			
			if(className == 'readMore'){
				readmoreBut.children("img").attr('src', URL+'/images/readmore.png');
			}else if(className == 'moreClip'){
				readmoreBut.children("img").attr('src', URL+'/images/moreClip.jpg');
			}else if(className == 'morePic'){
				readmoreBut.children("img").attr('src', URL+'/images/morePic.jpg');
			}
			
			excerptDiv.show();
				
				//get meta back to position
			if(excerptDiv.children(".excerptImg").length > 0 ){
				if($j.browser.safari){
					excerptDiv.parent().children(".entry-meta").css({'float':'right','margin-top':'-10px'});
				}else{
					excerptDiv.parent().children(".entry-meta").css({'float':'right','margin-top':'-80px'});
				}
			}
				//adjust border height
			//checkEntryHeight();
		}	
		return false; //very important!!!!
		
}

function checkPics(){
	var pic = $j(".excerptImg img");
	
	$j(".excerptImg img").each(function(i){	
		var excerpt = $j(this).parent().parent();
		var ratio = $j(this).attr('width') / 250;
		var h = $j(this).attr('height') / ratio;
		//$j(this).parent().css({'width': '250px', 'height': h, 'margin':'0', 'overflow':'hidden'});
		$j(this).css({'width': '250px', 'height': h, 'margin':'0', 'padding-bottom': '0'});
		//excerpt.css({'display':'block'});
		excerpt.height(h);
		
		var dis = h % 30;
		//alert(dis);
		
		var contentDiv = $j(this).parent().parent().children(".excerptContent");
		
		if($j.browser.safari){
			contentDiv.css({'height':h-10-dis, 'width': '360px', 'overflow':'hidden'});
			excerpt.parent().children(".entry-meta").css({'width':'360px', 'float':'right', 'margin-top':'-10px'});
		}else{
			contentDiv.css({'height':h-80-dis, 'width': '360px', 'overflow':'hidden'});
			excerpt.parent().children(".entry-meta").css({'width':'360px', 'float':'right', 'margin-top':'-80px'});
		}
	});
}

function checkGalleryExcerpt(){
		$j(".galleryExcerptImg img").each(function(i){	
			var excerpt = $j(this).parent().parent();
			
			if($j(this).attr('width') > 650){
				$j(this).css('width', '650px');
				var h = $j(this).attr('height');
				excerpt.height(h);
			}else{
				var h = $j(this).attr('height');
				excerpt.height(h);
			}
		});
}

function checkVideoExcerpt(){
		/*$j(".videoExcerpt p a object").each(function(i){	
			var excerpt = $j(this).parent().parent().parent().parent();
			var h = $j(this).attr('height');
			excerpt.css('height',h+'px');
		});*/
		
		$j(".videoExcerpt p object").each(function(i){
			var excerpt = $j(this).parent().parent().parent();
			var h = $j(this).attr('height');
			if(excerpt.attr('class') == 'videoExcerpt'){
				
				excerpt.parent().css('height',h+'px');
				//alert(excerpt.attr('class'));
			}else if(excerpt.attr('class') == 'excerpt'){
				//alert(excerpt.attr('class'));
				excerpt.css('height',h+'px');
			
			}
			
			
		});
}

function checkFeature(){
	var imgW = $j('.featureProductImage').children('img').height();	
	if(imgW > 350){
		$j('.featureProductImage img').width(350);
	}
}

//calculate entry height to set right border div height
/*function checkEntryHeight(){
	
	$j(".topEntry .mainContentRightBorder").each(function(i){
		var topContentHeight = $j(this).parent().children(".mainContentBlock").height(); 
		var topTitleHeight = $j(this).parent().children(".mainContentBlock").children(".entryHeading").height(); 
		//$j(this).css('margin-top', topTitleHeight);		
		$j(this).height(topContentHeight-3);
	});
			
	
	$j(".followEntry .mainContentRightBorder").each(function(i){												 	
		var contentHeight = $j(this).parent().children('.mainContentBlock').height();
		$j(this).height(contentHeight-3);
		//alert($j(this).height());
	}); 
	
	var singleContentHeight = $j(".hentry .mainContentRightBorder").parent().children(".mainContentBlock").height();
	var singleTitleHeight = $j(".hentry .mainContentRightBorder").parent().children(".mainContentBlock").children(".entryHeading").height();
	$j(".hentry .mainContentRightBorder").css('margin-top', singleTitleHeight);
	$j(".hentry .mainContentRightBorder").height(singleContentHeight - singleTitleHeight);
	
	
}*/

function checkAllContentImg(){
	$j('#content img').each(function(i){
		if($j(this).attr('width') > 650){
			
		    var ratio = $j(this).attr('width') / 650;
			var h = $j(this).attr('height') / ratio;
			$j(this).width(650);
			$j(this).height(h);
		}
							
	});
	
}

function featureListPosition(){
	
	$j('.productWindow li ul li').each(function(i){
												
		$j(this).children('a').children('img').height(180);
		
		if($j(this).children('a').children('img').width() > 220){
				$j(this).children('a').children('img').width(220);
		}
		$j(this).attr('name', i+1);
		
		if(i % 4 == 0){
			$j(this).addClass('first');	
		}							
	});
	
}

function featurePaginate(itemEach, currentPage){
	//var k = itemEach*(currentPage+1)+1;
	//alert(k);
	$j('.productWindow li ul li').each(function(i){
		if( itemEach*currentPage < $j(this).attr('name') && $j(this).attr('name') < itemEach*(currentPage+1)+1){
				$j(this).show('fast');
		}else{
				$j(this).hide('fast');
		}
	});
}

function seeMoreorLess(itemsEachPage, currentPage){
	var allItem = $j('.productWindow li ul li');
	var len = allItem.length;
	
	if(len < itemsEachPage || len == itemsEachPage){
		$j('.product-next').hide();
		//$j('.product-previous').hide();
	}else {
		if(currentPage == 0){
			//$j('.product-previous').hide();
			$j('.product-next').show();
		}else if(len - (currentPage*itemsEachPage) < itemsEachPage || len - (currentPage*itemsEachPage) == itemsEachPage){
			$j('.product-next').hide();
			//$j('.product-previous').show();
		}
	}
}

function checkBrowser(){
	var ua = navigator.userAgent.toLowerCase();	
	if (/firefox[\/\s](\d+\.\d+)/.test(ua)) {
	    var firefox_version = new Number(RegExp.$1);
		if (firefox_version < 3 && ua.indexOf('mac') != -1) {
		  return true;
		}
    }
  	return false;
}

function manage_default_input(id){
		var inpt = $j(id);
		var val = inpt.val();
		if(inpt){ inpt.val('').blur(function(){ if(inpt.val() != val && inpt.val() != ''){  }else{ inpt.val(val) } }) }
}

function css_browser_selector(u){
		/*
		CSS Browser Selector v0.3.1
		Rafael Lima (http://rafael.adm.br)
		http://rafael.adm.br/css_browser_selector
		License: http://creativecommons.org/licenses/by/2.5/
		Contributors: http://rafael.adm.br/css_browser_selector#contributors
		*/
		var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',h=document.getElementsByTagName('html')[0],b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3')?g+' ff3':is('gecko/')?g:/opera(\s|\/)(\d+)/.test(ua)?'opera opera'+RegExp.$2:is('konqueror')?'konqueror':is('chrome')?w+' '+s+' chrome':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;
}




