jQuery(document).ready(function($) {
	getSummWithoutDelivery();
	setDelivery();
	$("#menu_categories>li>a").click(function() {
		var li = $("#menu_categories").find("li");
		for (var i=0; i<li.length; i++ ) {
			var div = $(li[i]).find("a").attr("rel");
			if($(li[i]).attr('class') == 'active') {
				$(li[i]).removeClass("active");
				$("#" + div + ":visible").hide();
			} else {
				$(li[i]).addClass("active");
				$("#" + div + ":hidden").show();
			}
		}
		return false;
	});

	$("#login_form_link").click(function() {
		var login_form = $("#login_form");
		login_form.toggle();
		/*if(login_form.is(':visible')) {
			login_form.hide();
		} else {
			login_form.show();
		}*/
		return false;
	});

	$(".product").each(function(i) {
		var block = $(this).find(".block");
		$(block.find(".click")).click(function() {
			for (var i=0; i<block.length; i++ ) {
				var div = $(block[i]);
				if(div.attr('class') == 'block active') {
					div.removeClass("active").hide();
				} else {
					div.addClass("active").show();
				}
			}
		});
	});

    // Imitation of CSS :hover
	if($.browser.msie) {
		if($.browser.version == '6.0') {
			$('#catalog .product .block .short').hover(function() {
				$(this).addClass('ie');
			}, function() {
				$(this).removeClass('ie');
			});
		}
	}

	/*
	$(".cart").each(function(i) {
		var block = $(this).find(".block");
		$(this).find(".del").click(function() {
			if (confirm("Do you want to delete it?")) {
				alert('Send ajax-request, ROMI! See common.js');
				
				$.ajax({
					type: "GET",
					url:  "",
					error: function(request, data) {
						alert('Errrorrrr!');
					},
					success: function(data){
					},
					dataType: 'json'
				});
				
				block.fadeOut(500, function() {
					block.remove();
				});
			}
		});
	});
	*/
	$('#nav').droppy({speed: 100});

	/*$('#menu ul li').hover(
        function() {
            $(this).addClass("active");
            $(this).find('ul').slideDown();
        },
        function() {
            $(this).removeClass("active");        
            $(this).find('ul').slideUp('fast');
        }
    );*/

});




function deleteItem(id){
	var f = document.getElementById('form_cart');
	f.elements['shop_action'].value= 'DELETE';
	f.elements['product_id'].value= id;
	f.submit();
}

var oslo_0 = null;
var oslo_1 = null;

var km = null;

var addr = null;
var zip = null;
var city = null;
var comm = null;

var tot_price = null;
var tot_price_element = null;

var mva_price = null;
var mva_price_element = null;

var del_price = null;
var delivery_summ = null;
var tot_summ = null;
var discount_price_element = null;
var discount_price = 0;

var summ_without_delivery = 0;

function getSummWithoutDelivery(){
	try{
	initDelivery();
		summ_without_delivery = parseFloat(tot_summ.innerHTML);
		tot_price = parseFloat(tot_price_element.innerHTML);
		mva_price = parseFloat(mva_price_element.innerHTML);
		if(discount_price_element){
			discount_price = parseFloat(discount_price_element.innerHTML);
		}
	}
	catch(e){}
}

function initDelivery(){
	try{
	oslo_1 = document.getElementById('oslo_1');
	oslo_0 = document.getElementById('oslo_0');
	km = document.getElementById('km');
	delivery_summ = document.getElementById('delivery_summ');
	tot_price_element = document.getElementById('tot_price');
	mva_price_element = document.getElementById('mva_price');
	tot_summ = document.getElementById('tot_summ');
	discount_price_element = document.getElementById('discount_price');
	}
	catch(e){alert(e.message)}
}

function setDelivery(){
	try{
	var choice = document.getElementById('delivery_choice');
	if(!oslo_0){
		initDelivery();
	}
	var li = jQuery("#frakt_div").find("input");
	for (var i=0; i<li.length; i++ ) {
		li[i].disabled = choice.checked;
	}
	
	document.getElementById("std_d").disabled = !choice.checked;
	document.getElementById("std_m").disabled = !choice.checked;
	document.getElementById("std_y").disabled = !choice.checked;
	document.getElementById("stt_d").disabled = !choice.checked;
	document.getElementById("stt_m").disabled = !choice.checked;
	document.getElementById("stt_y").disabled = !choice.checked;
	
	if(choice.checked){
		delivery_summ.innerHTML = '0,-';
		tot_summ.innerHTML = summ_without_delivery;
		mva_price_element.innerHTML = mva_price + ',-';
		return;
	}
	else{
		var dp = parseInt(tot_price/10);
		if(dp < 600){
			dp = 600;
		}
		
		if(oslo_1.checked){
			//delivery_summ.innerHTML = parseInt(summ_without_delivery/10) + ',-';
			delivery_summ.innerHTML = dp + ',-';
			var mva = parseInt(tot_price + dp - discount_price)/4;
			mva_price_element.innerHTML = mva + ',-';
			//tot_summ.innerHTML = parseInt(summ_without_delivery/10 + summ_without_delivery) + ',-';
			tot_summ.innerHTML = parseInt(tot_price + mva + dp - discount_price) + ',-';
			//delivery_summ.innerHTML = 88
		}
		else{
			if(isNaN(parseInt(km.value))){
				km.value = 0;
			}
			else{
				km.value = parseInt(km.value);
			}
			//if(parseInt(d_d.value) != d_d.value || parseInt(d_m.value) != d_m.value || parseInt(d_y.value) != d_y.value || parseInt(d_h_f.value) != d_h_f.value || parseInt(d_m_f.value) != d_m_f.value){
			if(km.value < 11){
				//alert('Invalid Distance');
				delivery_summ.innerHTML = dp + ',-';
				
				var mva = parseInt(tot_price + dp - discount_price)/4;
				mva_price_element.innerHTML = mva + ',-';
				
				tot_summ.innerHTML = parseInt(tot_price + mva + dp - discount_price) + ',-';
			}
			else{
				dp += (km.value - 10)*50;
				delivery_summ.innerHTML = parseInt(dp) + ',-';
				var mva = parseInt(tot_price + dp - discount_price)/4;
				mva_price_element.innerHTML = mva + ',-';
				tot_summ.innerHTML = parseInt(tot_price + mva + dp - discount_price) + ',-';
			}
		}
	}
	}
	catch(e){}
	//comm.disabled = choice.checked;
}

function setN(d){

}

function setTime(){
	return;
	d_h_f = document.getElementById('d_h_f');
	d_m_f = document.getElementById('d_m_f');
	d_h_t = document.getElementById('d_h_t');
	d_m_t = document.getElementById('d_m_t');
	
	r_h_f = document.getElementById('r_h_f');
	r_m_f = document.getElementById('r_m_f');
	r_h_t = document.getElementById('r_h_t');
	r_m_t = document.getElementById('r_m_t');
	
	d_h_f.value = isNaN(parseInt(d_h_f.value)) ? 0 : d_h_f.value;
	d_m_f.value = isNaN(parseInt(d_m_f.value)) ? 0 : d_m_f.value;
	r_h_f.value = isNaN(parseInt(r_h_f.value)) ? 0 : r_h_f.value;
	r_m_f.value = isNaN(parseInt(r_m_f.value)) ? 0 : r_m_f.value;
	
	d_h_t.value = isNaN(parseInt(d_h_f.value)) ? 0 : (parseInt(d_h_f.value) + 3 > 24 ? parseInt(d_h_f.value) + 3 - 24 : parseInt(d_h_f.value) + 3);
	d_m_t.value = isNaN(parseInt(d_m_t.value)) ? 0 : d_m_f.value;
	
	r_h_t.value = isNaN(parseInt(r_h_f.value)) ? 0 : (parseInt(r_h_f.value) + 3 > 24 ? parseInt(r_h_f.value) + 3 - 24 : parseInt(r_h_f.value) + 3);
	r_m_t.value = isNaN(parseInt(r_m_t.value)) ? 0 : r_m_f.value;
}
