if (window.console == undefined) {
	window.console = { log: function() { }, dir: function() { } };
}


/************************************************/
/************ RELATERTE PRODUKTER ***************/
/************************************************/

$(function () {
var bredde = 193 ;
var antall = 0;
var tid = 1000;
var scrollReady = 1 ;
var maxAntall =   $('.wide > div').size() ;

  $('.vkright').click(function(){
      if (scrollReady == 1){
          scrollReady = 0;
          antall = antall + 3;
          if(antall >= maxAntall){ antall = 0;}
          posisjon = bredde * antall ;
          $('.variantliste .wide').animate({"marginLeft" : '-' + posisjon + 'px' },tid, 'swing' ,function(){
              scrollReady = 1;
          });
      }

  });
  $('.vkleft').click(function(){
      if (scrollReady == 1){
          scrollReady = 0;
          antall = antall - 3;
          if (antall <= 0){ antall = 0;}
          posisjon = bredde * antall ;
          $('.variantliste .wide').animate({"marginLeft" : '-' + posisjon + 'px' },tid, 'swing',function(){
                  scrollReady = 1;
              });
      }
  });
});


/************************************************/
/**************** HANDLEKURV ********************/
/************************************************/

/*
if (console == "undefined") {
	var console = { log: function() {} };
}
*/

var height_closed = 70;
var height_open = 120;
var height_current = height_closed;
var height_max = 414;
function toggleVisFullKurv() {
	
	if (height_current == height_closed) {	
		var th = $('#bunnkurv .handlekurv').height();	
		height_current = th + height_open;  
		if (height_current > height_max) height_current = height_max;		
		$("#vis_handlekurvspan").text("Skjul handlekurv ï¿½");
	}	
	else {
		height_current = height_closed;
		$("#vis_handlekurvspan").text("Vis handlekurv ï¿½");
	}
	$('#bunnkurv').animate({ top: hent_dimensjoner()[1]-height_current+'px' }, 400, 'swing');			
}
function positionBottomBasket() {
	
	var topcss = (hent_dimensjoner()[1] - height_current)+'px';
	$('#bunnkurv').css('top', topcss);	
}
$(window).bind('resize', positionBottomBasket); 
$(function() {
	positionBottomBasket();
});


var postnr = null;
var poststed = null;
var valgt_vare_id = null;
var valgt_assn_id = null;
var popup_legg_i_kurv = false;
var first = false;
function legg_i_kurv(vare_id, assn_id, gaa_til_kasse, callback) {
	var pulsespeed = 500;
	var bubblestay = 7000;
	
	//alert(assn_id);
	// Sjekk om det finnes en antall
	antall = 1;
	if(assn_id != 'undefined' && assn_id != null && $("#vk_antall"+assn_id).length){
		antall = $("#vk_antall"+assn_id).val();
	}
	
	$('.vk_handlekurv_container .innerkurv').load('/vk_cms/legg_i_kurv .innerkurv >*', { 'vare_id': vare_id,'antall': antall }, function (r, status, xhr) {
		
		if (gaa_til_kasse) {
			window.location = gaa_til_kasse;
			return;
		}
		/*alert(har_sett_kurven);	
		if (!har_sett_kurven) {
			toggleVisFullKurv();
			setTimeout('toggleVisFullKurv()', 2000);
			har_sett_kurven = true;			
		}*/						
		
		// hvis vi er på checkout-sida, oppdater den og
		$('.totalsum_checkout').text($('.innerkurv .totalsum').text());
		
		//if (vare_id == 0) return;		
		
		
		if (typeof callback == 'function') {
			callback();
		}
		
	});
}

function oppdater_antall(id, antall) {
	antall = antall + "";
	if (Math.abs(antall) != antall || antall.replace(/[^\d]/, '') == "") return false;
	if (antall == 0 && !confirm('Fjern produkt?')) return false;
	$('#produkt_' + id).load('/vk_cms/oppdater_antall', { 'vare_id': id, 'antall': antall }, function (r, status, xhr) {
		legg_i_kurv(0, null, false, function() {
			orig_color = $('#produkt_'+id).css('backgroundColor')
			orig_color_tot = $('#produkt_'+id).css('backgroundColor')
			$('#produkt_'+id).css('backgroundColor', '#ffffdd');
			$('.totalsum_checkout,.frakt_checkout').css('backgroundColor', '#ffffdd');
			setTimeout(function() {
				$('#produkt_'+id).css('backgroundColor', orig_color);
				$('.totalsum_checkout,.frakt_checkout').css('backgroundColor', orig_color_tot);
			}, 1500);
		});
	});
}


function vis_fraktberegning() {
	
	var ele = $("div.frakt-boks");
	var offset = $("div.frakt-boks").offset();
	//alert(offset.left + ' w:' + width);
	var newWidth = ele.width() * 2;
	var mWidth = newWidth - 34;
	$("#fraktberegning").css({
		width: newWidth,
		left: offset.left - (ele.width()/2)
	});
	$("#fraktberegning div.m").css({
		width: mWidth
	});
	$("#fraktberegning").animate({top: '-70'}, 'fast');			
}

function vis_fraktinformasjon() {
	var ele = $("div#fraktinfo");
	var offset = $("a#vis-frakt-info").offset();
	//alert(offset.left + ' w:' + width);
	var mWidth = ele.width() - 34;
	var height = ele.height() - 10;
	$("#fraktinfo").css({
		left: offset.left - (ele.width()/2)
	});
	$("#fraktinfo div.m").css({
		width: mWidth
	});
	$("#fraktinfo").animate({top: -height}, 'fast');	
}
function skjul_fraktinfo() {
	$("#fraktinfo").animate({top: '10px'}, 'fast');
}



function nullstillPostnr() {
	har_valgt_postnr = false;
	url = get_location();
	$.get(url + '?valgt-postnr=nullstill');
}


function get_location() {
	url = window.location.toString();
	url = url.replace(/#\w*?/, '');
	if (url.substr(url.length-1) != '/') {
		url += '/';
	}	
	url = url.replace(/\?.*$/,'');
	return url;	
}


function hent_dimensjoner() {
  var de = document.documentElement;
  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  arrayPageSize = [w,h];
  return arrayPageSize;
}


function gaa_til_kasse() {
	legg_i_kurv(valgt_vare_id, valgt_assn_id, '/vk_cms/gaa_til_kasse/?begrens=' + postnr);	
}



/************************************************/
/*********** KASSE / REGISTRERING  **************/
/************************************************/


var lastKnownPostkontorMapState = null;

function bringPopUpValgfrittPostkontor(url) {
//convert callbackUrl to https if page was loaded with https
	if ("https:" == document.location.protocol && url.indexOf("callbackUrl=http:") >= 0) {
	  url = url.replace("callbackUrl=http:", "callbackUrl=https:")  
	}
	
	//open popup:
	day = new Date();
	id = day.getTime();
	if(lastKnownPostkontorMapState != null) {
		url = url + '&mapstate='+lastKnownPostkontorMapState;
	}
	eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=910,height=600,left=160,top=50');");
}


function updatePostkontorvalg(enhetsid, enhetsnavn, adresse, postnr, poststed, mapstate, produktkode) {
	//populate form with result from popup:
	$("#bringFraktvalgdetaljer" + produktkode).html(enhetsnavn +' <br/>'+adresse+' <br/>'+postnr+' '+poststed);
	$("#bring" + produktkode + "radio").click();
	//in addition, some hidden fields could be populated.
	
	$(".bringFraktvalgTips").html("");
	lastKnownPostkontorMapState = mapstate;
}


// Hentet fra js.js

$(function() {
	
	$("a.jq-fancybox-info").fancybox({
	    'titleShow'     	: false,
	    'autoScale'			: false,
	    'autoDimensions'	: false,
	    'width'				: 800,
	    'height'			: 600
	});
	
	$("#se-kurv").toggle(function() {
		visPopupKurv();
	}, function() {
		skjulPopupKurv();
	});
	
	$('a.tooltip[title]').qtip({ style: { name: 'dark', tip: true } })
});


function skjulPopupKurv() {
	$("#se-kurv").removeClass('open');
	$("#popup-kurv").stop().animate({
			height: 0,
		}, '2000', function() {
			$(this).css({
				'-moz-box-shadow' : '0 0 0 #444',
				'-webkit-box-shadow' : '0 0 0 #444',
				'box-shadow' : '0 0 0 #444'
			})
		}) ;
}

function visPopupKurv() { 
	$("#se-kurv").addClass('open');
	var ele = $("#vk_kurv_boks");
	var height = ele.height() + $("#handlekurv_bunn").height();
	var width = ele.width() + 10;
	if(width < 206) {width = 206;}
	
	$("#popup-kurv").stop().css({
		'-moz-box-shadow' : '0 5px 10px #444',
		'-webkit-box-shadow' : '0 5px 10px #444',
		'box-shadow' : '0 5px 10px #444'
	}).animate({
			height: height,
		}, '2000');
}

