// var zc_leftNextImage = new Image();
// var zc_rightNextImage = new Image();
var zc_prevCaption = '';
var zc_nextCaption = '';
var zc_leftNext = zc_rightNext=0;

var zc_imagesLoaded = 0;

function zc_initCarousel()
{
	for(var i = 0; i < zc_images.length;i++) {
		var imgFname = zc_images[i];
		zc_images[i] = document.createElement('img');
//		zc_images[i].onLoad = zc_isReadyCarousel();
		zc_images[i].src = imgFname;
		
		jQuery("<img>")
	    .attr("src", imgFname)
	    .load(function() {
	    	zc_isReadyCarousel();
	    });
	}
}

function zc_isReadyCarousel()
{
    zc_imagesLoaded = zc_imagesLoaded + 1;
    if(zc_imagesLoaded == zc_images.length) {
    	zc_buildCarousel();
    	jQuery("#carouselleft").css("display", "inline");
    	jQuery("#carouselright").css("display", "inline");
    	jQuery("#ajaxloader").remove();
    }
}

function zc_fixTitlesCarousel()
{
    $('carouseltitle'+zc_slots[1]).setStyle({display : 'none', width: '27%', left: '0px', right: '',
    fontSize: '9px', marginLeft: '7px', marginTop: '1px', textAlign: 'left'});
    $('carouseltitle'+zc_slots[2]).setStyle({display : 'none', width: '27%', left: '0px', right: '',
    fontSize: '12px', marginLeft: '9px', marginTop: '1px', textAlign: 'left'});
    $('carouseltitle'+zc_slots[3]).setStyle({display : '', width: '99%', left: '0px', right: '',
    fontSize: '20px', marginLeft: '11px', marginTop: '12px', textAlign: 'left'});
    $('carouseltitle'+zc_slots[4]).setStyle({display : 'none', width: '27%', left: '', right: '0px',
    fontSize: '12px', marginRight: '9px', marginTop: '1px', textAlign: 'right'});
    $('carouseltitle'+zc_slots[5]).setStyle({display : 'none', width: '27%', left: '', right: '0px',
    fontSize: '9px', marginRight: '7px', marginTop: '1px', textAlign: 'right'});
//  $('carouseltext'+zc_slots[3]).setStyle({display : ''});
    
    if(zc_slots[1] == "0") {        

        $('carousellink' + zc_slots[3]).innerHTML = "";
    	$('carouselvideo' + zc_slots[3]).setStyle({display : 'block'});
    	
    	var isiDevice = /iPad|iPhone|iPod/i.test(navigator.userAgent);

	
    		$('carouselvideo' + zc_slots[3]).innerHTML = "<a id=\"promo_video\" href='" + zc_link[zc_slots[3]] + (isiDevice ? ".mp4" : ".flv") + "'><img border=\"0\" width=\"545\" height=\"340\" src=\"" + root_path + "/images/blank.gif\"></a>";
    	
   		flowplayer("promo_video", {src: root_path + "/swf/flowplayer-tvbed-3.2.5.swf", wmode: 'opaque'}, {
   			key: '$67eeff3df6be482cbdf',
   			clip: {
			scaling: 'orig',
 	                ipadUrl: zc_link[zc_slots[3]] + ".mp4"
   	        },    			
   			plugins: {
				content: {			
					// the only required property
					url: '/swf/flowplayer-tvbed-3.2.5.swf',
					backgroundColor: '#000000',
					backgroundGradient: 'none'					
				},
				controls: { 
					time: false, 
					volume: false					
				}
   			} 
   		}).ipad();

	
    	
    } else {
    
        $('carousellink' + zc_slots[3]).innerHTML = "<a href='" + zc_link[zc_slots[3]] + "'>VIEW THE RANGE<a/>";
    }
    $('carousellink' + zc_slots[3]).setStyle({display : ''});
    
    $('carousel'+zc_slots[1]).setStyle({zIndex : '1'});
    $('carousel'+zc_slots[2]).setStyle({zIndex : '2'});
    $('carousel'+zc_slots[3]).setStyle({zIndex : '3'});
    $('carousel'+zc_slots[4]).setStyle({zIndex : '2'});
    $('carousel'+zc_slots[5]).setStyle({zIndex : '1'});
    $('carouselimage'+zc_slots[1]).onclick=function() {
         zc_moveRight();
         return false;
    };
    $('carouselimage'+zc_slots[2]).onclick=function() {
         zc_moveRight();
         return false;
    };
    $('carouselimage'+zc_slots[3]).onclick=function() {
//    	if(zc_slots[3] == "3") {
    		
//    	} else {
    		window.location.href = zc_link[zc_slots[3]];
//    	}
    };
    $('carouselimage'+zc_slots[4]).onclick=function() {
         zc_moveLeft();
         return false;
    };
    $('carouselimage'+zc_slots[5]).onclick=function() {
         zc_moveLeft();
         return false;
    };

}

function zc_buildCarousel()
{
    $("carouselleft").onclick=function() {
            zc_moveRight();
            return false;
    }
    $("carouselright").onclick=function() {
            zc_moveLeft();
            return false;
    }
    for(var i = 1; i < 6; i++)
    {
        zc_copyElement('carousel', 'carouselwrap', zc_slots[i]); 
        $('carousel'+zc_slots[i]).setStyle(zc_slotStyles[i]);
        $('carouselimage'+zc_slots[i]).src = zc_images[zc_slots[i]].src;
        $('carouseltitle'+zc_slots[i]).innerHTML = zc_titles[zc_slots[i]];
        $('carouseltext'+zc_slots[i]).innerHTML = zc_text[zc_slots[i]];
        $('carousel'+zc_slots[i]).setStyle({display : ''});
    }
    zc_fixTitlesCarousel()
}

function zc_addCarousel(item, slot)
{
    zc_copyElement('carousel','carouselwrap',item); 
    $('carousel'+item).setStyle(zc_slotStyles[slot]);
    $('carouselimage'+item).src=zc_images[item].src;
    $('carouseltitle'+item).innerHTML=zc_titles[item];
    $('carouseltitle'+item).setStyle({display : 'none'});
    $('carouseltext'+item).innerHTML=zc_text[item];
    $('carousel'+item).setStyle({display : ''});
}

function zc_moveCarousel(direction)
{
    var start=2;
    var end=5;
    var zed=new Array(1,1,2,3,2,1)
    if(direction=='left') {
        start=1;
        end=4;
    }
    for(var i=start;i<=end;i++)
    {
        $('carousel' + zc_slots[i]).setStyle({zIndex : zed[i]});
        $('carouseltitle' + zc_slots[i]).style.display = 'none';
        $('carouseltext' + zc_slots[i]).style.display = 'none';
        $('carousellink' + zc_slots[i]).style.display = 'none';
        $('carousel' + zc_slots[i]).morph(zc_slotStyles[i],
        {afterFinish: function() {zc_fixTitlesCarousel();},
        duration: 0.5}
        );
    }
}

function zc_copyElement(elem, target, suffix)
{
    var element = $(elem);
    var copyElement = element.cloneNode(true);
    var copyLength = copyElement.childNodes.length - 1;
    copyElement.id = elem + suffix;

    for(var i = 0; copyLength >=i ;i++)
    {
	    if(copyElement.childNodes[i].id)
        {
    	    var copyNode=copyElement.childNodes[i];
    	    var firstId=copyNode.id;
    	    copyNode.id=firstId+suffix; 
        }
    }
    $(target).appendChild(copyElement);
}

function zc_outputStuff()
{
    var output="";
    output=output+"zc_rightNextImage: <img src='"+zc_rightNextImage.src+"' /><br/>";
    output=output+"rightNext: "+rightNext+"<br/>";
    output=output+"slot1: "+zc_slots[1]+"<br/>";
    output=output+"slot2: "+zc_slots[2]+"<br/>";
    output=output+"slot3: "+zc_slots[3]+"<br/>";
    output=output+"slot4: "+zc_slots[4]+"<br/>";
    output=output+"slot5: "+zc_slots[5]+"<br/>";
    output=output+"leftNext: "+leftNext+"<br/>";
    output=output+"leftNextImage: <img src='"+zc_leftNextImage.src+"' /><br/>";
    $("out").innerHTML=output;
}    
function zc_moveRight()
{
    //Work out what goes where
	$('carouselvideo' + zc_slots[3]).innerHTML = "";
	
    zc_leftNext = zc_slots[5];
    zc_slots[5] = zc_slots[4];
    zc_slots[4] = zc_slots[3];
    zc_slots[3] = zc_slots[2];
    zc_slots[2] = zc_slots[1];
    zc_slots[1] = zc_rightNext;
    zc_rightNext = zc_rightNext - 1;
        
    if(zc_rightNext < 0) zc_rightNext = zc_images.length - 1;
        
    //Now do the moves
    
    //Hide slot 5
    $('carousel' + zc_leftNext).remove();
    //Add slot 1
    zc_addCarousel(zc_slots[1], 1);
    //Move slots left
    zc_moveCarousel('right');
    
//    Download zc_rightNextImage
//    zc_leftNextImage.src = zc_images[zc_leftNext];
//    zc_rightNextImage.src = zc_images[zc_rightNext];
}

function zc_moveLeft()
{
	
	$('carouselvideo' + zc_slots[3]).innerHTML = "";
	
    //Work out what goes where
    zc_rightNext = zc_slots[1];
    zc_slots[1] = zc_slots[2];
    zc_slots[2] = zc_slots[3];
    zc_slots[3] = zc_slots[4];
    zc_slots[4] = zc_slots[5];
    zc_slots[5] = zc_leftNext;
    zc_leftNext = zc_leftNext + 1;
    if(zc_leftNext >= zc_images.length) zc_leftNext = 0;
    //Now do the moves
    
    //Hide old slot 1
    $('carousel' + zc_rightNext).remove();
    //Add slot 5
    zc_addCarousel(zc_slots[5], 5);
    //Move slots left
    zc_moveCarousel('left');
    
//    Download zc_leftNextImage
//    zc_leftNextImage.src = zc_images[zc_leftNext];
//    zc_rightNextImage.src = zc_images[zc_rightNext];
}
