$('document').ready( function () {
	/* INIT */

	//var startId = $('.modell_startId').attr('value');

	//$('.'+startId).attr('style',"background: url('images/bg_shoes_sel.png')");

	// Hide confirm-overlay div...
	
	$('.popuplink').click(function () {

		var id = $(this).attr('id');

		$('.body_'+id).show();
		$('#fade').show();
		$('#popup').show();
		return false;
	});

	$('.abonnement').click(function () {

		var id = 2;

		$('.body_'+id).show();
		$('#fade').show();
		$('#popup').show();
		return false;
	});

	$('.konkurrense').click(function () {

		var id = 7;

		$('.body_'+id).show();
		$('#fade').show();
		$('#popup').show();
		return false;
	});

	$('#closepop').click(function () {

		$('.body').hide();
		$('#fade').hide();
		$('#popup').hide();
		return false;
	});

	$('.half, .small, .big, .halfbig').focus(function() {
		if($(this).attr('value') == $(this).attr('id')){
			$(this).attr('value', '');
		}
	});

	$('.half, .small, .big, .halfbig').blur(function() {
		if($(this).attr('value') == undefined){
			var name = $(this).attr('id');
			$(this).attr('value', name);
		}
	});

	$('.bestill').click(function() {

		if($('#Fornavn').val() == 'Fornavn'){
			alert('Du må fylle ut fornavn');
			return false;
		}

		if($('#Etternavn').val() == 'Etternavn'){
			alert('Du må fylle ut efternavn');
			return false;
		}

		if($('#Gate').val() == 'Gate'){
			alert('Du må fylle ut gate/vei');
			return false;
		}

		if($('#Gatenr').val() == 'Gatenr'){
			alert('Du må fylle ut gatenr');
			return false;
		}

		if($('#Gatenr').val() == 'Gatenr'){
			alert('Du må fylle ut gatenr');
			return false;
		}

		if($('#Postnr').val() == 'Postnr'){
			alert('Du må fylle ut postnr');
			return false;
		}
	
		if($('#Poststed').val() == 'Poststed'){
			alert('Du må fylle ut poststed');
			return false;
		}

		if($('#Poststed').val() == 'Poststed'){
			alert('Du må fylle ut poststed');
			return false;
		}

		if($('#E-postadresse').val() == 'E-postadresse'){
			alert('Du må fylle ut E-postadresse');
			return false;
		}

//		var formdata = $('#form').formSerialize();
//
//		$.get("xmlhttp/send_confirm.php", { data: formdata }, function(data){
//			if(data == 'ok'){
//				$('.body_2').hide();
//				$('.body_6').show();
//			}else{
//				alert('Something went wrong, try again');
//			}
//		});
//
		return true;
	});
});

function selectAll (formId, checkboxclass, mode) {
	$('#'+ formId).find('.'+ checkboxclass).each(function(i) {
		this.checked = mode;
	});
}

function imgSwap(oImg) {
	var strOver  = "_on"; // image to be used with mouse over
	var strOff = "_off";  // normal image
	var strImg = oImg.src;
	if (strImg.indexOf(strOver) != -1) {
		oImg.src = strImg.replace(strOver,strOff);
	} else {
		oImg.src = strImg.replace(strOff,strOver);
	}
}

function changeProductMainPicture( filename, imageID, pos, totalthumbs ) {
	var ih = '<a href="productimage.php?imageID=' + imageID + '" onclick="return popup(this, \'prodimage\', 420, 420, \'no\', \'no\', \'yes\')"><img alt="Klicka f&ouml;r st&ouml;rre bild" align="center" src="webimages/' + filename + '" border="0" id="productImageLarg class="productimage"></a>';

	var largeImageTD = document.getElementById('productImageLargeTD');
	largeImageTD.innerHTML = ih;

	resetThumbnails( totalthumbs );
	activateThumbnail( pos, true );
}

function resetThumbnails( totalthumbs ) {
	for (i=0; i<totalthumbs; i++) {
		activateThumbnail( i, false );
	}
}

function activateThumbnail( pos, mode ) {
	var thumbnail = document.getElementById('thumbnailBox' + pos);
	var imgThumbnail = document.getElementById('thumbnailImage' + pos);

	if (mode) {
		thumbnail.className='productImageThumbnail-selected';
		imgThumbnail.className='productImageThumbnail-selected';
	} else {
		thumbnail.className='productImageThumbnail';
		imgThumbnail.className='productImageThumbnail';
	}
}


/* Output email-addess */
function safemail (part1, part2, text) {
	var email = part1 +'@'+ part2;

	if (! text) text = email;

	document.write('<a href="mailto:'+ email +'">'+ text +'</a>');
}

function popup (url, name, width, height, status, menu, resizable) {
	var prop = 'toolbar=no,location=no,directories=no,scrollbars=yes,copyhistory=no,';
	prop += 'status='+ (status ? 'yes' : 'no') +',';
	prop += 'resizable='+ (resizable ? resizable : 'yes') +',';
	prop += 'menu='+ (menu ? 'yes' : 'no') +',';
	prop += 'width='+ (width ? width : 500) +',';
	prop += 'height='+ (height ? height : 400);

	var win = window.open(typeof(url) == 'string' ? url : url.href, name, prop);

	win.focus();
	
	return false;
}

function freshpopup (url, width, height, status, menu, resizable) {
	var time = new Date();
	var name = Math.floor((Math.random() * Math.round(time.getTime())));

	var prop = 'toolbar=no,location=no,directories=no,scrollbars=yes,copyhistory=no,';
	prop += 'status='+ (status ? 'yes' : 'no') +',';
	prop += 'resizable='+ (resizable ? resizable : 'yes') + ',';
	prop += 'menu='+ (menu ? 'yes' : 'no') +',';
	prop += 'width='+ (width ? width : 500) +',';
	prop += 'height='+ (height ? height : 400);

	var win = window.open(typeof(url) == 'string' ? url : url.href, name, prop);
	win.focus();
	
	return false;
}

function setCookie (key, value) {
	document.cookie = key + "=" + value + "; path=/";
}

function getCookie (key) {
	var cookie = document.cookie.split(';');
	var name = key + "=";

	for(var i = 0; i < cookie.length; ++i) {
		var c = cookie[i];

		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}

		if (c.indexOf(name) == 0) {
			return c.substring(name.length, c.length);
		}
	}

	return null;
}

function delCookie (key) {
	document.cookie = key + "=; expires=-86400000; path=/";
}


function selectToUrl (selObj) {
	if (selObj && selObj.value) {
		document.location.href = selObj.value;
	}

	return false;
}

function showShoe(image, pris, merke, modell_id, str_fra, str_til, colors) {
	$('.prodThumb').attr('style',"background: url('images/bg_shoes.png')");

	$('.'+modell_id).attr('style',"background: url('images/bg_shoes_sel.png')");

	$('.productImage').attr('style',"background: url('images/products/"+image+"') center bottom no-repeat");
	$('.price').html(pris+",-");

	$('.info').html(merke+", mod "+modell_id+", Sort. "+str_fra+"-"+str_til);

	$('a.addhuske').attr('id', modell_id);
	$('a.delhuske').attr('id', modell_id);

	$('a.tipsenvenn').attr('id', modell_id);

	var colors = colors;

	var color = colors.split(',');

	var printColors = new Array(color.length);

	for (var i = 0; i < color.length; i++) {
		printColors[i] = '<img src="images/colors/'+color[i]+'.gif" width="49" height="49">';
	}

	var print = printColors.join("");

	$('.colors').html(print);
}
