

Trendy.Product = {
	voteStatus: null,
	booCheck: 1,
	initialize: function(){
		if($("#pageContent.product .col1 .scroll-pane"))
		$("#pageContent.product .col1 .scroll-pane").jScrollPane({"scrollbarWidth":13,"showArrows":true, "arrowSize":"10"});
		
	},
	addComment: function(idProduct){
		Trendy.Lightbox.open('/product/addcomment-form/?id='+idProduct,'Trendy.Product.initializeForm()',null);
	},
	initializeForm: function(){
		$("#divLightboxContent #formComment .submit").click(function(){$("#formComment").submit();});
		var options = {
			url: '/product/addcomment/',
			success: function(idProduct){
				if(idProduct > 0){
					Trendy.Lightbox.close();
					$("#pageContent.product .col1 .comments .scroll-pane").fadeOut(function(){
						$(this).load('/product/comments/?id='+idProduct, function(){$(this).fadeIn(function(){Trendy.Product.initialize()});});
					});
				}
			}
		}
		$("#formComment").validate({
			submitHandler: function(error) {
				$("#formComment").ajaxSubmit(options);
			}
		});
	},
	vote: function(idproduct){
		if(Trendy.Product.voteStatus == null){
			$.ajax({
				type: "POST",
				data: "id="+idproduct+"&intVote="+$("input[name=intVote]").val(),
				dataType: "json",
				url: '/product/vote/',
				success: function(data){
					if(data.message != '') $(".voteMsg").html(data.message).fadeIn();
					Trendy.Product.voteStatus = 1;
				}
			});
		}
	},
	voteFill: function(intVote){
		if(Trendy.Product.voteStatus == null){
			var intCursor; 
			$(".vote img").each(function(e){
				intCursor = e+1;
				if(intCursor <= intVote) $(this).attr('src','/public/img/front/button/voteFilledStar.png');
				else $(this).attr('src','/public/img/front/button/voteEmptyStar.png');
				
				$("input[name=intVote]").val(intVote);
			});
		}
	},
	voteFill2: function(intVote){
		var intCursor; 
		$(".vote img").each(function(e){
			intCursor = e+1;
			if(intCursor <= intVote) $(this).attr('src','/public/img/front/button/voteFilledStar.png');
			else $(this).attr('src','/public/img/front/button/voteEmptyStar.png');
			
			$("input[name=intVote]").val(intVote);
		});
	},
	addToFavoriteDesigns: function(id){
		$.ajax({
			type: "POST",
			url: '/myworkshop/ajaxaddfavoritedesign/',
			data: "id="+id,
			success: function(data){
				//$(".addToFavoriteDesigns").fadeOut(function(){$(".addToFavoriteDesigns").fadeIn().addClass('on');});
				$(".addToFavoriteDesigns").fadeOut();
			}
		});
	},
	checkVote: function(idproduct){
		if(Trendy.Product.voteStatus == null && Trendy.Product.booCheck == 1)
			$.ajax({
				type: "POST",
				data: "id="+idproduct,
				dataType: "json",
				url: '/product/check-vote/',
				success: function(data){
					if(data.message != '') $(".voteMsg").html(data.message).fadeIn();
					if(data.status != '' ){ 
						Trendy.Product.voteStatus = data.status;
						Trendy.Product.voteFill2($("input[name=intAvg]").val());
					}else{
						Trendy.Product.voteStatus = null;
						Trendy.Product.booCheck = null;
					}
				}
			});
	},
	
	initializeForm2: function(){
		var form;
		form = $("#addProductCommentForm");
		$("#addProductCommentForm .submit").click(function(){
			if(Trendy.Auth.isLogged() == false){
				Trendy.Lightbox.open(Trendy.Subscribe.url, "Trendy.Subscribe.initializeForm()",null);
				return false;
			}else 
				form.submit();
		});
		var options = {
			url: '/product/add-comment/',
			success: function(idClient){
				//$("#designerComments").fadeOut(function(){
					//$(this).load('/product/comments/?id='+idClient, function(){$(this).fadeIn(function(){
						//$("#pageContent.gallery .content.col23 #designerComments.scroll-pane").jScrollPane({"scrollbarWidth":13,"showArrows":true, "arrowSize":"10"});
					//	$("#addProductCommentForm textarea[name=strComment]").val('');
					//		});

				//	});
				//});
				$("#addProductCommentForm textarea[name=strComment]").val('');
			}
		}
		$("#addProductCommentForm").validate({
			rules:{
				strComment: {required:true}
			},
			messages:{
				strComment: ''
			}
/*			,
			submitHandler: function(error) {
				$("#addProductCommentForm").ajaxSubmit(options);
			}
*/
		});
		
	}
	
	
}

$().ready(function() {
	Trendy.Product.initialize();
});Trendy.Gallery = {
	highlight: function(dom){
		$(dom).addClass('on');
	},
	
	highlightOff: function(dom){
		$(dom).removeClass('on');
	},
	
	linkToProductDetails: function(idProduct,idSaleDesigner){
		document.location.href = '/CG_LANG_LINK_PRODUCTDETAILS?id=' + idProduct +'&ic=' +idSaleDesigner;
	},
	
	linkToDesigner: function(strPseudo){
		document.location.href = '/CG_LANG_LINK_DESIGNER_GALLERY' + strPseudo;
	},
	addToFavoriteDesigners: function(id){
		$.ajax({
			type: "POST",
			url: '/myworkshop/ajaxaddfavoritedesigner/',
			data: "id="+id,
			success: function(data){
				//$(".addToFavoriteDesigners").fadeOut(function(){$(".addToFavoriteDesigners").fadeIn().addClass('on');});
				$(".addToFavoriteDesigners").fadeOut();
			}
		});
	},
	
	initializeForm: function(){
		var form;
		form = $("#addCommentForm");
		$("#addCommentForm .submit").click(function(){
			if(Trendy.Auth.isLogged() == false){
				Trendy.Lightbox.open(Trendy.Subscribe.url, "Trendy.Subscribe.initializeForm()",null);
				return false;
			}else 
				form.submit();
		});
		var options = {
			url: '/designer/add-comment/',
			success: function(idClient){
				Trendy.Lightbox.close();
				$("#designerComments").fadeOut(function(){
					$(this).load('/designer/comments/?id='+idClient, function(){$(this).fadeIn(function(){
						$("#pageContent.gallery .content.col23 #designerComments.scroll-pane").jScrollPane({"scrollbarWidth":13,"showArrows":true, "arrowSize":"10"});
						$("#addCommentForm textarea[name=strComment]").val('');
					});});
				});
			}
		}
		$("#addCommentForm").validate({
			rules:{
				strComment: {required:true}
			},
			messages:{
				strComment: ''
			},
			submitHandler: function(error) {
				$("#addCommentForm").ajaxSubmit(options);
			}
		});
	}
}

$().ready(function() {
	Trendy.Gallery.initializeForm();
	$("#designerComments.scroll-pane").jScrollPane({"scrollbarWidth":13,"showArrows":true, "arrowSize":"10"});
//	$("#pageContent.gallery .content.col23 #designerComments.scroll-pane").jScrollPane({"scrollbarWidth":13,"showArrows":true, "arrowSize":"10"});
});Trendy.Auth = {
	initialize: function(){
		$("a[rel=checkLogged]").click(function(){
			if(Trendy.Auth.isLogged() == false){
				Trendy.Lightbox.open(Trendy.Subscribe.url, "Trendy.Subscribe.initializeForm()",null);
				return false;
			}
		});
	},
	isLogged: function(){
		var statusReturned = true;
		$.ajax({
			url:'/auth/islogged/', 
			async: false,
			success: function(data){
				if(data == '-1') statusReturned = false;
			}
		});
		return statusReturned;
	},
	login: function(){
		var optionsAuth = {
			url: Trendy.Subscribe.urlAuth,
			success: function(data){
				if(data == '1'){
					$(".rememberError").hide();
					$("#loginError").html('CG_LANG_LOGIN_ERROR1').fadeIn();
				}
				else if(data == '2'){
					$(".rememberError").hide();
				 	$("#loginError").html('CG_LANG_LOGIN_ERROR2').fadeIn();
				}
				else document.location.href = data;

			}
		};
		
		$("#formLogin").validate({
			rules:{
				strLogin: {required:true},
				strPassword: {required:true}
			}, 
			messages:{
				strLogin: '<img src="/public/icons/cross.png">',
				strPassword: '<img src="/public/icons/cross.png">'
			},
			submitHandler: function(error) {
				$("#formLogin").ajaxSubmit(optionsAuth);
			}
		});
	},
	forgottenPassword: function(){
		var optionsRemember = {
			success: function(data){
				if(data == '-1') 		$(".rememberError").html('CG_LANG_LOGIN_ERROR3').fadeIn();
				else{
					Trendy.Auth.seeLoginForm();
					$(".rememberError").html('CG_LANG_LOGIN_REMEMBEROK').fadeIn();
				}
			}
		}
		$("#formRemember").validate({
			rules:{
				strLogin: {required:true}
			}, 
			messages:{
				strLogin: '<img src="/public/icons/cross.png">'
			},
			submitHandler: function(error) {
				$("#formRemember").ajaxSubmit(optionsRemember);
			}
		});
	},
	invitation: function(){
		var optionsAuth = {
			url: '/auth/invitation/',
			success: function(error){
				if(error == '0')		$("#pageContent.login .right #subscribeForm").slideUp(function(){
											$("#pageContent.login .right #subscribeFormValid").slideDown();
										});
				else if(error == "1")	$("#invitationError").html('CG_LANG_INVITATION_ERROR1').fadeIn(); 
			}
		}
		$("#formInvitation").validate({
			rules:{
				strFirstName: {required: true},
				strLastName: {required: true},
				strEmail: {required: true, remote: Trendy.Subscribe.url+"ajaxcheckfieldinvitation", email:true},
				strPseudo: {required: true, remote: Trendy.Subscribe.url+"ajaxcheckfieldinvitation", rangelength:[4,15]},
				strPassword: {required:true}
			}, 
			messages:{
				strFirstName: '<img src="/public/icons/cross.png">',
				strLastName: '<img src="/public/icons/cross.png">',
				strEmail: '<img src="/public/icons/cross.png">',
				strPseudo: '<img src="/public/icons/cross.png">',
				strPassword: '<img src="/public/icons/cross.png">'
			},
			submitHandler: function(error) {
				$("#formInvitation").ajaxSubmit(optionsAuth);
			}
		});
	},
	seeRememberForm: function(){
		$("#loginForm").fadeOut(function(){$("#rememberForm").fadeIn();});	
	},
	seeLoginForm: function(){
		if($("#rememberForm").css('display') != 'none')
		$("#rememberForm").fadeOut(function(){$("#loginForm").fadeIn();});
		else if($("#subscribeForm").css('display') != 'none')
		$("#subscribeForm").fadeOut(function(){$("#loginForm").fadeIn();});
		else if($("#subscribeFormValid").css('display') != 'none')
		$("#subscribeFormValid").fadeOut(function(){$("#loginForm").fadeIn();});
		
	},
	seeSubscribeForm: function(){
		$(".rememberError").hide();
		if($("#rememberForm").css('display') != 'none')
		$("#rememberForm").fadeOut(function(){$("#subscribeForm").fadeIn(function(){
		
			Trendy.Auth.initializeForm();
		});});
		else if($("#loginForm").css('display') != 'none')
		$("#loginForm").fadeOut(function(){$("#subscribeForm").fadeIn(function(){
			Trendy.Auth.initializeForm();
		});});
	},
	strComment: null,
	strLastName: null,
	strFirstName: null,
	strPseudo: null,
	strPassword: null,
	strEmail: null,
	initializeForm: function(){
		if($("#subscribeForm").css('display') != 'none' && $(".infosPseudo").css('display') == 'none'){
			var topPosition = parseInt($("input[name=strPseudo]").position().top)-5;
			var leftPosition = parseInt($("input[name=strPseudo]").position().left)+220;
			
			$(".infosPseudo").css({'left':leftPosition,'top':topPosition}).fadeIn();
		}
				
		if(Trendy.Auth.strComment == null || Trendy.Auth.strComment == '') Trendy.Auth.strComment = $("textarea[name=strComment]").val();
		if(Trendy.Auth.strLastName == null || Trendy.Auth.strLastName == '') Trendy.Auth.strLastName = $("input[name=strLastName]").val();
		if(Trendy.Auth.strFirstName == null || Trendy.Auth.strFirstName == '') Trendy.Auth.strFirstName = $("input[name=strFirstName]").val();
		if(Trendy.Auth.strPseudo == null || Trendy.Auth.strPseudo == '') Trendy.Auth.strPseudo = $("input[name=strPseudo]").val();
		if(Trendy.Auth.strEmail == null || Trendy.Auth.strEmail == '') Trendy.Auth.strEmail = $("input[name=strEmail]").val();
		if(Trendy.Auth.strLogin == null || Trendy.Auth.strLogin == '') Trendy.Auth.strLogin = $("input[name=strLogin]").val();
		
		$("textarea").unbind('focus').unbind('blur');
		$("input").unbind('focus').unbind('blur');
		
		$("textarea[name=strComment]").focus(function(){if($(this).val()==Trendy.Auth.strComment) $(this).val('');});
		$("textarea[name=strComment]").blur(function(){if($(this).val()=='') $(this).val(Trendy.Auth.strComment);});
		
		$("input[name=strLastName]").focus(function(){if($(this).val()==Trendy.Auth.strLastName) $(this).val('');});
		$("input[name=strLastName]").blur(function(){if($(this).val()=='') $(this).val(Trendy.Auth.strLastName);});
		
		$("input[name=strFirstName]").focus(function(){if($(this).val()==Trendy.Auth.strFirstName) $(this).val('');});
		$("input[name=strFirstName]").blur(function(){if($(this).val()=='') $(this).val(Trendy.Auth.strFirstName);});
		
		$("input[name=strPseudo]").focus(function(){if($(this).val()==Trendy.Auth.strPseudo) $(this).val('');});
		$("input[name=strPseudo]").blur(function(){if($(this).val()=='') $(this).val(Trendy.Auth.strPseudo);});
		
		$("input[name=strPassword]").focus(function(){if($(this).val()=='******') $(this).val('');});
		$("input[name=strPassword]").blur(function(){if($(this).val()=='') $(this).val('******');});
		
		$("input[name=strEmail]").focus(function(){if($(this).val()==Trendy.Auth.strEmail) $(this).val('');});
		$("input[name=strEmail]").blur(function(){if($(this).val()=='') $(this).val(Trendy.Auth.strEmail);});
		
		$("input[name=strLogin]").focus(function(){if($(this).val()==Trendy.Auth.strLogin) $(this).val('');});
		$("input[name=strLogin]").blur(function(){if($(this).val()=='') $(this).val(Trendy.Auth.strLogin);});
	}
};