jQuery.noConflict();

/*
  * TYPOGRAPHY
*/

Cufon.replace('#gallery h2, #flash_gallery h2, #total_comments, h3, h4, h5, h6', {
	hover: true
});

/*
  * WE CALL PK_ FUNCTIONS
*/

jQuery(document).ready(function(){	
	jQuery("#menu").pk_menu();
	jQuery("#extras").pk_fixed();
	jQuery("#gallery").pk_gallery();
	jQuery("#tab_wrapper").pk_slide_tabs();
	jQuery("#formMail").pk_form();
	
	/**/
			
	jQuery("body").pk_improvements();
});

/*
  * PK_MENU ***
*/

(function($) {
	$.fn.pk_menu = function(options) {
		var defaults = {
			autoPosition: true,
			limitValue: "wrapper",
			easing: "easeOutQuint",
			speedIn: 400,
			speedOut: 200
		};
		
		var settings = $.extend({}, defaults, options);
		
		/**/
		
		return this.each(function () {
			var $root = $(this);
			var $mainmenu = $(">ul", this);
			var $headers = $mainmenu.find("ul").parent();
			var $limitValue = (settings.limitValue == "document") ? $(window).width() : 960;
			
			/** CLASS CURRENT **/
		
		 	var me = $("li.current", this);
		 	var w = me.width();
		 	var h = me.height();
            var $back = $('<li class="back"><div class="left"></div></li>').appendTo(me).width(w).height(h);
            $("li.back", this).css("left", 0 + "px");
            $("li.current a", this).css("background", "none");
            
			/**/
		
			$headers.each(function () {
				var $curobj = $(this);
				var $subul = $(this).find('ul:first');
				var $ul = $("ul", $curobj);
				var $rounded = $(".last", $root).length;
			
				$("ul ul", $root).css({display: "none"});
			
				/**/
			
				function getProperty($li, $ul) {
					$li.dimensions = {
						w: $li.offsetWidth, 
						h: $li.offsetHeight, 
						subulw: $ul.outerWidth(), 
						subulh: $ul.outerHeight()
					}
					return $li.dimensions;
				}
			
				function showMenu ($element, $e) {
					$element.css({marginTop: 10 + "px", visibility:'visible'});
					$element.hoverFlow($e.type, {
						'marginTop': 0,
      					'opacity': 'show'
    				}, settings.speedIn, settings.easing);
				}
 
    			function hideMenu ($element, $e) {
    				$element.hoverFlow($e.type, {
      					'opacity': 'hide'
    				}, settings.speedOut, settings.easing, function() {
    					$element.hide();
    				});
    			}
			
				/**/
			
				$curobj.hover(function(e) {
					getProperty(this, $subul);
					var $targetul = $(this).find("ul:first");
					var $offset = $(this).offset();
				
					if($curobj.parents("ul").length == 1) {
						$ul.css({visibility:'hidden'});
						this.firstLevel = true;
						$subul.css({top: this.dimensions.h + "px"});
					} else {
						this.firstLevel = false;
						$subul.css({top: 0});
					}
					
					if(this.firstLevel) {
						var menuleft = 0;
					} else {
						var menuleft = this.dimensions.w;
					}
					
					if($offset.left + menuleft + this.dimensions.subulw > $limitValue) {
						if(this.firstLevel) {
							menuleft = -(this.dimensions.subulw + this.dimensions.w);
						} else {
							if($rounded > 0) {
								menuleft = -(this.dimensions.w);
							} else {
								menuleft = -(this.dimensions.w + 2);
							}
						}
					}
				
					if(settings.autoPosition == true) {
						$targetul.css({left:menuleft + "px"});
					}
					showMenu($targetul, e);
				}, function(e) {
					var $targetul = $(this).find("ul:first");
					hideMenu($targetul, e);
				});
			});
		});
	};
})(jQuery);

/*
  * PK_GALLERY ***
*/

(function($) {
	$.fn.pk_gallery = function(options) {
		var defaults = {
			photos: ".item",
			thumbs: ".preview_images a",
			caption: "yes",
			slideshow: true,
			timer: 100000,
			easing: "easeInOutQuad",
			speedIn: 400,
			speedOut: 600
		};
		
		var settings = $.extend({}, defaults, options);
		
		/**/
		
		return this.each(function() {
			var $root = $(this);
			var $items = $(settings.photos , $root);
			var $thumbs = $(settings.thumbs, $root);
			var $totItems = $items.length;
			var $title = $("h2", $root).text();
			var $interval = null;
			
			$items.css({"position": "absolute"});
			
			/**/
			
			function slideshow($index) {
				if($index){
					var id = $index;
				} else {
					var id = 0;
				}
				$interval = setInterval(
					function() {
						play(id);
					}, settings.timer
				);
			}
			
			function play($index) {
				var id = $index;
				if(id < ($thumbs.length - 1)) {
					id++;
				} else {
					id = 0;
				}
				$thumbs.filter(":eq("+ id +")").trigger("click", [true]);
			}
			
			function changeThumbs($current) {
				for(i = 0; i < $thumbs.length; i++) {
					$thumbs.filter(":eq("+ i +")").find("img").stop().animate({
						"opacity": 1
					}, settings.speedOut, settings.easing);
				}
				$current.stop().animate({
					"opacity": 0.3
				}, settings.speedIn, settings.easing);
			}
			
			function changeTitle($string) {
				if($string) {
					$("h2", $root).text($string);
				} else {
					$("h2", $root).text($title);
				}
				Cufon.replace("#gallery h2");
			}
			
			/**/
	
			$thumbs.each(function(i) {
				$(this).click(function() {
					var $new_item = $items.filter(":eq(" + i + ")");
					var $media = $items.filter(":eq(" + i + ")").find("#movie").length;
					
					this.id = i;
					
					if(settings.slideshow) {
						if($interval) {
							clearInterval($interval);
						}
						if($media == 0) {
							slideshow(this.id);
						}
					}
					
					if($new_item.css("display") == "none") {
						$items.filter(":visible").fadeOut(settings.speedOut, settings.easing);
						$new_item.fadeIn(settings.speedIn, settings.easing);
					}
					
					changeThumbs($("img", this));
					changeTitle($(this).attr("title"));
					
					return false;
				});
			});
			
			/**/
			
			$thumbs.filter(":eq(0)").trigger("click", [true]);
			
			/**/
			
			if(settings.caption == "yes") {
				$items.find(".caption").fadeIn(settings.speedIn, settings.easing);
			}
			if(settings.caption == "autoHide") {
				$items.hover(function(){
					if($.browser.msie) {
						$(this).find(".caption").show();
					} else {
						$(this).find(".caption").fadeIn(settings.speedIn, settings.easing);
					}
				}, function() {
					if($.browser.msie) {
						$(this).find(".caption").hide();
					} else {
						$(this).find(".caption").fadeOut(settings.speedOut, settings.easing);
					}
				});
			}
			
			/**/
		
			$(".preview_images").pk_scroll({
				visibleItems: 5,
				scrollValue: 120,
				width: 584,
				height: 64,
				buttonPrev: '#th_prev',
				buttonNext: '#th_next',	
				easing: "easeOutQuad",
				speed: 300
			});
		});
	}
})(jQuery);

/*
  * PK_GRID ***
*/

(function($) {
	$.fn.pk_grid = function(options) {
		var defaults = {
			columsnumber: 3,
			box: ".medium_box",
			boxmarginleft: 20,
			boxmarginbottom: 60,
			boxwidth: 220
		};
		
		var settings = $.extend({}, defaults, options);
		
		/**/
		
		return this.each(function() {
			var self = $(this);
			var $box = $(settings.box, this);
			var index = 0;
			var gridheight = 0;
			var verticalPositions = [];
			var divsHeight = [];
			
			/**/
			
			function getHigherColumn(values, columns, margin) {
				var rows = Math.ceil(values.length / columns);
  				var res = 0;
  				for (var h = 0; h < columns; h++) {
  					var val = 0;
  					for (var i = 0; i < rows; i++) {
  						val += (values[(h + (i * columns))] == undefined) ? (rows > 1) ? 0 - margin : 0 : values[(h + (i * columns))];
  					}
  					res = Math.max(res, val);
  				}
  				return res + (margin * (rows - 1));
  			}
  			
			/**/
			
			$box.css({
				'position': 'absolute',
				'width': settings.boxwidth  + 'px'
			});
			$box.each(function(i) {
				var verticalPosition = 0;
				var horizontalPosition = (index * (settings.boxwidth + settings.boxmarginleft));
				
				if(verticalPositions[index]) { 
					verticalPosition = verticalPositions[index];
				}
				
				$(this).css({
					'margin-left': horizontalPosition + 'px',
					'margin-top': verticalPosition + 'px'
				});
						
				divsHeight[i] = $(this).outerHeight();
				verticalPositions[index] = verticalPosition + $(this).outerHeight() + settings.boxmarginbottom;
				index++;
				
				if (index === settings.columsnumber) { 
					index = 0; 
				}
			});
			
			/**/
			
			gridHeight = (getHigherColumn(divsHeight, settings.columsnumber, settings.boxmarginbottom));
			$(this).css({
				'padding-left': 20 + 'px',
				'height': gridHeight  + 'px'
			});
		});
	}
})(jQuery);

/*
  * PK_SLIDE_TABS ***
*/

(function($) {
	$.fn.pk_slide_tabs = function(options) {
		var defaults = {
			tabs: ["#search", "#social_networks"],
			buttons: ["#button_search", "#button_sn"],
			icons: ['./images/skin/button_close.png', 'images/skin/button_search.png', './images/skin/button_sn.png'],
			easing: "easeOutExpo",
			speedIn: 1000, 
			speedOut: 1000
		};
		
		var settings = $.extend({}, defaults, options);
		
		/**/
		
		return this.each(function() {
			
			var $root = $(this);
			var $width = $root.width();
			var $tab_search = $(settings.tabs[0], $root);
			var $tab_networks = $(settings.tabs[1], $root);
			var $button_search = $(settings.buttons[0]);
			var $button_networks = $(settings.buttons[1]);
			
			/**/
			
			$root.search = false;
			$root.networks = false;
			
			/**/
			
			function animateTabs($tab, $action, $button, $icon) {
				if($action == "open") {
					$root.show();
					$tab.stop().show().animate({
						'marginLeft': 0
					}, settings.speedIn, settings.easing, function() {
						changeButtonImage($button, $icon);
					});
				} else {
					$tab.stop().animate({
						'marginLeft': 230
					}, settings.speedOut, settings.easing, function () {
						if($root.search == false && $root.networks == false) {
							$root.hide();
						}
						changeButtonImage($button, $icon);
						$tab.hide();
					});
				}
			}
		
			function changeButtonImage($button, $icon) {
				if(!$icon) {
					$("img", $button).attr('src', settings.icons[0]);
				} else {
					$("img", $button).attr('src', $icon);
				}
			}
			
			/**/
		
			$button_search.click(function () {
				if($root.networks == true){
					$button_networks.click();
				}
				if($root.search == false) {
					$root.search = true;
					animateTabs($tab_search, "open", $(this));
				} else {
					$root.search = false;
					animateTabs($tab_search, "close", $(this), settings.icons[1]);
				}
			});
			
			$button_networks.click(function () {
				if($root.search == true){
					$button_search.click();
				}
				if($root.networks == false) {
					$root.networks = true;
					animateTabs($tab_networks, "open", $(this));
				} else {
					$root.networks = false;
					animateTabs($tab_networks, "close", $(this), settings.icons[2]);
				}
			});
			
			/*
			  * SOCIAL NETWORK: ROLLOVER
			*/
			
			$("#sn_list ul li", $root).hover(function () {
				var $label = $(this).find("img").attr('alt');
				$(".label", $root).text($label);
			}, function(e) {
				$(".label", $root).text("...");
			});
			
			$("#sn_list").pk_scroll();
		});
	};
})(jQuery);

/*
  * PK_SCROLL
*/

(function($) {
	$.fn.pk_scroll = function(options){
		var defaults = {
			visibleItems: 3,
			scrollValue: 35,
			width: 100,
			height: 30,
			buttonPrev: '#sn_prev',
			buttonNext: '#sn_next',	
			easing: "easeOutBack",
			speed: 400
		}; 
		
		var settings = $.extend({}, defaults, options);
		
		return this.each(function() {  
			var $root = $(this);
			var $buttonNext = $(settings.buttonNext);
			var $buttonPrev = $(settings.buttonPrev);
			var $totalItems = $("li", $root).length;
			var $limitScroll = $totalItems - (settings.visibleItems);
			var $listWidth = $totalItems * settings.scrollValue;
			var $indexScroll = 0;
			
			/**/
			
			$($root).css({'overflow' : 'hidden', 'width': settings.width + "px", 'height' : settings.height + 'px'});
			$("ul", $root).css('width', $listWidth + "px");
			
			/**/
			
			function scroll($button) {
				if ($button == "next") {
					$indexScroll = ($indexScroll >= $limitScroll) ? $limitScroll : $indexScroll + 1;	
				} else {
					$indexScroll = ($indexScroll <= 0) ? 0 : $indexScroll - 1;
				}
				scrollValue = ($indexScroll * settings.scrollValue * -1);
				$("ul", $root).animate({ 
					marginLeft: scrollValue 
				}, settings.speed, settings.easing);				
			};
			
			/**/
			
			$buttonPrev.hide();
			$buttonNext.hide();
			$buttonNext.click(function() {	
				scroll("next");
				if($.browser.msie) {
					if ($indexScroll >= $limitScroll) {
						$(this).hide();
					}
					$buttonPrev.show();
				} else {
					if ($indexScroll >= $limitScroll) {
						$(this).stop().animate({
							'opacity': 0.3
						}, settings.speed);
					}
					$buttonPrev.stop().animate({
						'opacity': 1
					}, settings.speed);
				}
			});
			$buttonPrev.click(function() {		
				scroll("prev");
				if($.browser.msie) {
					if ($indexScroll <= 0) {
						$(this).hide();
					}
					$buttonNext.show();
				} else {
					if ($indexScroll <= 0) {
						$(this).stop().animate({
							'opacity': 0.3
						}, settings.speed);
					}
					$buttonNext.stop().animate({
						'opacity': 1
					}, settings.speed);
				}
			});	
			
			/**/
			
			if($totalItems > settings.visibleItems) {
				$buttonNext.fadeIn(settings.speed);
				if($.browser.msie) {
					$buttonPrev.hide();
				} else {
					$buttonPrev.show().animate({
						'opacity': 0.3
					}, settings.speed);
				}
			}
		});
	};

})(jQuery);

/*
  * PK_FORM ***
*/

(function($) {
	$.fn.pk_form = function(options) {
		var defaults = {
			php: "sendMail.php",
			text: "Your message has been sent. Thanks!",
			textfields: ["#name", "#email", "#subject", "#message"],
			response: "#response",
			submit: "#submit",
			timer: 4000,
			easing: "",
			speedIn: 400,
			speedOut: 400
		};
		
		var settings = $.extend({}, defaults, options);
		
		return this.each(function () {
			var $root = $(this);
			var $response = $(settings.response);
			var $submit = $(settings.submit);
			
			/**/
			
			function showResponse($timer) {
				$submit.fadeOut(100);
				$response.css("opacity", 0);
				$response.show().stop().animate({
 					opacity: 1
 				}, settings.speedIn, settings.easing, function(){
					interval = setInterval(hideResponse, $timer);
				});
			}
			
			function hideResponse() {
				clearInterval(interval);
				$submit.fadeIn(400);
				$response.stop().animate({
					opacity:0
				}, settings.speedIn, settings.easing, function() {
					$response.hide();
				});
			}
			
			/**/
		
			$root.submit(function(){
				$.ajax({
					type: "POST",
					url: settings.php,
					data: $(this).serialize(),
					success: function(output) {
						$response.ajaxComplete(function() {
							if(output == 'ok') {
								for(var i = 0; i < settings.textfields.length; i++) {
									$(settings.textfields[i], $root).val('');
								}
								$(this).html("<p>" + settings.text
								+ "</p>");
								showResponse(settings.timer * 2);
							} else {
								$(this).html(output);
								showResponse(settings.timer);
							}
						});
					}
				});
				return false;
			});
		});
	}
})(jQuery);

/*
  * PK_FIXED ***
*/

(function($) {
	$.fn.pk_fixed = function() {
		return this.each(function() {
		
			var $element = $(this);
			var $msie6 = $.browser == "msie" && $browser.version < 7;
		
			if(!$msie6) {
				var $top = $element.offset().top;
		
				$(window).scroll(function() {
					var $y = $(window).scrollTop();
		
					if ($y >= $top) {
						$element.addClass('fixed');
					} else {
						$element.removeClass('fixed');
					}
				});
			}
		});
	};
})(jQuery);

/*
  * PK_IMPROVEMENTS ***
*/

(function($) {
	$.fn.pk_improvements = function(options) {
		var defaults = {
			easing: "easeOutQuint",
			speedIn: "fast",
			speedOut: "fast"
		};
	
		var settings = $.extend({}, defaults, options);
		
		return this.each(function () {
			var $root = $(this);
			var $buttons = $root.find(".buy_now a, .buy_now_detail_page a");
			
			/**/
			
			if($buttons.length > 1) {
				$buttons.hover(function() {
					$("img", this).stop().animate({"opacity": 0.6}, settings.speedIn);
				}, function() {
					$("img", this).stop().animate({"opacity": 1}, settings.speedOut);
				});
			}
			
			/* OPERA FIX */
			
			if($.browser.opera) {
				$root.find(".grid_read_more").css("margin-top", 0 + "px");
			}
			
			/* PRETTYPHOTO --- theme: light_rounded / dark_rounded / light_square / dark_square */
			
			if($("body").find("a[rel^='prettyPhoto']").length > 1) {
				$("a[rel^='prettyPhoto']").prettyPhoto({
					"opacity": 0.80,
					"theme": 'light_square'	
				});
			}
		});
	}
})(jQuery);

/*
  * END PK_CODE ***
*/


/*
  * hoverFlow - A Solution to Animation Queue Buildup in jQuery
  * Version 1.00

  * Copyright (c) 2009 Ralf Stoltze, http://www.2meter3.de/code/hoverFlow/
  * Dual-licensed under the MIT and GPL licenses.
  * http://www.opensource.org/licenses/mit-license.php
  * http://www.gnu.org/licenses/gpl.html
*/


(function($) {
	$.fn.hoverFlow = function(type, prop, speed, easing, callback) {
		/*
		  * only allow hover events
		*/ 
		
		if ($.inArray(type, ['mouseover', 'mouseenter', 'mouseout', 'mouseleave']) == -1) {
			return this;
		}
	
		/*
		  * build animation options object from arguments
		  * based on internal speed function from jQuery core
		*/
		
		var opt = typeof speed === 'object' ? speed : {
			complete: callback || !callback && easing || $.isFunction(speed) && speed,
			duration: speed,
			easing: callback && easing || easing && !$.isFunction(easing) && easing
		};
		
		/*
		  * run immediately
		*/
		
		opt.queue = false;
			
		/*
		  * wrap original callback and add dequeue
		*/ 
		
		var origCallback = opt.complete;
		opt.complete = function() {
		
			/* execute next function in queue */
			$(this).dequeue();
			
			/* execute original callback */
			if ($.isFunction(origCallback)) {
				origCallback.call(this);
			}
		};
		
		/*
		  * keep the chain intact
		*/
		
		return this.each(function() {
			var $this = $(this);
		
			/* set flag when mouse is over element */
			if (type == 'mouseover' || type == 'mouseenter') {
				$this.data('jQuery.hoverFlow', true);
			} else {
				$this.removeData('jQuery.hoverFlow');
			}
			
			/* enqueue function */
			
			$this.queue(function() {				
				/* check mouse position at runtime */
				var condition = (type == 'mouseover' || type == 'mouseenter') ?
				
				/* read: true if mouse is over element */
				$this.data('jQuery.hoverFlow') !== undefined :
					
				/* read: true if mouse is _not_ over element */
				$this.data('jQuery.hoverFlow') === undefined;
					
				/* 
				  * only execute animation if condition is met, which is:
				  * - only run mouseover animation if mouse _is_ currently over the element
				  * - only run mouseout animation if the mouse is currently _not_ over the element
				*/
				
				if(condition) {
					$this.animate(prop, opt);
				/* else, clear queue, since there's nothing more to do */
				} else {
					$this.queue([]);
				}
			});

		});
	};
})(jQuery);