$(function(){
	var dudetrans = ["dissolve"]
    window.myFlux = new flux.slider('.carousel', {
		autoplay: true,
        pagination: true,
		transitions: dudetrans,
        delay: 6000
	});
});
$(document).ready(function(){
	$(".video_email_send").click(function(){
		var from = 'info';
		var to = 'info@android.com.ua';
		
		var d1 = $("#name").val();
		var d2 = $("#surname").val();
		var d3 = $("#second_name").val();
		var dT = $("#phone_number").val();
		var dE = $("#email").val();
		var dC = $("#city").val();
		var dA = $("#address").val();
		var dO = $("#info").val();
		//Required fields check
		if(!d1 || !dT){
			alert("Не все обязательные поля были заполнены.");
			$("#dle-content #name, #dle-content #phone_number").each(function(){
				var cIT = $(this).val();
				if(!cIT)
					$(this).css({"border-color" : "red", "box-shadow" : "0 0 0 red"});
			});
			return;
		}//Email check
		if(dE.length){
			var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			var xE = pattern.test(dE);
			console.log(xE);
			if(!xE){
				alert("Вы ввели адрес Email неправильно.");
				$("#email").css({"border-color" : "red", "box-shadow" : "0 0 3px red"});
				return;
			}
		}
		var subject = 'Видеонаблюдение Заказ';
		
		var content = "Фамилия: " + d3 +
		"\nИмя: " + d1 +
		"\nОтчество: " + d2 +
		"\nКонтактный телефон: " + dT +
		"\nEmail: " + dE +
		"\nГород: " + dC +
		"\nАдрес доставки: " + dA +
		"\nДополнительная информация: " + dO + "\n\n";
		var i = 0;
		
		
		var content2 = encodeURIComponent(content);
		var subject2 = "Видеонаблюдение Заказ";
		var emailData = "from=" + from + "&to=" + to + "&subject=" + subject + "&content=" + content2;
        //Email the order and if an email was given order a copy to the customer
		 $.ajax({
                type: "POST",
                url: "/engine/send2.php",
                data: emailData,
                success: function(){
					if(dE.length){
						var emailData2 = "from=" + from + "&to=" + dE + "&subject=" + subject2 + "&content=" + content2;
						
						$.ajax({
							type: "POST",
							url: "/engine/send2.php",
							data: emailData2,
							success: function(){
								window.location = "/thank_you.html";
							}
						});
					}
					else{
						window.location = "/thank_you.html";
					}
					
                }
        });
	});	
	var cirURL = window.location.protocol + "://" + window.location.host + "/" + window.location.pathname;
	if(cirURL.indexOf("new_order_form.html") >= 0){
		$(".thecart, .cart_hid").hide();
		$("#big_cart .item_conts").append($(".thecart .simpleCart_items"));
	}
	var nT, c1, c2, p1, p2;//clicking on an item with two prices brings up a window
    $('a.double_add').click(function(){
        $(this).closest('.shop_item').addClass("working");
        nT = $('.working .shop_item_name a').text();
        c1 = $('.working .item_choice1').text();
        c2 = $('.working .item_choice2').text();
        p1 = $('.working .hid_price').text();
        p2 = $('.working .hid_price2').text();
		if (!nT)
			nT = $('.working .shop_item_name').text();	
        $('.choice_window #itemNameChoice').text(nT);
        $('#choice1 h2').text("Гарантия " + c1);
        $('#choice2 h2').text("Гарантия " + c2);
		var exRate = $("#current_ex_rate").text();
		exRate = exRate.replace(",", ".");
		exRate = parseFloat(exRate);
        $('#choice1 .choice_price1').text(p1);
        $('#choice2 .choice_price2').text(p2);
		$('#choice1 .choice_price1UAH').text(Math.round(p1 * exRate) + " грн.");
        $('#choice2 .choice_price2UAH').text(Math.round(p2 * exRate) + " грн.");
		
        $('.choice_window').show();
        $('.working').removeClass("working");

    });
	$('.button_choice_1').click(function(){
			$(".thecart").show();
			simpleCart.add( 'name=' + nT + '\ (' + c1 + '\)' , 'price=' + p1 + '\'' , 'quantity=1');
			$(".choice_window").hide();
	});
	$('.button_choice_2').click(function(){
			$(".thecart").show();
			simpleCart.add( 'name=' + nT + '\ (' + c2 + '\)' , 'price=' + p2 + '\'' , 'quantity=1');
			$(".choice_window").hide();
	});
	$('.single_add').click(function(){
		$('.thecart').show();
		$(this).closest('.shop_item').addClass("working");
        nT = $('.working .shop_item_name a').text();
		if (!nT)
			nT = $('.working .shop_item_name').text();
        c1 = $('.working .item_choice1').text();
        p1 = $('.working .hid_price').text();
        $('.working').removeClass("working");
		if(c1)
			simpleCart.add( 'name=' + nT + ' \(' + c1 + '\)' , 'price=' + p1 , 'quantity=1');
		else
			simpleCart.add( 'name=' + nT , 'price=' + p1 , 'quantity=1');
		$(".thecart").show();
	});
    $('a.more_cart').click(function(){
        $('.cart_hid').hide();
        $('.thecart').show();
    });
    $('a.less_cart').click(function(){
        $('.thecart').hide();
        $('.cart_hid').show();
    });
    $('#close_choice').click(function(){
        $('.choice_window').hide();
    });
	$("#big_cart input, #big_cart #address").focus(function(){
		$(this).css({"border-color" : "#ccc", "box-shadow" : "0 0 0 #ffffff"});
	});
	$(".thecart a.checkout_link").click(function(e){
		var numOrders = $(".itemContainer").length;
		if(numOrders == 0){
			e.preventDefault();
			alert("У вас нет товар в корзине.");
		}
	});
});
