// by PandaThemes.com



// ******************************
//
//		B Y   R E A D Y
//
// ******************************



var f = jQuery.noConflict();

jQuery(document).ready(function() {



	/* -- P A N D A   S L I D E R -------------------------------------------- */

			/*
				TRANSITIONS
				ptd	- Default
				ptf	- Fade
				ptfv	- Fade vertical
				ptv	- Slide vertical
				pth	- Slide horizontal
				pto	- Overlay vertical
			*/




	f('.pslider').each(function(){

		var parent = f(this);
		var pnavbar = parent.find('.pnavbar');
		var timeDelay = parent.attr('title'); parent.removeAttr('title');




		// SET DIMENTIONS FOR EACH SLIDE
		f('.pslides > div').each(function(){

			var w = parent.width();
			var h = f(this).height();
			
				f(this).css({width: w, height: h})
		});




		// ACTIVATION

			// Common
			var h = f('.pslides > div:first',this).height();

				f('.ploading',this).hide();
				f('.pslides',this).css({height: 50}).delay(300).animate({height: h}, 750);

			// First slide for the group of transitions
			var pttransition = [
				"ptd",
				"ptv",
				"pth",
				"pto"
			];

				for (var i in pttransition) {
					if (parent.hasClass(pttransition[i])) {
						f('.pslides > div:first',parent).addClass('block');
					};
				};

			// First slide for the Fade group of transitions
			var pttransition = [
				"ptf",
				"ptfv"
			];

				for (var i in pttransition) {
					if (parent.hasClass(pttransition[i])) {
						f('.pslides > div',this).css({opacity:0});
						f('.pslides > div:first',this).addClass('block').animate({opacity:1}, 750);
					};
				};




		// AUTOPLAY

		f(function() {
		
			if (timeDelay) {
	
				setInterval(function() {
		
					checkHover();
		
				}, timeDelay);
	
			}

	  })




		// CHECK MOUSEHOVER

		var checkCount = 0;

		function checkHover() {
			
			checkCount++;

			if ( parent.hasClass('autopause') || !checkCount ) {

				f(this).delay(timeDelay,function() { checkHover() })

			} else {

				next()

			};

		}




		// AUTOPAUSE

		parent.hover(

			function () {
				f(this).addClass('autopause');
			},

			function () {
				f(this).removeClass('autopause');
			}

		)




		// NEXT AUTO

		function next() {

			// Get a slides
			var currentSlide = parent.find('.pslides > div.block');
			nextSlide = currentSlide.next().length ? currentSlide.next() : parent.find('.pslides').children().eq(0);

			// Get the tabs
			var currentTab = parent.find('.pscurrent');
			var nextTab = currentTab.next().next().length ? currentTab.next().next() : parent.find('.pnav span:first');

				// Select the tabs
				currentTab.removeClass('pscurrent');
				nextTab.addClass('pscurrent');


			// T r a n s i t i o n s

				// Transition Default
				if ( parent.hasClass('ptd') ) {

					// Hide active slide
					currentSlide.removeClass('block');

					// Show new slide
					nextSlide.addClass('block');

					placeholderHeight();

				}

				// Transition Fade
				if ( parent.hasClass('ptf') ) {

					// Hide active slide
					currentSlide.animate({opacity: 0}, 750, function(){ f(this).removeClass('block'); placeholderHeight(); });

					// Show new slide
					nextSlide.addClass('block').animate({opacity: 1}, 750);
	
				}

				// Transition Fade vertical
				if ( parent.hasClass('ptfv') ) {

					// Hide active slide
					currentSlide.css({zIndex: 2}).animate({opacity: 0, top: 50}, 750, function(){ f(this).css({zIndex: 1}).removeClass('block'); placeholderHeight(); });

					// Show new slide
					nextSlide.addClass('block').css({top: 0, opacity: 1});

				}

				// Transition Vertical
				if ( parent.hasClass('ptv') ) {

					h = currentSlide.height();

					// Hide active slide
					currentSlide.css({zIndex: 1}).animate({height: 0}, 1000, function(){ f(this).css({height: h}).removeClass('block'); });

					// Show new slide
					nextSlide.addClass('block').css({zIndex: 2, top: h}).animate({top: 0}, 750);

					// Change the height of placeholder
					placeholderHeight();

				}

				// Transition Horizontal
				if ( parent.hasClass('pth') ) {

					w = parent.width();

					// Hide active slide
					currentSlide.css({zIndex: 1}).animate({left: 0}, 1500, function(){ currentSlide.removeClass('block') });

					// Show new slide
					nextSlide.addClass('block').css({zIndex: 2, left: w}).animate({left: 0}, 750, function(){ placeholderHeight(); });

				}

				// Transition Overlay
				if ( parent.hasClass('pto') ) {

					var h = currentSlide.height();
					var newH = nextSlide.height();

					// Hide active slide
					currentSlide.css({zIndex:  1}).animate({height: 0}, 750, function(){ currentSlide.css({height: h}).removeClass('block') });

					// Show new slide
					nextSlide.addClass('block').css({zIndex: 2, height: 0}).animate({height: newH}, 750, function(){ placeholderHeight(); });

				}


			// Change placeholder height
			function placeholderHeight() {

				newH = nextSlide.height();
				f('.pslides',parent).animate({height: newH}, 750);

			}


		}




		// BY CLICK

		f('.pnav span',this).click(function(){

			span = f(this);

				
				if ( ! span.hasClass('pscurrent')) {

					// Select the tab
					span.parent().children('.pscurrent').removeClass('pscurrent');
					span.addClass('pscurrent');


					spanIndex = span.prevAll('span').length;
					newSlide = f('.pslides',parent).children().eq(spanIndex);
					newH = newSlide.height();

					// Hide active slide
					f('.pslides',parent).children('.block').removeClass('block');

					// Show new slide
					newSlide.addClass('block').css({opacity: 1, top: 0});

					placeholderH();

				}


				// Change placeholder height
				function placeholderH() {

					var newH = newSlide.height();
					f('.pslides',parent).animate({height: newH}, 750);

				}


		});



	})

	/* -- e n d   P A N D A   S L I D E R -------------------------------------------- */



	// AUTOMATIC PRETTYPHOTO FOR IMAGES
	f('a').each(function(){

		var rel = f(this).attr('rel');

		if (rel != 'prettyPhoto[gallery]') {

			var url = f(this).attr('href');

			if (url.match(/\b(jpg|jpeg|png|gif)\b/gi)) {

				f(this).attr('rel','prettyPhoto');

			};

		}

	});



	// PRETTYPHOTO FOR FLICKR
	f('.flickr_badge_image a').attr({

		rel: 'prettyPhoto[flickr]',
		href: function() {

			var src = f(this).find('img').attr('src');

			var href = src.replace(/_s.jpg/g,'.jpg');

			return href;

		}

	});



	// PRETTYPHOTO ACTIVATION
	f("a[rel^='prettyPhoto']").prettyPhoto();



});




// ******************************
//
// 		C O M M O N
//
// ******************************



var p = jQuery.noConflict();

p(function(){



	// TOOLTIPS
	p('.tooltip').mouseenter(function(){

		var tt_title = p(this).attr("title");

		if (tt_title) {

			var tt_id = 'tt_id' + Math.floor(Math.random()*999);

			p(this).attr('title','');
			p("body").append('<div id="' + tt_id + '" class="tt_wrap"><div class="ttbox"><span>' + tt_title + '</span></div></div>');

			offset = p(this).offset();
			width = p(this).outerWidth() / 2;
			tt_left = offset.left - 90 + width;
			tt_top = offset.top - 250;
			tt_wrap_id = '#' + tt_id;

			var tt_box_id = tt_wrap_id + '> div';

			p(tt_wrap_id).css({left: tt_left, top: tt_top});
			p(tt_box_id).css({bottom:15, opacity:0}).animate({bottom:5, opacity:1}, 300);

		}

		p(this).mouseleave(function(){

			p(tt_box_id).stop().animate({bottom:30, opacity:0}, 300, rem);

				function rem() {
					p(this).parent().remove();
				}

			p(this).attr('title',tt_title);

		})

	})



	// TABS
	p('.kul li').each(function(){
		
		p(this).click(function(){

			var li = p(this);

				li.parent().children('.kcurrent').removeClass('kcurrent');
				li.addClass('kcurrent');

			var liIndex = li.prevAll().length;
			var ktabs = li.parent().next('.ktabs');

				ktabs.children('.block').removeClass('block');
				ktabs.children().eq(liIndex).addClass('block');

		})

	});


		// Tabs auto-activation
		p('.kul-auto li:first-child').addClass('kcurrent');
		p('.ktabs-auto div:first-child').addClass('block');


		// Current tab on comments
		var cmt = p('#comments');
			cmt.children().eq(0).addClass('kcurrent');
			cmt.next('.ktabs').children().eq(0).addClass('block');


		// Comments qty on tabs
		var cm = p('#cmt-qty').html();
			if (cm > 0) { cmt.children('#cmt').append('&nbsp;(' + cm + ')').css({'position':'relative'}); } // Relative for Chrome


		// Tracbacks qty on tabs
		var tr = p('#trb-qty').html();
			if (tr > 0) { cmt.children('#trb').append('&nbsp;(' + tr + ')').removeClass('none').css({'position':'relative'}); } // Relative for Chrome


		// Calculate the qty of images
		var q = p('.product-image').size();
			if (q) { p('#images-data-tab').append(' (' + q + ')').css({'position':'relative'}) }


		// Calculate the qty of reviews
		var q = p('.rated-review').size();
			if (q) { p('#tab-reviews, #reviews-total').append(' (' + q + ')').css({'position':'relative'}) }


		// Open the Reviews tab
		p('#reviews-total').click(function(){
			p('.kul li').removeClass('kcurrent').parent().find('#tab-reviews').addClass('kcurrent');
			p('.ktabs > div').removeClass('block');
			p('.ktabs > div:last-child').addClass('block');
		})


		// Hide an empty the Specs & Images tabs
		var id = [
			"#specs-data",
			"#images-data"
		];

		for (var i in id) {

			var spec = p(id[i]).html();

			if ( ! spec ) { p(id[i]+'-tab').addClass('none'); };

		};



	// RATING
	p('#rstars div').mouseenter(function(){

		var div = p(this);

		starIndex = div.prevAll().length + 1;

		var pos = 40 * starIndex;

		div.parent().css('background-position','0 -'+pos+'px');

	}).mouseleave(function(){

		p('#rstars').removeAttr('style');

	}).click(function(){

		p(this).parent().removeAttr('class').addClass('star-'+starIndex);
		p('#rate').val(starIndex);

	});



	// QUICK REPLY FORM
	p('a.quick-reply').click(function(){

		var id = p(this).attr('title');
		var form = p('#commentform-a');
		var author = p('#author-'+id).html();

			form.find('#to-author').html(author);

			form.removeClass('none');

			form.remove();

			p('#commentform, .cancel-reply').addClass('none');
			p('#review-label').removeClass('none');

			// Put the form to the holder
			p('#comment-'+id).find('.quick-holder:eq(0)').append(form);
			p('#comment-'+id).find('.cancel-reply:eq(0)').removeClass('none');
		
			// Set an ID for hidden field
			p('#comment_parent').val(id);

		return false;

	})


		// Cancel reply
		p('a.cancel-reply').click(function(){

			p('#commentform-a, .cancel-reply').addClass('none');
			p('#commentform').removeClass('none');

			return false;

		})


		// Check the Website input field
		p('input[type="submit"]').mousedown(function(){

			var url = p(this).parent().parent().parent().find('input[name="url"]');
			var lnk = url.val();

			if ( lnk == 'Website' ) { url.val('') ;}

			return true;

		})



	// FIX THE HEIGHT OF THE POSTS ON PRODUCT ARCHIVE
	p('.prdcts').each(function(){

		// Titles
		var maxH = 0;

			p('h6',this).each(function(){

				var h = p(this).height();

					if (h > maxH) { maxH = h }

			})

			p('h6',this).css('height',maxH);

		// Excerpts
		var maxH = 0;

			p('.prdcts-excerpt',this).each(function(){

				var h = p(this).height();

					if (h > maxH) { maxH = h }

			})

		p('.prdcts-excerpt',this).css('height',maxH);

	})



	// FIX THE HEIGHT OF THE POSTS SHORTCODE
	p('.grid-auto').each(function(){

		var maxH = 0;

			p('.posts-post',this).each(function(){

				var h = p(this).height();

					if (h > maxH) { maxH = h }

			})

		p('.posts-post > div',this).css('height',maxH);

	})



	// TOGGLE + ACCORDION
	p('.toggle .ta, .toggle .t').click(function() {

		var parent = p(this).parent();

		if ( parent.parent().hasClass('accordion') ) {

			var accordion = parent.parent();

		};

		arrow = parent.find('.t div');
		title = p(this);
		box = p(this).parent().find('.tb');
		height = box.find('.tc').outerHeight(true);

		if ( ! parent.hasClass('tclicked')) {

			// For accordion
			if (accordion) {

				accordion

					.find('.tclicked').removeClass('tclicked')

					.find('.tb').stop(true, false).animate({height:0}, 300)

					.parent().find('.t div').stop(true, false).animate({top: '0'});

			};

			// For toggle
			parent.addClass('tclicked');

			box.stop(true, false).animate({height:height}, 300, function(){

				arrow.stop(true, false).animate({top: '-26px'});

			});

		}

		else {

			// For accordion
			if (accordion) {

				accordion

					.find('.tclicked').removeClass('tclicked')

					.find('.tb').stop(true, false).animate({height:0}, 300)

					.parent().find('.t div').stop(true, false).animate({top: '0'});

			};

			// For toggle
			parent.removeClass('tclicked');

			box.stop(true, false).animate({height:0}, 300, function(){

				arrow.stop(true, false).animate({top: '0'});

			});

		}

	});


		// Toggle auto-activation
		p('.toggle-auto').each(function(){

			var toggle = p(this);

			var height = toggle.find('.tc').outerHeight(true);

				toggle

					.addClass('tclicked')

					.find('.tb').stop(true, false).animate({height: height}, 300)

					.parent().find('.t div').css({top: '-26px'});

	  })



});
