﻿var tooltip=function(){
	var id = 'tt';
	var top = 3;
	var left = 3;
	var maxw = 300;
	var speed = 10;
	var timer = 20;
	var endalpha = 95;
	var alpha = 0;
	var tt,t,c,b,h;
	var ie = document.all ? true : false;
	return{
		show:function(v,w){
			if(tt == null){
				tt = document.createElement('div');
				tt.setAttribute('id',id);
				t = document.createElement('div');
				t.setAttribute('id',id + 'top');
				c = document.createElement('div');
				c.setAttribute('id',id + 'cont');
				b = document.createElement('div');
				b.setAttribute('id',id + 'bot');
				tt.appendChild(t);
				tt.appendChild(c);
				tt.appendChild(b);
				document.body.appendChild(tt);
				tt.style.opacity = 0;
				tt.style.filter = 'alpha(opacity=0)';
				document.onmousemove = this.pos;
			}
			tt.style.display = 'block';
			c.innerHTML = v;
			tt.style.width = w ? w + 'px' : 'auto';
			if(!w && ie){
				t.style.display = 'none';
				b.style.display = 'none';
				tt.style.width = tt.offsetWidth;
				t.style.display = 'block';
				b.style.display = 'block';
			}
			if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
			h = parseInt(tt.offsetHeight) + top;
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(1)},timer);
		},
		pos:function(e){
			var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			tt.style.top = (u - h) + 'px';
			tt.style.left = (l + left) + 'px';
		},
		fade:function(d){
			var a = alpha;
			if((a != endalpha && d == 1) || (a != 0 && d == -1)){
				var i = speed;
				if(endalpha - a < speed && d == 1){
					i = endalpha - a;
				}else if(alpha < speed && d == -1){
					i = a;
				}
				alpha = a + (i * d);
				tt.style.opacity = alpha * .01;
				tt.style.filter = 'alpha(opacity=' + alpha + ')';
			}else{
				clearInterval(tt.timer);
				if(d == -1){tt.style.display = 'none'}
			}
		},
		hide:function(){
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
		}
	};
}();

if (!window.SI) { var SI = {}; };
SI.Files =
{
	htmlClass : 'SI-FILES-STYLIZED',
	fileClass : 'file',
	wrapClass : 'cabinet',
	
	fini : false,
	able : false,
	init : function()
	{
		this.fini = true;
		
		var ie = 0 //@cc_on + @_jscript_version
		if (window.opera || (ie && ie < 5.5) || !document.getElementsByTagName) { return; } // no support for opacity or the DOM
		this.able = true;
		
		var html = document.getElementsByTagName('html')[0];
		html.className += (html.className != '' ? ' ' : '') + this.htmlClass;
	},
	
	stylize : function(elem)
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };
		
		elem.parentNode.file = elem;
		elem.parentNode.onmousemove = function(e)
		{
			if (typeof e == 'undefined') e = window.event;
			if (typeof e.pageY == 'undefined' &&  typeof e.clientX == 'number' && document.documentElement)
			{
				e.pageX = e.clientX + document.documentElement.scrollLeft;
				e.pageY = e.clientY + document.documentElement.scrollTop;
			};

			var ox = oy = 0;
			var elem = this;
			if (elem.offsetParent)
			{
				ox = elem.offsetLeft;
				oy = elem.offsetTop;
				while (elem = elem.offsetParent)
				{
					ox += elem.offsetLeft;
					oy += elem.offsetTop;
				};
			};

			var x = e.pageX - ox;
			var y = e.pageY - oy;
			var w = this.file.offsetWidth;
			var h = this.file.offsetHeight;

			this.file.style.top		= y - (h / 2)  + 'px';
			this.file.style.left	= x - (w - 30) + 'px';
		};
	},
	
	stylizeById : function(id)
	{
		this.stylize(document.getElementById(id));
	},
	
	stylizeAll : function()
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };
		
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++)
		{
			var input = inputs[i];
			if (input.type == 'file' && input.className.indexOf(this.fileClass) != -1 && input.parentNode.className.indexOf(this.wrapClass) != -1)
			{
				this.stylize(input);
			};
		};
	}
};

$(document).ready(function() {
	SI.Files.stylizeAll();
	$(".imagewr").hover(function() {
		$(this).children(".info").slideDown('fast',function(){
			$(this).children(".info").show();		

		});					 
	 });
	$(".imagewr").mouseleave(function(){					
		$(this).children(".info").slideUp('fast',function(){
			$(this).children(".info").hide();										  
		});
	});
	
	$("#imgcontainer").hover(function() {
		$(".uploadav").fadeIn('fast',function() {
			$(".uploadav").show();
		});
	});
		
	$("#imgcontainer").mouseleave(function() {
		$(".uploadav").fadeOut('fast',function() {
			$(".uploadav").hide();
		});	
	});
	
	$("#feture").scrollable().autoscroll({steps: 5,interval: 3000});
	
		var validator = $("#registeration").validate({
		rules: {
			username: {
				required: true,
				minlength: 4,
				remote: "index.php?app=user&m=checkusername"
			},
			password: {
				required: true,
				minlength: 6
			},
			password_confirm: {
				required: true,
				minlength: 6,
				equalTo: "#fpassword"
			},
			email: {
				required: true,
				email: true,
				remote: "index.php?app=user&m=checkemail"
			},
		},
		errorPlacement: function(error, element) {
			error.appendTo( $(".showerr") );
		},
		messages: {
			username: {
				required: "<li>نام کاربری را وارد نکردید.<li>",
				minlength: jQuery.format("<li>نام کاربری باید بیشتر از  {0} حرف باشد<li>"),
				remote: jQuery.format("<li>این نام کاربری قبلا ثبت شده است.<li>")
			},
			password: {
				required: "<li>کلمه عبور را وارد نکردید<li>",
				rangelength: jQuery.format("<li>کلمه عبور نباید کوچکتر از  {0} باشد<li>")
			},
			password_confirm: {
				required: "<li>تکرار کلمه عبور را وارد نکردید<li>",
				minlength: jQuery.format("<li>تکرار کلمه عبور نباید کمتر از  {0} حرف باشد<li>"),
				equalTo: "<li>تکرار کلمه عبور ، با کلمه عبور مطابقت ندارد<li>"
			},
			email: {
				required: "<li>لطفا یک آدرس ایمیل صحیح وارد کنید<li>",
				minlength: "<li>لطفا یک آدرس ایمیل صحیح وارد کنید<li>",
				remote: jQuery.format("<li>این آدرس ایمیل قبلا ثبت شده است.<li>")
			},
		},
		submitHandler: function() {
			registerStep(2);
		}
	});
});

function showRegister() {
	$(".registerform").overlay({ 
		top: 172, 
		onClose : function() {
			$("#step2").hide();
			$("#step3").hide();
			$("#step1").show();
		},
		expose: {  
			color: '#fff', 
			loadSpeed: 200, 
			opacity: 0.5 
		}, 
		closeOnClick: false, 
		api: true 
	}).load();	
}

function showLogin() {
	$(".loginform").overlay({ 
		top: 172, 
		expose: {  
			color: '#fff', 
			loadSpeed: 200, 
			opacity: 0.5 
		}, 
		closeOnClick: true, 
		api: true 
	}).load();	
}

function updateProfile() {
	$(".updatepro").overlay({ 
		top: 172, 
		expose: {  
			color: '#fff', 
			loadSpeed: 200, 
			opacity: 0.5 
		}, 
		closeOnClick: true, 
		api: true 
	}).load();	
}

function doUpdatePro() {
		$.ajax({
		  type: "POST",
		  url: "index.php?app=user&m=updateprofile",
		  data: $("#updatepro").serialize(),
		  success: function(msg){
			
		 }
		});
}

function doLogin() {
	var username = $("#lusername").val();
	var password = $("#lpassword").val();
	if ( (username.length < 4) || (password.length < 5) ) {
		$("#loginErr").html("لطفا تمام فیلد ها را کامل پر کنید");
	} else {
		$("#loginErr").html("");
		$("#spinner").show();
		$.ajax({
		  type: "POST",
		  url: "index.php?app=user&m=checkauth",
		  data: $("#login").serialize(),
		  success: function(msg){
				if (msg == 'true') {
					$("#login").submit();
					$("#spinner").hide();
				} else {
					$("#loginErr").html("نام کاربری یا کلمه عبور اشتباه است.");
					$("#spinner").hide();
				}
		 }
		});
	}
	return false;	
}

function uploadClick(time) {
	window.setTimeout(function() {
		var file = 	$(".file").val();
		$("#upload_field").val(file);
	}, time);
}
function showUpload() {
	$(".file").trigger('click');
}
function swapImage(adress) {
	source = $(adress).attr("src").replace("thumb__", "");
	source = 'url(' + source + ')';
	$("#feature").css("background-image",source);
}

function registerStep(step) {
	if (step == 2) {
		$("#step1").fadeOut('fast',function(){
			$("#step1").hide();							
		});
		$("#step2").fadeIn('fast',function() {
			$("#step2").show();						   
		});
	} else if (step == 3) {
		$("#regbtn").attr("disabled","disabled");
		$("#spinner").show();
		$.ajax({
		  type: "POST",
		  url: "index.php?app=user&m=register",
		  data: $("#registeration").serialize(),
		  success: function(msg){
			$("#step2").fadeOut('fast',function(){
				$("#step2").hide();							
			});
			$("#step3").fadeIn('fast',function() {
				$("#step3").show();						   
			});
		 }
		});

	}
}
function updateStat(type,uid,pid,upid) {
	if (type == 'like') {
		$.ajax({
		  type: "POST",
		  url: "index.php?app=user&m=updatestat",
		  data: 'type=like&uid=' + uid + '&pid=' + pid + '&upid=' + upid,
		  success: function(){
			$("#likebtn").attr("src","http://static.qalleqube.com/img/frontend/like-on.png");
		 }
		});
	} else if (type == 'wish') {
		$.ajax({
		  type: "POST",
		  url: "index.php?app=user&m=updatestat",
		  data: 'type=wish&uid=' + uid + '&pid=' + pid + '&upid=' + upid,
		  success: function(){
			$("#wishbtn").attr("src","http://static.qalleqube.com/img/frontend/wish-on.png");
		 }
		});
	}
}
function updateStats(type,uid,pid,upid,controll) {
	if (type == 'like') {
		$.ajax({
		  type: "POST",
		  url: "index.php?app=user&m=updatestat",
		  data: 'type=like&uid=' + uid + '&pid=' + pid + '&upid=' + upid,
		  success: function(){
			$(controll).attr("src","http://static.qalleqube.com/img/frontend/like-on.png");
		 }
		});
	} else if (type == 'wish') {
		$.ajax({
		  type: "POST",
		  url: "index.php?app=user&m=updatestat",
		  data: 'type=wish&uid=' + uid + '&pid=' + pid + '&upid=' + upid,
		  success: function(){
			$(controll).attr("src","http://static.qalleqube.com/img/frontend/wish-on.png");
		 }
		});
	}
}
function insertComment(uid,pid,type,avatar) {
	var cmt = $("#cmt").val();
	if (cmt.length < 3) {
		$("#cmt").val("متن پیام کمتر از 3 حرف است.");
		exit;
	}
	$("#cmt").attr("disabled","disabled");
		$.ajax({
		  type: "POST",
		  url: "index.php?app=user&m=insertcomment",
		  data: 'type=' + type + '&uid=' + uid + '&pid=' + pid + '&cmt=' + cmt,
		  success: function(msg){
			$("#cmt").attr("disabled","");
			$("#cmt").val(" ");
			
			$("#cuc").prepend("<div class='comment'><div id='avatar'><img src='" + avatar +  "' /></div><div id='cm'><div id='pad'><img src='http://static.qalleqube.com/img/frontend/cmbox-right.png' /></div>" + cmt + "</div></div><div class='clear'></div><br />").fadeIn('slow');
			
		 }
		});
}
function showuploadav() {
	$(".uploadavbox").overlay({ 
		top: 172, 
		expose: {  
			color: '#fff', 
			loadSpeed: 200, 
			opacity: 0.5 
		}, 
		closeOnClick: true, 
		api: true 
	}).load();
}

function uploadavform() {
		$.ajax({
		  type: "POST",
		  url: "index.php?app=user&m=uploadavatar",
		  data: 'type=like&uid=' + uid + '&pid=' + pid + '&upid=' + upid,
		  success: function(){
			$(controll).attr("src","http://static.qalleqube.com/img/frontend/like-on.png");
		 }
		});
}
function artistupload() {
	$(".uploadartist").overlay({ 
		top: 172, 
		expose: {  
			color: '#fff', 
			loadSpeed: 200, 
			opacity: 0.5 
		}, 
		closeOnClick: true, 
		api: true 
	}).load();
}
function checkupAvatar() {
	var imgf = $("#avatari").val();
	if (imgf.length < 1) {
		alert('فيلد تصوير خالي است.');
		return false;
	} else {
		return true;
	} 
}
function checkupArt() {
	var imgf = $("#imagear").val();
	if (imgf.length < 1) {
		alert('فيلد تصوير خالي است.');
		return false;
	} else {
		return true;
	} 
}
function calcTotal() {
	var latt = parseInt($("#bslatt").val());
	var paper = parseInt($("#bsphoto").val());
	var size = parseInt($("#bssize").val());
	var base = parseInt($("#bsprice").val());
	var sz = parseInt($("#sizeprice").val());
	
	if (base == 0) {
		var total =  ( ( sz + paper) * size ) + (latt * 500);
	} else {
		var total =  ( ( base + paper) * size ) + (latt * 500);
	}
	
	
	
	$("#totalprice").html(total);
}
function changeLatt(val) {
	$("#bslatt").val(val);
	calcTotal();
	if (val == 0) {
		$("#lattno").html('یک');
	} else if (val == 2) {
		$("#lattno").html('دو');
	} else if (val == 3) {
		$("#lattno").html('سه');
	}
}
function changeMatt(val,title) {
	$("#bsphoto").val(val);
	$("#papert").html(title);
	calcTotal();
}
function changeProd(val,type,pid) {
	$("#bsprice").val(val);
	$("#bsprod").val(pid);
	
	if (type == 'size') {
		$(".sizeSelector").show();
		$(".latt").show();
		$(".material").show();
		$("#matt").show();
		$("#size").show();
		
		$("#latt").show();
		
		$("#myDrawing").show();
		$("#myImg").hide();
		$(".material > b").click();
		
		calcTotal();
	} else if (type == 'cm') {
		$(".material").hide();
		$(".latt").hide();
		
		$(".sizeSelector").show();
		$("#matt").show();
		$("#size").show();
		
		$("#bsphoto").val(0);
		
		$("#papert").html('پوستر');
		$("#latt").hide();
		
		$("#myDrawing").show();
		$("#myImg").hide();
		
		calcTotal();
	} else if(type == 'total') {
		$("#latt").hide();
		$("#matt").hide();
		$("#size").hide();
		$(".material").hide();
		$(".latt").hide();
		$(".sizeSelector").hide();
		
		$("#totalprice").html(val);
		$("#myDrawing").hide();
		$("#myImg").show();
	}
	
}
function calcSizep(selectedSize,size,sizePrice) {
	var bs =  $("#bsprice").val();
	
	$("#sizeprice").val(sizePrice);	
	
	$("#bssize").val(size);
	$("#slsize").html(selectedSize);
	calcTotal();
	
}

function addorder() {
	var latt = parseInt($("#bslatt").val());
	var total = parseInt($("#totalprice").html());
	var paper = $("#papert").html();
	var size = $("#slsize").html();
	var base = parseInt($("#bsprice").val());
	var sz = parseInt($("#sizeprice").val());
	var prod = $("#bsprod").val();
	var pid = $("#bsartwork").val();
	
		$.ajax({
		  type: "POST",
		  url: "order.php?m=add",
		  data: 'latt=' + latt + '&total=' + total + '&paper=' + paper + '&size=' + size + '&base=' + base + '&sz=' + sz + '&pid=' + pid + '&prod=' + prod,
		  success: function(msg){
			window.location = 'http://www.qalleqube.com/order.php?m=my';
		 },
		 error : function(XMLHttpRequest, textStatus, errorThrown) {
				window.location = 'http://www.qalleqube.com/order.php?m=my';
		 }
		});
		
}
function setShipment(val) {
	$("#shipment").val(val);
	$(".submit").attr("disabled","");
	
	if (val == 2) {
		$(".uploadavbox").overlay({ 
			top: 172, 
			expose: {  
				color: '#fff', 
				loadSpeed: 200, 
				opacity: 0.5 
			}, 
			closeOnClick: true, 
			api: true 
		}).load();
	}
}
function addtid() {
	var tid = $("#tid").val();
	$("#tnum").val(tid);

}