/*

 */
$(document).ready(function() {
	init();
});

/**


















 */
function init() {
	$('#StartBox').css("visibility", "visible");
	

	initHoverAccordion();







	initLensButton();
	initZoomStandardButton();
	initZoomDecreaseButton();
	initZoomIncreaseButton();
	initZoomMaximizeButton();


	initColorButtons();
	
	initializeModules();
	
	if(isViewZoneFx()) initialViewZoneFx();
	else executeImageFading();	
}

var app={
	options: {






	}
}

function initHoverAccordion() {
	$('#accordion').hoverAccordion({
		activateitem: '1',
		speed: 'slow'
	});
}

function initLensButton() {
	$('a#lensButton').click(function() {
		startLens();
		initializeImagePos();
    });
}

function initZoomStandardButton() {
	$('a#zoomStandard').click(function() {
		minimize();
        initializeImagePos();
	});
}

function initZoomDecreaseButton() {
	$('a#zoomDecrease').click(function() {	
		zoomOut();
        initializeImagePos();
	});
}

function initZoomIncreaseButton() {
	$('a#zoomIncrease').click(function() {
		zoomIn();
        initializeImagePos();
	});
}

function initZoomMaximizeButton() {
	$('a#zoomMaximize').click(function() {
		maximize();
		$('#ImageBox a').css("position", 'absolute');
		$('#ImageBox a').css("left",  0);
	});
}

function initColorButtons() {
	$('#ColorSelectorNeutral').addColorPicker({
		clickCallback: function(c) {

			$('#fragment-4').css("visibility", "hidden");
			$('.idTabsNav>a').removeClass('selected');
			onColorPicker(c);
		},
		/*titles: [
			'Schwarz', 'Weiss'
			],*/
		colors: [
			'#3F3E3C', '#F5FDFF'
			]
	});

	$('#ColorSelectorAlpina').addColorPicker({
		clickCallback: function(c) {

			$('#fragment-1').css("visibility", "hidden");
			$('.idTabsNav>a').removeClass('selected');
			onColorPicker(c);
		},
		/*titles: [
			'Life','Move','Dance','Pulse','Joy','Cover',
			'Change','Passion','Step','Fun','Flower','Break',
			'Expo','Drop','Look','Energy','Sweet','Spirit'
			],*/
		colors: [
			'#FFDF8C','#CABD4D','#EBD9D9','#573F4D','#F3E4BB','#F8EDDB',
			'#E3E8A8','#8D3B41','#7683A5','#F99056','#FDEAB2','#FFD497',
			'#837062','#9FC1CA','#FFB963','#AB4536','#FCE5C3','#DCE6E8'
			]
	});

	$('#ColorSelectorDulux1').addColorPicker({
		clickCallback: function(c) {

			$('#fragment-2').css("visibility", "hidden");
			$('.idTabsNav>a').removeClass('selected');
			onColorPicker(c);
		},
		/*titles: [
			'Grimsey','Sunset','Formosa','Tahiti','Kashmir','Metropol.',
			'N..Bast','Belvedere','Isabella','Polarw.','Sansibar','BoaVista',
			'N.Leinen','Moroni','Java','Maurit.','Soledad','Vulcano'
			],*/
		colors: [
			'#DBDBD3','#FCE5BC','#F0D05F','#F5D7B3','#DA744C','#BCD4E0',
			'#EEDDC9','#FDDAA4','#EEC051','#F3F0E1','#F59A54','#ABD8D3',
			'#F0E4CE','#CEC2B2','#F7D284','#E9E9DF','#EDAA80','#A94239'
			]
	});

	$('#ColorSelectorDulux2').addColorPicker({
		clickCallback: function(c) {

			$('#fragment-3').css("visibility", "hidden");
			$('.idTabsNav>a').removeClass('selected');
			onColorPicker(c);
		},
		/*titles: [
			'N.Bambus','N.Sand','G.Gate','KapVerde','Montego','Niagara',
			'Jamaica','N.Weide','Elysee','Bonaire','Broadw.','Barbad.',
			'Cabana','Tobago','Grenada','Andros','Orkney','Tinos'
			],*/
		colors: [
			'#F8E9CC','#E8DCCE','#FDC87C','#D9DDCC','#E1A084','#5282B2',
			'#FAE9CD','#F3DFC4','#FAD8BF','#D4E1C7','#D2D7D1','#98B5D3',
			'#F0E2BD','#F6E5C7','#F3BD9B','#D0DD8E','#B5C6CD','#6A9FC7'
			]
	});
}





/*



 */
function initializeModules() {
	initializeCarousel();
	initializeLightbox();
}
	   
function initializeCarousel() {

	$('#carousel').jcarousel({

	});   
}

function initializeLightbox() {

	$('a.lightbox').each(function(){

		if(this.mousedown_bound) return;
		this.mousedown_bound=true;
		
		$(this).mousedown(function(e){

			if(e.which != 1) return false;
			initializeZoom();
			this.href=$('#ImageBox a>img').attr('src');
			



			$('#caption').html(getLightboxDetails()).show();

		});
	});
}

/**


 */
function getLightboxDetails() {
	var items=getLightboxItems();
	
	title='<table cellpadding="1" cellspacing="0" border="0">';
	for(var i=0, index=0; i<items.length; i++) {
		title+=formatLightboxItem(items[i]);
	}
	title+='</table>';
				
	return title;
}

/**




 */
function formatLightboxItem(item) {
	var row='<tr><td>' + item["image"] + '</td>' +
				'<td align="top">' + item["key"] + '<br>' +
				item["description"] +
				'</td></tr>';
				
	return row;
}

/**



 */
function getLightboxItems() {
	var items=[{"key": $('#currentProductKey').text(),
				"description": $('#currentProductDescription').text(),
				"image": $('#currentProductImage').html()}
	];

	return items;
}

/**





 *	return getLightboxItemsByIds();


 */
function getLightboxItemsByIds() {
	var items=[];
	for(var i=0, index=0; ; i++) {
		if($('#currentProductKey'+i).length<=0) break;
		
		items[i]={"key": $('#currentProductKey'+i).text(),
				"description": $('#currentProductDescription'+i).text(),
				"image": $('#currentProductImage'+i).html()};
	}
	
	return items;
}

/**


 */
function removeDuplicateItems(items) {
	for(var i=0, index=0; i<items.length; i++) {
		var key=items[i]["key"];
		

		for(var ix=items.length-1; ix>i; ix--) {
			if(items[ix]["key"]==key) items.splice(ix, 1);
		}
	}
	
	return items;
}

var sceneObjects=null;
function setSceneObjects(objects) {
	sceneObjects=objects;
}

function getSceneObject(index) {
	return sceneObjects[index];
}

/**



 */
function getColorObjectIndex(objectIndex) {
	if(!sceneObjects[objectIndex].colorPermitted) return -1;
	var colorIndex=0;
	for(var i=objectIndex-1; i>=0; i--) {
		if(sceneObjects[i].colorPermitted) colorIndex++;
	}
	return colorIndex;
}

/**



 */
function getSceneObjectIndex(colorObjectIndex) {
	var colorIndex=-1;
	for(var i=0; i<sceneObjects.length; i++) {
		if(sceneObjects[i].colorPermitted) colorIndex++;
		if(colorIndex==colorObjectIndex) return i;
	}
	return -1;
}

/**






 */
function isDifferentContent(zoneHolder, html) {



    var test=zoneHolder.cloneNode(false);
    test.innerHTML = html;





    var newContent=$(test).children();
	var oldContent=$(zoneHolder).children();
    if(newContent.length!=oldContent.length) return true;
    
    for(var i=0; i<newContent.length; i++) {
    	if(newContent[i].innerHTML!=oldContent[i].innerHTML) return true;
    }
    
    return false;
}

/**

 */
function isViewZoneFx() {
	return false;
}

/**


 */
function initialViewZoneFx() {
	var zoneHolder=document.getElementById("aazone.viewZ");
	var html=zoneHolder.innerHTML;

	$("#ImageBox a").css({opacity: 0});
	ZoneFx.viewZoneCrossFade(zoneHolder, html);
}



var execID=0;
var execState=0;
var fadingTime=500;
var currentImage=null;
var newImage=null;
var newImageSource;
var additionalRequests=0;

/**

 */
function executeImageFading() {
	if (execID > 0) {









		additionalRequests++;
		return;
	}









	if($('#ImageBox').html()) {
		preImageFading();

		newImage=$('#ImageBox').html();
		newImageSource=$('#ImageBox>a>img').attr('src');
		if(currentImage!=null) {
			$('#ImageBox').html(currentImage+newImage);
		}

		execState=0;
		execID = setInterval("periodicExecution()", 10);
	}
}

/**


 */
function preImageFading() {
}

/**


 */
function postImageFading() {
}

/**



 */
function periodicExecution() {


    if (execState==0) {
    	execState=01; // begin to process this state, make sure that it is not invoked again
        
    	$('#ImageBox').append('<div id="WaitBox" style="position:relative; left:'+(($('#ImageBox').width()/2)-16)+'px; top:'+(($('#ImageBox').height()/2)-16)+'px; width:32px; height:32px;z-index:1;"><img src="images/ajax-loader.gif" border="0" style="width:32px;height:32px;"></div>');
        
    	execState=10;
    }
    


    else if (execState==10) {
    	execState=11; // begin to process this state, make sure that it is not invoked again
    	
        preloader = new Image();
         preloader.onload=function(){
            execState=20; // done, state transition to next phase
        }
        preloader.src = newImageSource;
    }
    


    else if (execState==20) {
    	execState=21; // begin to process this state, make sure that it is not invoked again
	    
    	if (isCentered()) {
	        var imageBoxWidth = $('#ImageBox').width();
	        setImagePosX(((imageBoxWidth-preloader.width)/2)+imagePosLeft);
	    }
	    else {
	        setImagePosX(imagePosLeft+imageBorder);
	    }
	    setImagePosY(imagePosTop+imageBorder);
        initializeImagePos();
        
        execState=30; // done, state transition to next phase
    }
    


    else if (execState==30) {
    	execState=31; // begin to process this state, make sure that it is not invoked again
    	
    	if ($('#ImageBox').children('a').size() > 1) {





    		var imgOld=$('#ImageBox').children('a:first');
    		var imgNew=$('#ImageBox').children('a:last');
    		if(imgOld.width()!=imgNew.width() || imgOld.height()!=imgNew.height()) imgOld.fadeOut(fadingTime);
    	}
        $('#ImageBox').children('a:last').fadeIn(fadingTime, function() {
            if ($('#ImageBox').children('a').size() > 1) {
                $('#ImageBox').children('a:first').remove();
            }
            $('#WaitBox').remove(); 
            execState=40; // done, state transition to next phase
        });
    }
    


    else if (execState==40) {
    	execState=41; // begin to process this state, make sure that it is not invoked again
    	
    	currentImage=$('#ImageBox').html().trim();
	    setNewImageLoaded();



	    $(".lightbox").lightbox({displayTitle: false}); //Lightbox.initialize();
	    initializeLightbox();
	    
	    execState=90; // done, state transition to next phase
    }
    


    else if (execState==90) {
    	execState=91; // begin to process this state, make sure that it is not invoked again
    	
        clearInterval(execID);
        execID=0;
        
        postImageFading();
        


        if(additionalRequests>0) {
        	additionalRequests--;
        	executeImageFading();
        }
    }
}


/*



 */
function initializeImagePos() {
	var a_last=$('#ImageBox a:last');//$('#ImageBox').children('a:last');
	a_last.css("position", 'absolute');
	a_last.css("left", getImagePosX()-imageBorder);
}


/**

 */
function isCentered() {
    return true;
}


/*

 */
String.prototype.trim = function () {
	return this.replace(/^\s*|\s*$/, "");
}


function openPrintPreview() {
	var parameter = "width=865px,scrollbars=yes,menubar=no,toolbar=no,status=no,resizable=no";
	printPreview = window.open('print.do', '_blank', parameter);
	printPreview.document.close();
}
