jQuery(function(){
	$("#main-nav li, #shop-nav li").iNav();
});
//simple navigation animate
$.fn.iNav=function(){
	return this.hover(function(){
		var $target=$(this);
		nav=setTimeout(function(){
			$target.children("ul").stop(false,true).slideDown("fast");
			$target.children("a").children("img.icon").animate({marginLeft:0},"fast");
		},300);
	},function(){
		clearTimeout(nav);
		$(this).children("ul").stop(false,true).slideUp("fast");
		if(!$(this).hasClass("sel"))
			$(this).children("a").children("img.icon").animate({marginLeft:"-100px"},"fast");
	});
}
//home slider (custom function)
$.fn.ihSlider=function(start){
	if(start)
		openSheet($(start));
	processing=false;
	return this
	.click(function(){
		var $target=$(this);
		if((!$target.hasClass("hs-active"))&&(!processing)){
			var $old=null;
			if($(".hs-active").length)$old=$(".hs-active");
			changeSheet($old,$target);
		}
	})
	.each(function(){
		$(".hide",$(this).siblings()).click(function(){
			var $target=$(this).closest(".slide-content");
			if(!$target.hasClass("slide-tab"))
				$target=$target.prev(".slide-tab");
			changeSheet($target,null);
			return false;
		});
	});
}
	function openSheet(target){
		if(target){
			$("#wrapper a").hide();
			var nw=target.children().children(".thumb").width();
			var sheet_box=target.next(".slide-content");
			if(target.hasClass("slide-content")){
				sheet_box=target;
				target.addClass("hs-active").show().children().children(".thumb").fadeOut(500).siblings(".wrap").animate({"width":"show"},500);
			} else {
				target.addClass("hs-active").children().animate({"width":nw+"px"},500).children(".thumb").animate({"opacity":"1"},500);
				sheet_box.show().children(".holder").animate({"width":"show"},500);
			}
		} else {
			$("#wrapper a").show();
		}
		processing=false;
	}
	function changeSheet(old,target){
		processing=true;
		if(old){
			var sheet_box=old.next(".slide-content");
			if(old.hasClass("slide-content")){
				old.removeClass("hs-active").children().children(".wrap").animate({"width":"hide"},"fast",function(){
					openSheet(target);
				}).siblings(".thumb").show().animate({"opacity":"0.2"},"fast");
			} else {
				old.removeClass("hs-active").children(".wrap").animate({"width":"67px"},"fast",function(){
					openSheet(target);
				}).children(".thumb").animate({"opacity":"0.2"},"fast");
				sheet_box.children(".holder").animate({"width":"hide"},500,function(){$(this).parent().hide();});
			}
		}
		else 
			openSheet(target);
	}
//home slider real-women
$.fn.iWomenSlider=function(data){
	this.each(function(){
		var list=$(this)
		.scrollable({keyboard:false})
		.navigator('.flownavs')
		.data("scrollable")
		.onSeek(function(){
			$(data).children().slideUp(400).eq(list.getIndex()).slideDown(400);
		});
	});
}
//custom gallery for bfr (press)
$.fn.custGallery=function(){
	return this.each(function(){
		$.fn.setItemGallery=function(){
			return this.each(function(){
				var $setter=$(this);
				$target.attr({"href":$setter.attr("href"),"title":$setter.attr("title")}).children("img.photo").attr("src",$setter.attr("rel"));
				$setter.addClass("sel").siblings(".sel").removeClass("sel");
				$target.lightbox({fileLoadingImage:path+'/images/lbox/loading.gif',fileBottomNavCloseImage:path+'/images/lbox/closelabel.gif'});
				if($num>0)$("a.prev",$pag).removeClass("dis");else $("a.prev",$pag).addClass("dis");
				if($num<$sum)$("a.next",$pag).removeClass("dis");else $("a.next",$pag).addClass("dis");
				$(".pag .num").html($num+1);
			});
		}
		var $wrap=$(this);
		var $target=$(".large > a",$wrap);
		var $thumbs=$(".thumbs a",$wrap);
		var $sum=$thumbs.length-1;
		var $num=0;
		var $pag=$(".pag",$wrap);
		$($thumbs).click(function(){
			$(this).setItemGallery();
			return false;
		}).eq(0).setItemGallery();
		$(".pag a",$(this)).click(function(){
			var $anch=$(this).attr("href").substring($(this).attr("href").indexOf('#')+1);
			if($anch=="prev")$num--;
			if($anch=="next")$num++;
			$thumbs.eq($num).setItemGallery();
			return false;
		});
	});
}
//blur input
jQuery.fn.iFocusBlur=function(){
	return this
	.each(function(){
		if($(this).val()!="")
			$(this).siblings("label").css({"display":"none"});
		else 
			$(this).siblings("label").css({"display":"block"});
	})
	.focus(function(){
		if($(this).val()=="")
			$(this).siblings("label").css({"opacity":"0.5","display":"block"});
	})
	.blur(function(){
		if($(this).val()=="")
			$(this).siblings("label").css({"opacity":"1","display":"block"});
	})
	.keypress(function(event){
		$(this).siblings("label").css({"display":"none"});
	});
}
//simple accordion (find/stores list)
$.fn.iSimpAccord=function(){
	return this.click(function(e){
		var $target=$(e.target).closest("li");
		if(!$target.hasClass("sel"))
			$target.addClass("sel").siblings(".sel").removeClass("sel");
	});	
}
//sheets
jQuery.fn.iSheets=function(){
	return this.click(function(){
		$.fn.changeSheet($(this));
		return false;
	});
}
jQuery.fn.changeSheet=function(target){
	var sheet=target.attr("href").substring(target.attr("href").indexOf('#'));
	var holder=target.attr("rel");
	$("#"+holder+" .sheet").hide();
	if(sheet!="#close")
		$("#"+holder).fadeIn("fast").children(sheet).show();
	else 
		$("#"+holder).fadeOut("fast",function(){$("#"+holder+" .sheet").hide();});
}
//popup
jQuery.fn.iPopup=function(){
	$("body").append('<div id="playout"><table><tr><td><div id="pwrap"></div><div id="popup" class="popup"><a href="#close" id="pclose" class="close">close</a><div id="pcontent" class="sheet">Loading...</div></div></td></tr></table></div>');
	$("#pclose, #pwrap").click(function(){
		$.fn.hPopup();
		return false;
	});
	return this.click(function(){
		$.fn.sPopup();
		return false;
	});
}
	jQuery.fn.sPopup=function(){
		$("#playout").show();
	}
	jQuery.fn.hPopup=function(){
		$("#playout").hide();
		$("#pcontent").html("Loading...");
	}
//shopping basket feature
$.fn.iBasket=function(){
	return this.each(function(){
		basket=$(this);
		basket_list=null;
		$(".view",basket).click(function(){
			if(basket.hasClass("active"))
				hBasket();
			else
				sBasket();
			return false;
		});
		setBasketList();
	});
}
	function sBasket(){
		basket.animate({height: "87px",paddingTop:"4px"},"medium").addClass("active");
	}
	function hBasket(){
		resetBasketItem();
		basket.animate({height: "33px",paddingTop:0},"medium").removeClass("active");
	}
	function setBasketList(){
		basket_list=$(".flowpanes",basket)
		.click(function(e){
			var $target=$(e.target).closest("li");
			if($target.is("li"))
				if($target.hasClass("sel"))
					resetBasketItem();
				else
					setBasketItem($target.attr("id"));
		})
		.scrollable({keyboard:false,prev:".browse-left",next:".browse-right",activeItems:5})
		.data("scrollable")
		.onBeforeSeek(function(){
			resetBasketItem();
		});
	}
	function setBasketItem(target){
		resetBasketItem();
		$target=$("#"+target);
		if(!$target.hasClass("sel")){
			$target.addClass("sel");
			var lp=$target.offset().left-basket.offset().left+parseInt($(".flowpopup").children(".wrapper").position().left)+4;
			$(".flowpopup").css({"left":lp+"px"}).show().children(".data").html($target.children(".data").clone());
		}
	}
	function resetBasketItem(){
		$(".flowpopup").hide().children(".data").empty();
		$("li.sel",basket).removeClass("sel");
	}
	function addBasketItem(data){
		basket_list.addItem(data).end();
	}
	function updateBasketItem(target,qty){
		$("#"+target+" input.qty-val",basket).attr("value",qty);
	}
	function removeBasketItem(target){
		$(".flowpopup").hide().children(".data").empty();
		$("#"+target,basket).remove();
	}

//fadepanes
(function($){
	$.fn.iGallery=function(options){
		settings=null;
		gdelay=null;
		$.fn.iGallery.defaults={
			delay:3,
			speed:"slow",
			easing:"swing"
		};
		settings=$.extend($.fn.iGallery.defaults, options);
		return this.each(function(){
			$.fn.iGallery.init(this);
		});
	}
	$.fn.iGallery.init=function(gallery){
		icount=$(gallery).children().length;
		$(window).load(function(){
			if(icount>1)
				$.fn.iGallery.change(gallery,0,settings.delay);
			else 
				$(gallery).children().transitionIn();
			$(gallery).addClass("loaded");
		}).resize(function(){
			var $d = ($(window).width()-369)/$(window).height();
			if($d < 1.5){
				$(gallery).addClass('height-fixed');
			} else {
				$(gallery).removeClass('height-fixed');
			}
		}).trigger('resize');
	}
	$.fn.iGallery.change=function(gallery,i,delay){
		$(gallery).children(".sel").removeClass("sel").transitionOut();
		$(gallery).children().eq(i).addClass("sel").transitionIn();
		if(i==(icount-1))
			i=0;
		else 
			i++;
		gdelay=setTimeout(function(){
			$.fn.iGallery.change(gallery,i,settings.delay);
		},delay*1000);
	}
	$.fn.transitionIn=function(){
		return this.animate({opacity:'show'},settings.speed,settings.easing);
	}
	$.fn.transitionOut=function(){
		return this.animate({opacity:'hide'},(settings.speed+parseInt(settings.speed/2)),settings.easing);
	}
	$.fn.iGalleryEmpty=function(){
		$(this).remove();
		clearTimeout(gdelay);
		settings=null;
	}
})(jQuery);
	

function initUploadify(http_path, session_id){
		$(function() {
		$("#uploadify").uploadify({
			'uploader'       : http_path+'/uploadify.swf',
			'script'         : http_path+'/uploadify.php',
			'cancelImg'      : http_path+'/js/uploadify/cancel.png',
			'folder'         : 'photos',
			'queueID'        : 'fileQueue',
			'auto'           : true,
			'buttonText'     : 'upload',
			//'queueSizeLimit' : 1,
			'multi'          : true,
			'fileDesc'		 : 'jpg',
			'fileExt'		 : '*.jpg',
			'sizeLimit'      : '524288000',//max size bytes - 800kb
	        'scriptData'     : {'session_id':session_id},
	        'session_id'     : session_id, 

			'onComplete'  	 : function(event,queueID,fileObj,response,data) {
				                $("#response").attr('innerHTML', "File(s) upload complete!");
								},													
			'onError'		 : function(){
									alert('error');
							   }			   
		});
		});   	
}


function initUploadifyM(http_path, session_id){
	$(function() {
	$("#uploadify").uploadify({
		'uploader'       : http_path+'/uploadify.swf',
		'script'         : http_path+'/uploadify.php',
		'cancelImg'      : http_path+'/js/uploadify/cancel.png',
		'folder'         : 'photos',
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'buttonText'     : 'upload',
		'queueSizeLimit' : 1,
		'multi'          : false,
		'fileDesc'		 : 'jpg',
		'fileExt'		 : '*.jpg',
		'sizeLimit'      : '52428800',//max size bytes - 800kb
        'scriptData'     : {'session_id':session_id},
        'session_id'     : session_id, 

		'onComplete'  	 : function(event,queueID,fileObj,response,data) {
			                $("#response").attr('innerHTML', "File(s) upload complete!");
							},													
		'onError'		 : function(){
								alert('error');
						   }		   
	});
	});   	
}

function initUploadifyVideo(http_path, session_id){
        $(function() {
        $("#uploadify").uploadify({
            'uploader'       : http_path+'/uploadify.swf',
            'script'         : http_path+'/uploadify.php',
            'cancelImg'      : http_path+'/js/uploadify/cancel.png',
            'folder'         : 'videos',
            'queueID'        : 'fileQueue',
            'auto'           : true,
            'buttonText'     : 'upload',
            //'queueSizeLimit' : 1,
            'multi'          : true,
            'fileDesc'       : 'Web Video Files (.mov,.flv,.mp4,.f4v,.3gp,.3g2)',
            'fileExt'        : '*.mov;*.flv;*.mp4;*.f4v;*.3gp;*.3g2',

            //'fileDesc'         : 'jpg',
            //'fileExt'         : '*.jpg',
            'sizeLimit'      : '52428800',//max size bytes - 800kb
            'scriptData'     : {'session_id':session_id},
            'session_id'     : session_id, 

            'onComplete'       : function(event,queueID,fileObj,response,data) {
                                $("#response").attr('innerHTML', "File(s) upload complete!");
                                },                                                    
            'onError'         : function(){
                                    alert('error');
                               }               
        });
        });       
}

function initUploadifyMVideo(http_path, session_id){
    $(function() {
    $("#uploadify").uploadify({
        'uploader'       : http_path+'/uploadify.swf',
        'script'         : http_path+'/uploadify.php',
        'cancelImg'      : http_path+'/js/uploadify/cancel.png',
        'folder'         : 'videos',
        'queueID'        : 'fileQueue',
        'auto'           : true,
        'buttonText'     : 'upload',
        'queueSizeLimit' : 1,
        'multi'          : false,
        //'fileDesc'       : 'Web Video Files (.mov,.flv)',
        //'fileExt'        : '*.mov;*.flv;*.mp4;',
        'fileDesc'       : 'Web Video Files (.mov,.flv,.mp4,.f4v,.3gp,.3g2)',
        'fileExt'        : '*.mov;*.flv;*.mp4;*.f4v;*.3gp;*.3g2',
        //'fileDesc'         : 'jpg',
        //'fileExt'         : '*.jpg',
        'sizeLimit'      : '52428800',//max size bytes - 800kb
        'scriptData'     : {'session_id':session_id},
        'session_id'     : session_id, 

        'onComplete'       : function(event,queueID,fileObj,response,data) {
                            $("#response").attr('innerHTML', "File(s) upload complete!");
                            },                                                    
        'onError'         : function(){
                                alert('error');
                           }           
    });
    });       
}


function get_reviews_page(page){
    $.ajax({
        url: path + "/product/"+product_action +"/get_reviews/page/"+page,
        success: function(data) {            
            $("#reviews").html(data);
        }
    });
}

function write_review(type){    
    var post_data;
    if (type=='login'){
        post_data = $("#login_form").serialize();
    } else if(type=='review'){
        post_data = $("#write_review_form").serialize();
    }
    $.post(
        path + "/product/"+product_action +"/write_review/",
        post_data,
        function(data) {            
            $("#pcontent").html(data);
        }
    );
}

function subscribe(){
 $.post(path+'/subscribe/', $("#subscriber_email").serialize(),function(data){
	 if (!data) {
			 $("#subscribe .result").html('<img src="'+path+'/images/titles/subscribe-thanks.png" alt="Thank you for subscribing" />');           
			 $("#subscribe .lbl").removeClass("error").html("enter e-mail");                         
	 } else 
		$("#subscribe .lbl").addClass("error").html(data);           
	 $("#get_subscriber_email").val("").focus();
	 
 });
}  

function wrapperClosePopup(){	
	//alert('test');
	jQuery.fn.hPopup();
	//$("#popup").hide();
}

