var KeyLooks=new Object();
/***** CONTENT SWITCH *****/
KeyLooks.tabLinks=function(row,itemId,activelink,link1,link2){
    // Check that the link isnt already active
    if(itemId!=''){if($(activelink).className=='active'){var linkCheck='true';}else{var linkCheck='false';}}
	// Reset active properties
	$(activelink).className='';
	$(link1).className='';
	$(link2).className='';
	// Set the new elements active
	if(itemId!=''&&linkCheck=='false'){$(activelink).className='active';}
}

/***** SELECT TEXT *****/
KeyLooks.selectSize=function(itemId,linkElement,size){
    if($(linkElement).className!='inactive'){
        // Reset all classnames to ''
        var AList=$('select-size'+itemId).getElementsByTagName('a');
        for(var i=0;i<AList.length; i++){if(AList[i].className!='inactive'){AList[i].className='';}}
        $('hdn_select-size'+itemId).value='';
        // Set the next link to active
        $(linkElement).className='active';
        $('hdn_select-size'+itemId).value=size;
        $('cbo_select-item'+itemId).checked=true;
   }
}

/***** ITEM OPENER *****/
KeyLooks.closer1=function(divElement){
    var shellList=$$('#keylooks_detail .details .item_row .content .content_shell');
    for(var i=0;i<shellList.length;i++){shellList[i].style.height='auto';}
    Global.openerCloser2(divElement,'close');
}

/***** NAVIGATE THE IMAGES *****/
var fullScreenChange='false';
var activeFullImage=0;
var qs=window.location.search.substring(1);
var pairs=qs.split("&");
for(i=0;i<pairs.length;i++){
    qs=pairs[i].split("=");
    if(qs[0].toLowerCase()=="total"){activeFullImage=parseInt(qs[1].replace("#",""))-1;}
}

KeyLooks.fullScreenSlider=function(direction){
    if(fullScreenChange=='false'){
        var imageContainer=$$('#keylooks_home .image_content')[0];
        var imageList=imageContainer.getElementsByTagName('img');

        if(direction=='prev'){
            if(activeFullImage==0){imageId=(imageList.length-1);}else{imageId=activeFullImage-1;}
            new Effect.Parallel([
                new Effect.BlindUp($(imageList[activeFullImage]),{scaleX:true,scaleY:false,sync:true}),
                new Effect.Fade($(imageList[activeFullImage]),{sync:true})
                ],{beforeStart:function(){
                    fullScreenChange='true';
                    setTimeout('KeyLooks.fullScreenPos(\'prev\',\''+imageId+'\')',50);
				},duration:0.5,afterFinish:function(){
				    $(imageList[activeFullImage]).style.zIndex='50';
				    activeFullImage=imageId;
				    $(imageList[imageId]).style.zIndex='100';
				    fullScreenChange='false';
				}});
		}else{
		    if(activeFullImage==(imageList.length-1)){imageId=0;}else{imageId=activeFullImage+1;}
		    new Effect.Parallel([
		        new Effect.Appear($(imageList[imageId]),{sync:true}),
		        new Effect.BlindDown($(imageList[imageId]),{scaleX:true,scaleY:false,sync:true})
		        ],{beforeStart:function(){
		            fullScreenChange='true';
		            setTimeout('KeyLooks.fullScreenPos(\'next\',\''+imageId+'\')',50);
		        },duration:0.5,afterFinish:function(){
		            $(imageList[activeFullImage]).style.zIndex='50';
		            $(imageList[activeFullImage]).hide();
		            activeFullImage=imageId;
		            fullScreenChange='false';
		        }});
		}
	}
}

KeyLooks.fullScreenPos=function(direction,imageId){
    var imageContainer=$$('#keylooks_home .image_content')[0];
    var imageList=imageContainer.getElementsByTagName('img');
    if(direction=='next'){
        $(imageList[activeFullImage]).style.zIndex='75';
        $(imageList[imageId]).style.zIndex='100';
        $(imageList[imageId]).style.height='678px';
        $(imageList[imageId]).height='678';
    }else{
        $(imageList[imageId]).show();
        $(imageList[activeFullImage]).style.zIndex='100';
        $(imageList[imageId]).style.zIndex='75';
    }
}

/***** REMOVE ITEM *****/
KeyLooks.RemoveItem=function(itemNo){
    $('cbo_select-item'+itemNo).checked=false;
    // Reset all classnames to ''
    var AList=$('select-size'+itemNo).getElementsByTagName('a');
    for(var i=0;i<AList.length;i++){if(AList[i].className=='active'){AList[i].className='';}}
    $('hdn_select-size'+itemNo).value='';
}

/********************************************************************************/
/***** CONTENT SWITCHER ********************************************************/
/******************************************************************************/
var switcherTransition='off';

KeyLooks.Content={
    /***** HUB FOR THE TRANSITIONS *****/
    Switcher:function(Opener,Closer,Container){
        // Get all of the DIVs styles set for the transition
        $(Container).setStyle({height:getOffsetHeight($(Closer))+'px',width:getOffsetWidth($(Closer))+'px',position:'relative',overflow:'hidden'});
   
        var openerLayout=KeyLooks.Content.Layout(Opener,Closer);

        // The content transition itself (fade out the active DIV,fade in the next)
        new Effect.Parallel([
			new Effect.Fade($(Closer),{sync:true}),
			new Effect.Appear($(Opener),{sync:true}),
			new Effect.Morph($(Container),{style:{height:openerLayout.Height+'px'},sync:true})
			],{duration:0.7, beforeStart:function(){
			    switcherTransition='on';
			},afterFinish:function(){
			    switcherTransition='off';

			// Change the DIVs back to normal positioning. Set the container height to auto for closing
			$(Opener).style.position ='';
			$(Closer).style.position ='';
			$(Container).style.height ='auto';
		}})},

        /***** DOM CHANGER FOR ALL DIVS IN PLAY *****/
		Layout:function(Opener,Closer){
		    // Prepare the Closing DIV
			$(Closer).setStyle({position:'absolute',left:'0',top:'0'});

		    // Prepare the Opening DIV
			$(Opener).show();
			var layout={Height:getOffsetHeight($(Opener)),Width:getOffsetWidth($(Opener))}
			$(Opener).setStyle({visibility:'hidden',position:'absolute',left:'0',top:'0',visibility:'visible'})
		
		    // Re-hide the opening DIV for the transition
			$(Opener).hide();
			
			return layout;
		},

        /***** OPEN THE DIV FOR THE FIRST TIME *****/
		Open:function(divElement){
		    // Opening transition
			new Effect.Parallel([
				new Effect.BlindDown($(divElement),{sync:true}),
				new Effect.Appear($(divElement),{sync:true})
			],{duration:0.7,beforeStart:function(){
				switcherTransition='on'
			},afterFinish:function(){
				switcherTransition='off'
			}})},

        /***** CLOSE THE DIV AND RESET VARIABLES *****/
		Close:function(divElement){
		    // Closing transition
			new Effect.Parallel([
				new Effect.Fade($(divElement),{sync:true}),
				new Effect.BlindUp($(divElement),{sync:true})
			],{duration:0.7,beforeStart:function(){
				switcherTransition='on'
			},afterFinish:function(){
				switcherTransition='off'
			}});
	}
}

/***** SWITCHING FUNCTION *****/
KeyLooks.Switcher=function(divElement){
    if(switcherTransition=='off'){
		var Container=$(divElement).up();
		var ContainerChildren=$(Container).childElements();
		var status='';

		for(var i=0;i<ContainerChildren.length;i++){
			if(ContainerChildren[i].style.display!='none'){
				var Closer=ContainerChildren[i];
				if(ContainerChildren[i]==$(divElement)){status='closing';}else{var divActive=ContainerChildren[i];status='switching';}
				break;
			}else{status='opening';}
		}
		
		switch(status){
		    case 'opening':KeyLooks.Content.Open(divElement);break;
		    case 'closing':KeyLooks.Content.Close(divElement);break;
		    case 'switching':Opener=divElement;Closer=divActive;KeyLooks.Content.Switcher(Opener,Closer,Container);
		    break;
		 }
	}
}