$(document).ready(function() {

	$('.link, .link-on').click(function () {
		var id = $(this).attr('id');

		$('.link, .link-on').attr("class","link");

		$("#"+id).attr("class","link-on");

		$.get("ajax/productlist.php", {artId: id}, function (data) {
			if(data){
				$("td.article").html(data);
			}
		});

	});

	$('#start_images').cycle({ 
    	fx:    'fade', 
    	speed:  1500,
		after: changeSub
 	});

});

function changeSub() {
	id = this.id;
	image = this.alt;
	last_id = this.name;

	src = $('#'+last_id).attr('src');

	if(src == undefined){}else{
		src = src.replace('-on.png', '.png');

		$('#'+last_id).attr('src', src);
	}

	$('#'+id).attr('src', 'images/menu/'+image);
}

function myAlert (str) {
	var tmp = document.createElement("textarea");
	tmp.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	alert(tmp.value);
}


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 testFunc( value ) {

	alert(value);

	var art = $('.art_'+value).attr("id");

	$(".link").css("color","#FFFFFF");
	$(".link-on").css("color","#FFFFFF");

	$("#"+art).css("color","#00a4e8");

	$.get("ajax/productlist.php", {artId: art}, function (data) {
		$("td.article").html(data);
	});

}
