var modwin2 = null;
var modwin3 = null;
var modwin4 = null;

// CUFON replaces
Cufon.replace('#topmenu a',{ hover: true });
Cufon.replace('#topundermenu a',{ hover: true });
Cufon.replace('.forsideBannerSlider *');
Cufon.replace('.forsideBannerSlider a',{ hover: true });
Cufon.replace('h1');
Cufon.replace('h2');
Cufon.replace('h3');
Cufon.replace('h4');
Cufon.replace('#brodkrumme_step');
Cufon.replace('.produktListeStep1_item_bund_h a');
Cufon.replace('.produktListeStep2_variant_bund');
Cufon.replace('.samlet_pris');
Cufon.replace('.produktListeStep2_relaterede_item_bund');
Cufon.replace('.produktListeStep2_relaterede_til_kassen a');
Cufon.replace('#shop_steps div');
Cufon.replace('.shop_paging_v');
Cufon.replace('.shop_paging_h');
Cufon.replace('#lev1 li.first');
Cufon.replace('.forside_produktliste_knap_koeb a');

jQuery(document).ready(function() {
	jQuery('img').pngFix();
	initCo3Gallery();
	hidePalleInfo();
	initPostnrPriceCheck();
        initKurvPris();
        dkkort();

	var lukFpBanner = $(".forsideBannerSlider_luk img");
	lukFpBanner.css("cursor", "pointer");
	lukFpBanner.toggle(
		function () {
			lukForsideBanner();
			lukFpBanner.attr("src","/files/billeder/easystone/gfx/forside_aaben.png");
		},
		function () {
			aabenForsideBanner();
			lukFpBanner.attr("src","/files/billeder/easystone/gfx/forside_luk.png");
		}
	);

	var lukFpBannerNy = $(".forsideBannerSlider_lukNy img");
	lukFpBannerNy.css("cursor", "pointer");
	lukFpBannerNy.toggle(
		function () {
			lukForsideBannerNy();
			lukFpBannerNy.attr("src","/files/billeder/easystone/gfx/forside_aaben.png");
		},
		function () {
			aabenForsideBannerNy();
			lukFpBannerNy.attr("src","/files/billeder/easystone/gfx/forside_luk.png");
		}
	);


	if(jQuery("#restparti").length > 0) { 
		restparti(); 
		jQuery("#restparti input").bind("click",function(){
			jQuery("#restparti").Co3ModalWindowClose();
			jQuery.cookie("restparti", "OK");
		});
	}
});


jQuery(window).load(function(){
	setTimeout(function(){initForsideBanner();}, 650);
	setTimeout(function(){initForsideBannerNy();}, 650);
});



var tout = "";
function initCo3Gallery() {
	var imageLists = $(".imageList");
	var settings = {
		imageInformationHeight: 60,
		imageInformationTemplate: '[description]<br /><br />Billede [imagenumber] af [numberofimages]',
		playSpeed: 4000,
		onClosed: function() { clearTimeout(tout); }
	}
	imageLists.each(function() {
		var imageList = jQuery(this);
		 jQuery(this).children("img").CreateGallery(settings)
	});
}
function startCo3Slideshow() {
	jQuery(".imageList img:first").click();
//	tout = setTimeout( function (){	jQuery.Co3Gallery.togglePlay() },5500);
}


function startCo3Slideshow2(el,picNo) {
if(picNo != "2" && picNo != "3") { jQuery(el).parent().parent().find(".imageList img:first").click(); }
if(picNo == "2") { jQuery(el).parent().parent().find(".imageList img:eq(1)").click(); }
if(picNo == "3") { jQuery(el).parent().parent().find(".imageList img:eq(2)").click(); }
}


function indtastPostnr(el,tal) {
  if(jQuery("#HasPrices").attr("hasprice") === "False") {
    jQuery(el).parent().parent().parent().parent().find(".produktListeStep1_item_top_h_txt").css("display","none");
    jQuery(el).parent().parent().parent().parent().find(".produktListeStep1_item_top_h_postnr").css("display","block");
    jQuery(el).removeAttr("onclick");

    jQuery(el).bind("click", function(){
      validatePostnrClick(tal, this, "func");
      tal = 0;
    });
  } else { 
    var gplgrp = jQuery(el).parent().parent().parent().parent().find(".gpl_GroupID").val();
    location.href='Default.aspx?ID=2905&GroupID='+gplgrp;
  }
}


function beregnPris(el) {
	var m2pris = $(el).findParent(".produktListeStep2_variant").find(".produktPrisUMoms").val();
//	var m2pris = $(el).parent().parent().parent().parent().parent().parent().parent().find(".produktPris").val();
	m2pris = m2pris.replace(/\./g,"");
	m2pris = m2pris.replace(/,/g,".");
	var pris = $(el).val() * m2pris;
	pris = pris * 1.25;
	pris = pris * 100;
	pris = parseInt(pris);
	pris = pris/100;
	pris = pris+"";
	pris = pris.replace(/\./g,",");
        if(pris.indexOf(",") < 0) { pris = pris + ",00"; }
        if(pris.split(",")[1].length === 1) { pris = pris + "0"; }

	$(el).parent().parent().parent().parent().parent().parent().parent().parent().find(".samlet_pris").text(""+pris);
	Cufon.replace('.samlet_pris');
}


function updateCart(el,elm) {
	elmVal = $("#"+elm).val();
	orgVal = parseInt(elmVal);
	if(elmVal != "0" && elmVal != "") {
		var cart = $(el).parent().parent().parent().parent().find("form");
		var cartName = $(el).parent().parent().parent().parent().find("form").attr("name");
		cart.attr("action","/Default.aspx?ID=2904&step=1")
		cart.submit();
	} else {
		alert("Antal skal v\u00E6re st\u00F8rre end 0.");
alert(orgVal)
$("#"+elm).val(orgVal);
	}
}


function hidePalleInfo() {
	$(".ordre_detaljer_tr_PROD3").find(".ordre_detaljer_td3").find("input").removeAttr("onblur")
//	$(".ordre_detaljer_tr_PROD3").find(".ordre_detaljer_td3").find("a:eg(1)").css("display","none");
	$(".ordre_detaljer_tr_PROD3").find(".ordre_detaljer_td3").find(".imgOpdaterAntal").css("display","none");
	$(".ordre_detaljer_tr_PROD3").find(".ordre_detaljer_td6").html("&nbsp;");
}


function fOrdreSpg1(txt) {
	if(txt.toString() === "" && jQuery("#ordreSpg1andet").val() != "") {
		$(".tdOrdreSpg1 input").val(jQuery("#ordreSpg1andet").val());
	} else { 
		$(".tdOrdreSpg1 input").val(txt);		
	}
}


function fOrdreSpg2(txt) {
	if(txt.toString() === "" && jQuery("#ordreSpg2andet").val() != "") {
		$(".tdOrdreSpg2 input").val(jQuery("#ordreSpg2andet").val());
	} else { 
		$(".tdOrdreSpg2 input").val(txt);		
	}
}


function initForsideBanner() {
	$(".forsideBannerSlider").animate({left:"0px"}, 200);
}


function lukForsideBanner() {

	$(".forsideBannerSlider").animate({left:"-370px"}, 100);
}


function aabenForsideBanner() {
	$(".forsideBannerSlider").animate({left:"0"}, 100);
}



function initForsideBannerNy() {
	$(".forsideBannerSliderNy").animate({left:"0px"}, 200);
}


function lukForsideBannerNy() {

	$(".forsideBannerSliderNy").animate({left:"-370px"}, 100);
}


function aabenForsideBannerNy() {
	$(".forsideBannerSliderNy").animate({left:"0"}, 100);
}



/*
var brofasteOer = ["1111","2222","3333"];

function postnrExist(postnr) {
	for(i=0; i<brofasteOer.length; i++) { 
		if(brofasteOer[i] === postnr) { return true; }
	}
	return false;
}
*/

function validatePostnr(el,elform,evt) {
	evt = window.event ? window.event : evt;

	if (evt.keyCode == 13) {
		var postnr = $(el).val();
		if(postnr == "") {
			alert("Du skal indtaste dit postnummer.");
		} else {
			if(!objScript.FourNumberCheck(postnr)) {
				alert("Postnummer skal indholde 4 cifre.");
			} else { 
				elform.submit();
			}
		}

	} else {
		return true;
	}
}


function validatePostnrClick(tal,el,fra) { 
	if(tal != 1) {
		if(fra === "func") {
			var postnr = jQuery(el).parent().parent().parent().parent().find(".zipcode").val();
			var elform = jQuery(el).parent().parent().parent().parent().find("form");
		} else if(fra === "modwin") {
			var postnr = jQuery(el).parent().parent().parent().find(".zipcode").val();
			var elform = jQuery(el).parent().parent().parent().find("form");
		} else {
			var postnr = jQuery(el).parent().find(".zipcode").val();
			var elform = jQuery(el).parent().parent().find("form");
		}
		if(postnr == "") {
			alert("Du skal indtaste dit postnummer.");
		} else {
			if(!objScript.FourNumberCheck(postnr)) {
				alert("Postnummer skal indholde 4 cifre.");
			} else { 
				elform.submit();
			}
		}
	}
} 


var varLoopElmNr = 0;
var ProdVariantCombinations = "";

//Finds the closest parent that meets the selector requirements
jQuery.fn.findParent = function(selector) {
  var correctParent = this.eq(0).parent();
  while (correctParent.filter(selector).length === 0) {
    correctParent = correctParent.parent();
    if (correctParent.filter("html").length > 0) {
      correctParent = correctParent.slice(0, correctParent.length - 1);
      break;
    }
  }
  return correctParent;
};


function LaegIkurv(prodID,el) {
  var antalProds = jQuery(el).findParent(".produktListeStep2_varianter_loop").find(".quantity").val();
  if(antalProds <= 0 || antalProds.toString() === "") {
    alert("Husk at v\u00E6lge antal."); return true;
  }

  if(jQuery(el).findParent(".produktListeStep2_varianter_loop").find(".vargrp_0").length > 0) {
    var selValue = jQuery(el).findParent(".produktListeStep2_varianter_loop").find(".vargrp_0").val().toString();
    if(selValue === "" || selValue === "valg") {
      alert("Husk at v\u00E6lge variant inden du l\u00E6gger i kurven.")
    } else {
      //jQuery(el).findParent(".produktListeStep2_varianter_loop").find("form").submit();

       jQuery("#modalContent03").showBlock({
          maxOpacity: 0.4,
          blockBgColor: "white",
          blockZIndex: 1000,
          closeOnBlockClick: false,
          animateTime: 100
        });

        // L?gger i kurven og refresher siden
        jQuery("#knap_kurv_fortsaet").click(function() {
          jQuery(el).findParent(".produktListeStep2_varianter_loop").find("form").submit();
          return false;
        });

        // L?gger i kurven og redirecter til indk?bskurven
        jQuery("#knap_kurv_bet").click(function() {
          jQuery("#redirect"+prodID).val() = "Default.aspx?ID=2904";
          jQuery(el).findParent(".produktListeStep2_varianter_loop").find("form").submit();
          return false;
        });

    }
  } else { 

       jQuery("#modalContent03").showBlock({
          maxOpacity: 0.4,
          blockBgColor: "white",
          blockZIndex: 1000,
          closeOnBlockClick: false,
          animateTime: 100
        });

        // L?gger i kurven og refresher siden
        jQuery("#knap_kurv_fortsaet").click(function() {
          jQuery(el).findParent(".produktListeStep2_varianter_loop").find("form").submit();
          return false;
        });

        // L?gger i kurven og redirecter til indk?bskurven
        jQuery("#knap_kurv_bet").click(function() {
          document.getElementById("redirect"+prodID).value = "Default.aspx?ID=2904";
          jQuery(el).findParent(".produktListeStep2_varianter_loop").find("form").submit();
          return false;
        });

  }
}




function initPostnrPriceCheck() {
  if(jQuery("#HasPrices").attr("hasprice") === "False") {

    jQuery("#modalContent02").showBlock({
      maxOpacity: 0.4,
      blockBgColor: "#666666",
      blockZIndex: 1000,
      closeOnBlockClick: false,
      animateTime: 100
    },function(block){ modwin2 = block; modwin3 = block; });

  }
}

function initKurvPris() {
  var antKurvVarer = jQuery("#kurv_antVarer").text();
  if(!antKurvVarer) { jQuery("#top_kurv_pris").text("0,00"); }
}

var groupId = "";

function aabenDKkort(el,grpId) {
  groupId = grpId;
  if(jQuery("#HasPrices").attr("hasprice") === "False") {
    var settings={ 
      opacity: 0.6, 
      background: '#fff', 
      zIndex: 999, 
      animateTime: 300, 
      closeOnBlockClick: true,
      onClosed: function() {
        jQuery("#modalContent06").css("display","none");
      }
    };
    jQuery('#modalContent06').css("display","block").Co3ModalWindowOpen(settings);
  } else {
    var gplgrp = jQuery(el).parent().parent().parent().parent().find(".gpl_GroupID").val();
    location.href='Default.aspx?ID=2905&GroupID='+gplgrp;
  }
}

function dkkort() {
  if(jQuery("#dkkort").length > 0) {
    jQuery("#dkkorts1").mouseover(function() {
      jQuery("#dkkort").attr("src","/files/billeder/easystone/gfx/dkkort_jylland.jpg");
    });
    jQuery("#dkkorts2").mouseover(function() {
      jQuery("#dkkort").attr("src","/files/billeder/easystone/gfx/dkkort_sjaelland.jpg");
    });
    jQuery("#dkkorts1").click(function() {
      dkkortGO("7430");
    });
    jQuery("#dkkorts2").click(function() {
      dkkortGO("1000");
    });
    jQuery("#dkkorts1").mouseout(function() {
      jQuery("#dkkort").attr("src","/files/billeder/easystone/gfx/dkkort.jpg");
    });
    jQuery("#dkkorts2").mouseout(function() {
      jQuery("#dkkort").attr("src","/files/billeder/easystone/gfx/dkkort.jpg");
    });
    jQuery("#dkkort").mouseout(function() {
      jQuery("#dkkort").attr("src","/files/billeder/easystone/gfx/dkkort.jpg");
    });
  }
}

function dkkortGO(lev) {
  jQuery(".zipcode").val(lev);
  jQuery("#postnr_input"+groupId).submit();
}

function restparti() {
  if(!jQuery.cookie("restparti")) {
    var settings={ 
      opacity: 0.6, 
      background: '#fff', 
      zIndex: 999, 
      animateTime: 300, 
      closeOnBlockClick: false,
      onClosed: function() {
        jQuery("#restparti").css("display","none");
      }
    };
    jQuery('#restparti').css("display","block").Co3ModalWindowOpen(settings);
  }
}
