// JavaScript Document
$(document).ready(function(){
		$("#basket").click(function() {
			$("#popup_order_box").show();
		});
		$(".close").click(function() {
			$(".overlay").hide();
		});
		$(".close_box").click(function() {
			$(".overlay").hide();
		});
		$(".reg").click(function() {
			$("#registration_box").show();
		});		
		$(".login, #registration_already").click(function() {
			$("#login_box").show();
		});		
		$("#person").click(function() {
			$("#company_box").hide();
			$("#person_box").show();
		});
		$("#company").click(function() {
			$("#company_box").show();
			$("#person_box").hide();
		});		
		$("#leasing_show").click(function() {
			$("#leasing").show();
		});				
		$(".print").click(function() {
			$('#content').printElement(); 
		});
		//changePassword
		if(changePassword) {
			$.ajax({
			  url: '/users/changePassword/',
			  data: { close: false },
			  dataType: "script"
			});
		}
		
		//ShowLogin
		if(showLogin) {
			showLoginBox();
		}
		
		$(".compare_check").click(function () {
			var checked = 'false';
			if($(this).is(":checked")) {
				checked = 'true';
			}
			$.ajax({
			  url: '/products/compare/',
			  data: { box: 'true', checked: checked, id: $(this).val() },
			  dataType: "script"
			});
		});
		
		$('label[for!=""]').addClass('pointer');
		leasing.init();
		setPagingEvents();
		makeDetailsClickable();
});

function makeDetailsClickable() {
	$('.product .right ul').css('cursor', 'pointer').click(function(){window.location.href= $('a', this).attr('href');});
}
function setPagingEvents() {
	if ($('#filters').length == 0) return;
	$('.paging a').click(function(e) {
		e.preventDefault();
		$('#fUrl').val($(this).attr('href'));
		showFilter();
		return false;
	});
}


//SHOW FLY MESSAGE
function showMessage(message, title, type) {
	$('body').append('<div class="overlay" id="registration_box"><div class="pop_up"><div class="gray_line"><a href="javascript: void(0);" class="close" title="">X</a><h1>'+title+'</h1></div><div class="content '+type+'"><strong>'+message+'</strong></div></div></div>');
	$(".close").click(function() { $(".overlay").remove(); });
}

//LOGIN USER
function showLoginBox() {
	showLoading();
	$(".overlay").remove();
	$.ajax({
	  url: '/users/login/',
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
}

function submitLogin() {
	showLoading();
	
	if($('#login-input-captcha').length > 0) {
		var data = { login:'login', email: $('#login-input-email').val(), password: $('#login-input-password').val(), code: $('#login-input-captcha').val() };
	}
	else {
		var data = { login:'login', email: $('#login-input-email').val(), password: $('#login-input-password').val() };
	}
	
	$.ajax({
	  url: '/users/login/',
	  type: 'POST',
	  data: data,
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
	return false;
}

function showForgottenPassword() {
	showLoading();
	$(".overlay").remove();
	$.ajax({
	  url: '/users/forgotten/',
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
}

function submitForgotten() {
	showLoading();
	
	if($('#forgotten-input-captcha').length > 0) {
		var data = { forgotten:'forgotten', email: $('#forgotten-input-email').val(), code: $('#forgotten-input-captcha').val() };
	}
	else {
		var data = { forgotten:'forgotten', email: $('#forgotten-input-email').val() };
	}
	
	$.ajax({
	  url: '/users/forgotten/',
	  type: 'POST',
	  data: data,
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
	return false;
}

function showChangePassword() {
	showLoading();
	$.ajax({
	  url: '/users/changePassword/',
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
}

function submitPasswordChange() {
	showLoading();
	
	if($('#pass-input-captcha').length > 0) {
		var data = { change:'change', password: $('#change-input-password').val(), new_password: $('#change-input-password2').val(), new_password2: $('#change-input-password3').val(), code: $('#pass-input-captcha').val() };
	}
	else {
		var data = { change:'change', password: $('#change-input-password').val(), new_password: $('#change-input-password2').val(), new_password2: $('#change-input-password3').val() };
	}
	
	$.ajax({
	  url: '/users/changePassword/',
	  type: 'POST',
	  data: data,
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
	return false;
}

function closeProductImage() {
	$("#product-image").slideUp();	
}

function showProductImage(t) {
	$("#product-image-src").attr('src', $(t).attr('href'));
	$("#product-image-src").show();
	$("#product-video").hide();
	
	$("#product-image").slideDown();
	
	return false;
}

function showVideo() {
	
	$("#product-image-src").hide();
	$("#product-video").show();
	
	$("#product-image").slideDown();
	
	return false;
}

function buyMe(id, leasing) {
	showLoading();
	$.ajax({
	  url: '/products/buy/',
	  data: {id: id, leasing:leasing},
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
}

function loadBasket() {
	showLoading();
	$.ajax({
	  url: '/orders/basket/',
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
}

function changeOrderCount(id, t) {
	showLoading();
	$.ajax({
	  url: '/orders/basket/',
	  data: { changeCount: 'true', id: id, count: $(t).val() },
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
}

function changeCount(id, t) {
	showLoading();
	$.ajax({
	  url: '/orders/basket/',
	  data: { changeCount2: 'true', id: id, count: $(t).val(), deliveryPay: withNalojen },
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
}

function deleteFromCart(id) {
	showLoading();
	$.ajax({
	  url: '/orders/basket/',
	  data: { del: 'true', id: id },
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
}

function showLoading() {
	$("#loading").show();
}

function hideLoading() {
	$("#loading").hide();
}

function showProducer(id) {
	$("#categories > span").removeClass('selected');
	$("#categories > span").removeClass('round_top');
	$("#producer-btn-"+id).addClass('selected');
	$("#producer-btn-"+id).addClass('round_top');
	$("#categories > div").hide();
	$("#producer-box-"+id).show();
}

function clear_form_elements(ele) {
	$(ele).find(':input').each(function() {
		switch(this.type) {
			case 'password':
			case 'select-multiple':
			case 'select-one':
			case 'text':
			case 'textarea':
				$(this).val('');
				break;
			case 'checkbox':
			case 'radio':
				this.checked = false;
		}
	});
	$("#amount").val($( ".slider-range" ).slider('option','min'));
	$("#amount2").val($( ".slider-range" ).slider('option','max'));
	$( ".slider-range" ).slider('values',0,$("#amount").val());
	$( ".slider-range" ).slider('values',1,$("#amount2").val());
}

var byprice = 'asc';
function showFilter(type, val) {
	
	/*if(type != '') {
		filterURL[type] = val;
	}*/
	filterURL.minprice = $("#amount").val();
	filterURL.maxprice = $("#amount2").val();
	filterURL.byprice = byprice;
	filterURL.producer = $('#producerId').val();
	
	$(".filter-select-box").each(function(index, value) { 
		var f = $(this).attr('id');
		if($(this).val() != '') {
			filterURL[f.substring(2)] = $(this).val();
		}
		else {
			filterURL[f.substring(2)] = '';
		}
	});
	
	$(".filter-checkbox").each(function(index, value) { 
		if($(value).is(":checked")) {
			filterURL.extras[$(value).val()] = 'yes';
		}
		else {
			filterURL.extras[$(value).val()] = '';
		}		
	});
	
	showLoading();
	$.ajax({
	  url: '/products/getlaptops/'+$('#fUrl').val(),
	  data: filterURL,
	  dataType: "script",
	  complete: function() {
		hideLoading();
		setPagingEvents();
		makeDetailsClickable();
	  }
	});
}
function changeOrder(t) {
	byprice = t;
	showFilter('', '');
	$('.byprice').click(function() { changeOrder('asc'); });
}

function delCompare(id) {
	var checked = 'false';
	$.ajax({
	  url: '/products/compare/',
	  data: { box: 'true', checked: checked, id: id },
	  dataType: "script"
	});
	$("#checkb-"+id).attr('checked', '');
}

function compare() {
	showLoading();
	$.ajax({
	  url: '/products/compare/',
	  dataType: "script",
	  complete: function() {
		hideLoading();
	  }
	});
}
var leasing = {
	curCreditor:null,
	init:function() {
		$('#a_bulbank').click(function(){
			leasing.fixExtraMonths(false);
			leasing.calc();
			$('#Leasing_oldClient').parent().hide();
		});
		$('#a_tbi').click(function(){
			leasing.fixExtraMonths(true);
			$('#Leasing_oldClient').parent().show();
			leasing.calc();
		});
		$('#Leasing_months, #Leasing_downPayment').change(leasing.calc);
		$('#Leasing_oldClient').click(leasing.calc);
		$('input[name="first_vnoska"]').keyup(leasing.calc);
		leasing.calc();
	},
	fixExtraMonths:function(disabled) {
		$('#Leasing_months option').each(function() {
			if (this.value == 15 || this.value == 18 || this.value == 48) {
				this.disabled = disabled;
			}
		});		
	},
	calc: function() {
		var period = $('#Leasing_months').val(),
			amount = $('#Leasing_totalAmount').val(),
			$first = $('input[name="first_vnoska"]'),
			$monthly = $('#Leasing_monthlyFee');
		if ($first.length) {
			amount -= ($first.val()==''? 0 : parseFloat($first.val()));
		}
		if ($('#leasing-type').val() == 'bulbank') {
			$monthly.val(leasing.calcUni(period, amount));
		} else {
			res = leasing.calcTbi(period, amount, $('#Leasing_oldClient').is(':checked'));
			$monthly.val(res[0]);
		}
	},
	calcTbi: function(period, amount, oldClient) {
		var interes = {
			'new': {
				3: 1.038,
				6: 1.048,
				9: 1.080,
				12:1.098,
				24:1.188,
				36:1.278
			},
			'old': {
				3: 1.0247,
				6: 1.0312,
				9: 1.0520,
				12:1.0637,
				24:1.1222,
				36:1.1807
			}
		};
		oldClient = oldClient || false;
		var i = oldClient == false? interes['new'] : interes['old'];
		if (!i[period]) return ['', ''];
		return [
		    (Math.round(((amount * i[period])/period + (amount*0.0083+2.8))*100)/100).toFixed(2),
		    (Math.round(((amount * i[period]) + (amount*0.0083+2.8)*period)*100)/100).toFixed(2)
		];
	},
	calcUni: function(period, amount) {
		if (typeof onePercent == 'undefined')
			onePercent = false;
		var interes = {
			3: 1.061,
			6: 1.108,
			9: 1.157,
			12:1.206,
			24:1.417,
			36:1.650
		};
		var interesOnePercent = {
			12:0.09333727,
			15:0.07666272,
			18:0.06556213,
			24:0.05166863,
			36:0.03777514,
			48:0.03084023
		};
		if (onePercent == false || period < 12)
			return Math.round(amount * interes[period] / period);
		else
			return (amount * interesOnePercent[period]).toFixed(2); 
	},
};
