var scrolling = false;
var nrOfInfoTables;
var statusMessage;
var scrollAmount;
var scrollingUp;
var scrollTimer;
var nrOfCounts;
var timer;
var form;

window.onload = init;

function init() {
	
        form = document.forms[0];
	statusMessage = form.roller;
	document.oncontextmenu = rightClicks;
	//nrOfInfoTables = parseInt(form.infoTablesCount.value);
        	
        $("a[rel='colorbx']").colorbox({rel:"nofollow"});
        var divList = $("div[id^='infoTable']");
        nrOfInfoTables = divList.size();
        

        if (nrOfInfoTables > 0) {
                showInfoTable(0);
		getLayer('copy').style.top = '1px';
		getLayer('scroller').style.display = 'block';
		getLayer('headTable').style.width = '466px';
		


	}
	preloadImages('images/menubar/brollop_pink.jpg', 'images/menubar/gladje_pink.jpg', 'images/menubar/hem_pink.jpg');
	preloadImages('images/menubar/karlek_pink.jpg', 'images/menubar/kontakt_pink.jpg', 'images/menubar/present_pink.jpg');
	preloadImages('images/menubar/sasong_pink.jpg', 'images/menubar/sorg_pink.jpg');
	preloadImages('images/menus/arrangemang_pink.jpg', 'images/menus/arrangemang_white.jpg', 'images/menus/brudbukett_pink.jpg');
	preloadImages('images/menus/brudbukett_white.jpg', 'images/menus/buketter_pink.jpg', 'images/menus/buketter_white.jpg');
	preloadImages('images/menus/corsage_pink.jpg', 'images/menus/corsage_white.jpg', 'images/menus/kistdekoration_pink.jpg');
	preloadImages('images/menus/kistdekoration_white.jpg', 'images/menus/krukvaxter_pink.jpg', 'images/menus/krukvaxter_white.jpg');
	preloadImages('images/menus/samplantering_pink.jpg', 'images/menus/samplantering_white.jpg', 'images/menus/sorgarrangemang_pink.jpg');
	preloadImages('images/menus/sorgarrangemang_white.jpg', 'images/menus/sorgkransar_pink.jpg', 'images/menus/sorgkransar_white.jpg');
	preloadImages('images/menus/tarnbukett_pink.jpg', 'images/menus/tarnbukett_white.jpg');
	preloadImages('images/logo_glow.jpg');

	/*adjustImageSize(document.images['tableImg'], 350, 262);*/
	/*showStatusMessage();*/
	adjustRose();
        
        

	
	/*adjustImages();*/
}



function adjustRose() {
	var height = $('#BinfoTable').height()+25;
        
        $('#rose').css({'top':height})
        $('#copy').css({'top':height})
        //getLayer('rose').style.top = (getLayer('infoTable').scrollHeight + 25) + 'px';
}
function closeMenus() {
	getLayer('gladje').style.display = 'none';
	getLayer('brollop').style.display = 'none';
	getLayer('sorg').style.display = 'none';
}
function gotoLink(newMenuName) {
	if (newMenuName == 'hem') self.location.href = '/';
	else if (newMenuName == 'kärlek') self.location.href = '/produkt/4';
	else if (newMenuName == 'present') self.location.href = '/produkt/5';
	else if (newMenuName == 'säsong') self.location.href = '/produkt/6';
	else if (newMenuName == 'kontakt') self.location.href = '/kontakt';

	else if (newMenuName == 'buketter') self.location.href = '/produkt/7';
	else if (newMenuName == 'arrangemang') self.location.href = '/produkt/8';
	else if (newMenuName == 'krukväxter') self.location.href = '/produkt/9';
	else if (newMenuName == 'samplantering') self.location.href = '/produkt/10';

	else if (newMenuName == 'brudbukett') self.location.href = '/produkt/11';
	else if (newMenuName == 'corsage') self.location.href = '/produkt/12';
	else if (newMenuName == 'tärnbukett') self.location.href = '/produkt/13';

	else if (newMenuName == 'kistdekoration') self.location.href = '/produkt/14';
	else if (newMenuName == 'sorgarrangemang') self.location.href = '/produkt/15';
	else if (newMenuName == 'sorgkransar') self.location.href = '/produkt/16';

	else if (newMenuName == 'blomsterpinglan') self.location.href = '/';
	
	void(0);
}
function hideGlow() {
	getLayer('glowing').style.visibility = 'hidden';
}
function preloadImages() {
	if (document.images) {
		if (!document.preloadedImages) document.preloadedImages = new Array();
		imageIndex = document.preloadedImages.length;

		for (i = 0; i < preloadImages.arguments.length; i++) {
			if (preloadImages.arguments[i].indexOf('#') != 0) {
				document.preloadedImages[imageIndex] = new Image();
				document.preloadedImages[imageIndex++].src = preloadImages.arguments[i];
			}
		}
	}
}
function restoreImages() {
	for (i = 0; i < document.images.length; i++) {
		document.images[i].src = document.images[i].src.replace(/_pink/g, '_white');
	}
}
function rotateString(newString) {
	var rotatedString;

	if (newString.length > 1) rotatedString = newString.substring(1, newString.length) + newString.charAt(0);
	else rotatedString = newString;

	return rotatedString;
}
function showStatusMessage() {
	if (statusMessage.value == '')  {
		self.defaultStatus = 'Blomsterpinglan.se';
	} else {
		self.defaultStatus = (statusMessage.value.length > 50? statusMessage.value.substr(0, 50): statusMessage.value);
		statusMessage.value = rotateString(statusMessage.value);
		setTimeout('showStatusMessage()', 120);
	}
}
function showGlow() {
	getLayer('glowing').style.visibility = 'visible';
}
function showMenu(newMenuName) {
	closeMenus();
	clearTimeout(timer);
	getLayer(newMenuName).style.display = 'block';
}
function swapImage(newImage) {
	newImage.src = newImage.src.replace(/_white/g, '_pink');
}
function timeoutMenus() {
	timer = setTimeout('closeMenus()', 350);
}



/*--------------------*/
function adjustImages() {
	for (i = 0; i < nrOfInfoTables; i++) {
		adjustImageSize(document.images['img' + i], 71, 130);
		adjustImageSize(document.images['tableImg' + i], 200, 200);
		getLayer('transLayer' +  i).style.top = (getLayer('headText').scrollHeight + 180) + 'px';
		getLayer('watermark' +  i).style.top = (parseInt(getLayer('transLayer' +  i).style.top) + (parseInt(document.images['tableImg' + i].style.height) / 2) + 73) + 'px';
	}
}
function disableScroll() {
	if (scrolling) {
		scrolling = false;
		clearTimeout(scrollTimer);
		getLayer('scrollerUp').style.backgroundImage = getLayer('scrollerUp').style.backgroundImage.replace(/_frame/g, '_normal');
		getLayer('scrollerDown').style.backgroundImage = getLayer('scrollerDown').style.backgroundImage.replace(/_frame/g, '_normal');

		scrollAmount = 4;
		slowDowns = 14;
		nrOfCounts = slowDowns;

		if (scrollingUp) slowScrollUp();
		else slowScrollDown();
	}
}
function hideInfoTables() {
	for (i = 0; i < nrOfInfoTables; i++) {
		document.images['img' + i].style.borderColor = '#D4AAFE';
		getLayer('infoTable' + i).style.display = 'none';
	}
}
function openImageWindow(newImage, newIndexID) {
	var windowWidth, windowHeight;
	windowScrolls = false;

	if (newImage.width > screen.availWidth - 50) {
		windowWidth = screen.availWidth - 50;
		windowScrolls = true;
	} else {
		windowWidth = newImage.width;
	}

	if (newImage.height > screen.availHeight - 50) {
		windowHeight = screen.availHeight - 50;
		windowScrolls = true;
	} else {
		windowHeight = newImage.height;
	}

	windowFeatures = 'height=' + windowHeight;
	windowFeatures = windowFeatures + ', menubar=no';
	windowFeatures = windowFeatures + ', scrollbars=' + (windowScrolls? 'yes': 'no');
	windowFeatures = windowFeatures + ', status=no';
	windowFeatures = windowFeatures + ', toolbar=no';
	windowFeatures = windowFeatures + ', width=' + windowWidth;

	document.body.style.cursor = 'default';
	/*getLayer('transLayer' +  newIndexID).style.cursor = 'pointer';*/
	imageWindow = window.open('', '', windowFeatures);
	srcArray = newImage.src.split('/');

	with (imageWindow.document) {
		writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
		writeln('<html xmlns="http://www.w3.org/1999/xhtml">');
		writeln('<head>');
		writeln('<title>Blomsterpinglan.se</title>');
		writeln('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />');
		writeln('<style type="text/css">');
		writeln('<!--');
		writeln('body {');
		writeln('\tmargin: 0px;');
		writeln('\toverflow: auto;');
		writeln('}');
		writeln('html {');
		writeln('\toverflow: auto;');
		writeln('}');
		writeln('img {');
		writeln('\tcursor: pointer;');
		writeln('}');
		writeln('-->');
		writeln('</style>');
		writeln('</head>');
		writeln('<body>\n\n\n\n');
		write('<img alt="" src="images/flowers/fullsize/' + srcArray[srcArray.length - 1] + '" />');
		write('<img alt="" src="images/blomster_mark_large.gif" style="left: 5px; position: absolute; top: 5px;" />');
		write('<img alt="Klicka på bilden för att stänga fönstret" onmousedown="self.close()" src="images/pixel.gif" style="left: 0px; height: ' + newImage.height + 'px; position: absolute; top: 0px; width: ' + newImage.width + 'px" />');
		writeln('\n\n\n\n</body>');
		write('</html>');
	}
}
function resizeScroller() {
	getLayer('scrollContent').style.height = (getLayer('headText').scrollHeight + getLayer('BinfoTable').scrollHeight - 48) + 'px';
}
function rightClicks() {
	return false;
}
function scrollDown() {
	scrolling = true;
	scrollingUp = false;
	if (getLayer('scrollerDown').style.backgroundImage.search(/_normal/g) > 0) getLayer('scrollerDown').style.backgroundImage = getLayer('scrollerDown').style.backgroundImage.replace(/_normal/g, '_frame');
	getLayer('scrollContent').scrollTop += 4;
	clearTimeout(scrollTimer);
	scrollTimer = setTimeout('scrollDown()', 30);
}
function scrollUp() {
	scrolling = true;
	scrollingUp = true;
	if (getLayer('scrollerUp').style.backgroundImage.search(/_normal/g) > 0) getLayer('scrollerUp').style.backgroundImage = getLayer('scrollerUp').style.backgroundImage.replace(/_normal/g, '_frame');
	getLayer('scrollContent').scrollTop -= 4;
	clearTimeout(scrollTimer);
	scrollTimer = setTimeout('scrollUp()', 30);
}
function showImage(newIndexID) {
	viewImage = new Image();
	document.body.style.cursor = 'wait';
	/*getLayer('transLayer' +  newIndexID).style.cursor = 'wait';*/
	viewImage.onload = function() {openImageWindow(viewImage, newIndexID)};
	viewImage.src = document.images['tableImg' + newIndexID].src.replace(/flowers/g, 'flowers/fullsize');
}

$('.elementLink').live('click', function(){
        var id = $(this).attr('id');
        id = id.substring(8);
        var full = $('#full_image' + id).val();
        $('#confirm').empty();
        $('#confirm').append('<img id="bi" src="/images/upload/'+full+'">');
        $('#confirm').append('<img alt="" src="/images/blomster_mark_large.gif" style="left: 20px; position: absolute; top: 15px;" />');
        //var img_width = $("#bi").width()+50;
        //var img_height = $("#bi").height()+150;
        

        $('#confirm').dialog({
            autoOpen: true,
            width: 840,
            height: 600,
            modal: true,
            resizable: false,
            buttons: {
                "Stäng": function() {
                    $(this).dialog("close");
                 }
            }
        });
});
function showInfoTable(newIndexID) {
        
	/*hideInfoTables();*/

       $('#infoTable' + newIndexID).show();
	getLayer('infoTable' + newIndexID).style.display = 'block';
	document.images['img' + newIndexID].style.borderColor = '#FFFFFF';
	resizeScroller();
}
function showInfoTable2(id){
        
        $("div[id^='infoTable']").hide();
        
        $('#infoTable' + id).show();

        $("img[id^='img']").css({"border-color":"#D4AAFE"});
        $('#img' + id).css({"border-color":"#FFFFFF"});
}
function showPointer(newObject) {
	newObject.style.cursor = 'pointer';
}
function slowScrollDown() {
	getLayer('scrollContent').scrollTop += (slowDowns % parseInt(nrOfCounts / scrollAmount) == 0? scrollAmount--: scrollAmount);
	if (slowDowns-- > 0) scrollTimer = setTimeout('slowScrollDown()', 30);
}
function slowScrollUp() {
	getLayer('scrollContent').scrollTop -= (slowDowns % parseInt(nrOfCounts / scrollAmount) == 0? scrollAmount--: scrollAmount);
	if (slowDowns-- > 0) scrollTimer = setTimeout('slowScrollUp()', 30);
}
