//open banners in new window
$(document).ready(function(){

$('.bannerNewWindow').click(function(){
	window.open(this.href);
	return false;
});

if (!$('#commentErrors').attr("id")){

	$('#add_comment').hide();
	
} else {

	$('#addNewComment').hide();

}

$('#addNewComment').click(function(){
	
	$(this).hide();
	$("#add_comment").slideDown("medium",function(){
	
		$('body').scrollTo('100%',"800");
	
	});
	
	return false;
		
});

if ($("#commentErrors").attr("id")){

	$('body').scrollTo('100%');

}


});

$(document).ready(function() {
	$('.reqField').blur(function(){
	
		if ($(this).val() != ""){
		
			$(this).removeClass();
			$(this).siblings('.reqInput').slideUp('medium');
		
		}
	
	});
});

//Acordion tree
$(document).ready(function() {

	$('.level1subitems').hide();
	$('.level2subitems').hide();
	
	var activeItem = $('.activeCategory').closest('.menuTree');
	
	activeItem.find('.level1Header span').addClass('openTree1');
	activeItem.find('.level1subitems').show();
	
	$('.activeCategory').closest('.level1subitems').find('.level2Header span').addClass('openTree2');
	$('.activeCategory').closest('.level2subitems').show();
	
	
	$('.level1Header').click(function(){
	
		$(this).find('span').toggleClass("openTree1");
		
	});
	
	/*
	$('.level1Header2').click(function(){
	
		$(this).find('span').toggleClass("openTree1");
		$(this).siblings('.level1subitems').slideToggle("fast");
		
		return false;
	
	});
	*/

	
	$('.level2Header').click(function(){
	
		$(this).find('span').toggleClass("openTree2");
		$(this).siblings('.level2subitems').slideToggle("fast");
	
	});

});

//Acordion tree
$(document).ready(function(){

	$('.qPlus,.qMinus').click(function(){
	
		var currentItem = $(this).siblings('.productQuantity');
		var currentValue = parseInt(currentItem.val());
		
			if ($(this).attr('class') == "qPlus"){
			
				currentItem.attr("value",currentValue+1);
			}	

		if (currentValue > 1){		
			
			if ($(this).attr('class') == "qMinus"){
			
				currentItem.attr("value",currentValue-1);
			}
		
		}
		
		return false;
	});

});

//Acordion tree
$(document).ready(function(){

	var orgImg = $('.productImage').find('img').attr('src');

	$('.smallProductImage').hover(function(){
	
		var imgSrc = $(this).find('img').attr('src');
		
		$('.productImage').find('img').attr('src',imgSrc);
	
	},
	
	function(){
			
			$('.productImage').find('img').attr('src',orgImg);
			
			}
	
	)

});

//Clear input on click
$(document).ready(function() {
	$('.clear_field').click(function() {
	var orig_value = $(this).val();
	$(this).val("");
	$(this).blur(function () {
	     if ($(this).val()==""){
         $(this).val(orig_value);
		 }
    });
	});
});

//mainmenu event
$(document).ready(function(){  
$('.button_small').mousedown(function(){
	$(this).css("background-position","-52px 0");
});
$('.button_medium').mousedown(function(){
	$(this).css("background-position","-123px 0");
});
$('.button_small').mouseleave(function(){
	$(this).css("background-position","0px 0px");
});
$('.to_cart,.button_big').mousedown(function(){
	$(this).css("background-position","-147px 0");
});
$('.to_cart,.button_big').mouseleave(function(){
	$(this).css("background-position","0px 0px");
});
$('.to_cart,.button_medium').mouseleave(function(){
	$(this).css("background-position","0px 0px");
});
});

//calc price
$(document).ready(function(){ 

	checkImgSize($('#weight'),0);
 
	$('#product_img').change(function(){ calcPrice($('#weight')); });
	$('#quantity').keyup(function(){ calcPrice($('#weight')); });
	$('#weight').change(function(){ checkImgSize($(this),"user"); calcPrice($(this)); });
	$('#img_size').change(function(){ calcPrice($('#weight')); });
	$('#product_img').click(function(){ $(this).val(""); calcPrice($('#weight')); });

});

var calcPrice = function(price_option){
	
	var is_image;
	var img_item = $('#img_size').find(':selected').attr('id');
	var img_price = price_option.find(':selected').attr(img_item);
	if (!img_price) img_price = $('#oneWeight').attr(img_item);
	
	//var price_txt = price_option.find(':selected').attr('class');
	
	var add = 0;
	
	var price_o = price_option.find(':selected').attr('id');
	
	if (price_o) price_o = price_o.substr(1)
	
	var quantity = parseInt($('#quantity').val());
	
	if (!price_o) price_o = $('#normal_price').val(); 
	
	if (img_price != "" && $('#product_img').val()){

			add = img_price;
			is_image = 1;
	}	
	
	/*
	if ($('#product_text').attr("id")){
	
		if ($('#product_text').val() != "" && is_image != 1){
	
			add = price_txt; 
		}
	}
	*/
	
	var price = (( parseFloat(price_o) + parseFloat(add)) * quantity).toFixed(2);
	
	$('#man_price').text(price);
	
}
	

//check image size values
checkImgSize = function(item,call){

	$('#img_size').find(':first-child').attr("selected", "selected");
	
	var n_count = new Array();

	if (item.find(':selected').attr('A5') == "" || $('#oneWeight').attr('A5') == ""){ $('#A5').attr("disabled","disabled").hide(); n_count.push("A5"); } else { $('#A5').removeAttr("disabled").show(); }
	if (item.find(':selected').attr('A4') == "" || $('#oneWeight').attr('A4') == ""){ $('#A4').attr("disabled","disabled").hide(); n_count.push("A4"); } else { $('#A4').removeAttr("disabled").show(); }
	if (item.find(':selected').attr('A3') == "" || $('#oneWeight').attr('A3') == ""){ $('#A3').attr("disabled","disabled").hide(); n_count.push("A3"); } else { $('#A3').removeAttr("disabled").show(); }
	if (item.find(':selected').attr('A2') == "" || $('#oneWeight').attr('A2') == ""){ $('#A2').attr("disabled","disabled").hide(); n_count.push("A2"); } else { $('#A2').removeAttr("disabled").show(); }
	if (item.find(':selected').attr('A1') == "" || $('#oneWeight').attr('A1') == ""){ $('#A1').attr("disabled","disabled").hide(); n_count.push("A1"); } else {$('#A1').removeAttr("disabled").show(); }

	//if no image prices hidde image option
	if (n_count.length == 5){
	
		if (call == "user") $('#addImage').slideUp("medium"); else $('#addImage').hide();
		$('#product_img').val("");
	
	} else {
	
		$('#addImage').slideDown("medium");
	
	}
}

//Adress form
$(document).ready(function(){

	if ($('#sameaddress').is(':checked')){ 
		
		$('#addressForm2').hide();
	
	} else {
	
		$('#addressForm2').show();
	
	}
	
	$('#sameaddress').click(function(){ $('#addressForm2').slideToggle("fast"); });

});

//Shipping date & time
$(document).ready(function(){

		dateTimeSelection();

	$('#shpping_area').change(function(){

		dateTimeSelection();
	
	});

});

var dateTimeSelection = function(){

		if ($('#shpping_area').find(":selected").attr('class') == "shpping_dateF"){ 
			
				$('#shpping_dateF').slideDown("fast"); 
				$('#dateReq').val("1"); 
			
			} else { 
			
				$('#shpping_dateF').hide();
				$('#dateReq').val("0"); 
			
			}
		
		if ($('#shpping_area').find(":selected").attr('rel') == "shpping_timeF"){ 
		
			$('#shpping_timeF').slideDown("fast");  
			$('#timeReq').val("1"); 
		
		} else { 
		
			$('#shpping_timeF').hide(); 
			$('#timeReq').val("0");
			
		}

}

$($.date_input.initialize);

//date
$.extend(DateInput.DEFAULT_OPTS, {
  stringToDate: function(string) {
    var matches;
    if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
      return new Date(matches[1], matches[2] - 1, matches[3]);
    } else {
      return null;
    };
  },

  dateToString: function(date) {
    var month = (date.getMonth() + 1).toString();
    var dom = date.getDate().toString();
    if (month.length == 1) month = "0" + month;
    if (dom.length == 1) dom = "0" + dom;
    return date.getFullYear() + "-" + month + "-" + dom;
  }
});


//simple modal
jQuery.fn.modal = function() {

	return this.each(function(){
	
		var myModal = $(this);
	
		var ah = $(this).height();
        var wh = $(window).height();
		
        nh = (wh/2) - (ah/2);
 
        var aw = $(this).width();
        var ww = $(window).width();
        nw = (ww/2) - (aw/2);
 
        // Trim body height, append overlay, and disable scrolling (this can be undone with a custom close button)
        $("body").height(wh).css('overflow','hidden').append('<div class="overlay"></div>');
        $(".overlay").fadeTo("slow", 0.5);
		
        $(this).css({'top':nh, 'left':nw}).fadeIn("slow");
		
		$(".overlay,#modalClose").click(function(){ 
		
			$(".overlay").fadeOut("slow"); 
			myModal.hide(); 
			$("body").css('overflow','auto');
		
		});
		
		
	});
	
};

$(document).ready(function(){

		 $('#cartModal').modal();

		//to cart disabled message
		/*
		$('.to_cart').click(function(e) {
			e.preventDefault();
			
			var content = $("#info_link").val();
			
			$.nyroModalManual({
				ajax: {url: content}
			});
			return false;
		});
		*/

});

