﻿/* these scripts require jQuery and clientDetection to be present on the page */
var currentX = 1, zIndex = 1000000;

/* 'currentX' keeps track of what photo is currently shown - this function switches between that photo and the one given in 'x' */
/* both 'currentX' and 'x' are integers - the count of photos begins @ 1, not 0 */
/* if x = 0, go forward 1 from currentX | if x = -1, go backward 1 from currentX - this is for the next/previous buttons */
function GoToPhotoOrVideo(x){
    if (x == 0){
        x = (currentX + 1);
    };
    if (x == -1){
        x = (currentX - 1);
    };
    
    var curPhoto = $("#photo" + currentX),
        curThumb = $("#thumb" + currentX),
        nextPhoto = $("#photo" + x),
        nextThumb = $("#thumb" + x);
    
    if (nextThumb.length > 0 && !nextThumb.hasClass("selected")){
        if (nextPhoto.length > 0){
            /* is photo */
            
            if (curThumb.attr("video") != undefined) {
            
                // Last selected was a video
                
                // Make sure to stop any videos playing
                $("#videoParent").html('');


                $('#videoParent').css({ "display" : "none" });
                nextPhoto.css({ "zIndex" : (zIndex -= 1), "display" : "block" });

            } else {


                // Last selected was a photo

                curPhoto.css({ "zIndex" : zIndex });
                nextPhoto.css({ "zIndex" : (zIndex -= 1), "display" : "block" });


                setTimeout(function(){
                    curPhoto.animate({ "width" : "0px", "opacity" : "0" }, 700, function(){
                        $(this).css({ "display" : "none", "opacity" : "1", "width" : "963px" });
                    });
                }, 10);

            }
            
            curThumb.removeClass("selected").addClass("unselected");
            nextThumb.removeClass("unselected").addClass("selected");
            
            
            currentX = x;
        }else if (nextThumb.attr("video") != undefined){
            /* is video */
            
            curPhoto.css({ "zIndex" : zIndex });
            
            $('#videoParent').css({ "zIndex" : (zIndex -= 1), "display" : "block" , "opacity" : "1", "width" : "963px" });
            loadVideo(nextThumb.attr("video") , nextThumb.attr("vwidth") , nextThumb.attr("vheight") , nextThumb.attr("vtrackingname"));
            

            setTimeout(function(){
                curPhoto.animate({ "width" : "0px", "opacity" : "0" }, 700, function(){
                        $(this).css({ "display" : "none", "opacity" : "1", "width" : "963px" });
                    });
                }, 10);

            
            curThumb.removeClass("selected").addClass("unselected");
            nextThumb.removeClass("unselected").addClass("selected");

            currentX = x;


        };
    };
};

/* flashExtLink is declared on the page with Common.GetExternalLink("get_flash_player") */
var currentVideoTrackingName = '';
function loadVideo(vidName , width , height , vidTrackingName){

    var oDetection = new ClientDetection();
    
    var sSwfPath = '/images/future-cars/videoplayer.swf?sSrc=' + vidName.replace('.flv' , '') + '&bLoop=0&nW=' + width + '&nH=' + height + '&bottomSpace=44';
    var embedwidth = "100%";
    var embedheight = "100%";
    
    if(oDetection.HasFlash()){
        var Html = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"" + embedwidth + "\" height=\"" + embedheight + "\" align=\"middle\"  id=\"flashmovie\"  name=\"flashmovie\">" +
            "<param name=\"allowScriptAccess\" value=\"sameDomain\" />" +
            "<param name=\"movie\" value=\"" + sSwfPath + "\" />" +
            "<param name=\"quality\" value=\"high\" />" +
            "<param name=\"loop\" value=\"false\" />" +
            "<param name=\"menu\" value=\"false\" />" +
            "<param name=\"bgcolor\" value=\"#ffffff\" />" +
            "<param name=\"allowFullScreen\" value=\"true\" />" +
            "<param name=\"wmode\" value=\"transparent\" />" +            
            "<embed src=\"" + sSwfPath + "\" id=\"flashmovie\"  name=\"flashmovie\" menu=\"false\" loop=\"false\" quality=\"high\" bgcolor=\"#ffffff\" width=\"" + embedwidth + "\" height=\"" + embedheight + "\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" allowFullScreen=\"true\" wmode=\"transparent\" />" +
            "</object>";
        
        EmbededObjectWriteContent("videoParent", Html);
        currentVideoTrackingName = vidTrackingName;
    }else{
        $("#videoParent").html("<div id=\"FlashMessageContent\">" + GetFlashPlayerMessage() +
            "<div class=\"FlashMessageIcon\"><a target=\"_blank\" href=\"" + flashExtLink + "\"><img src=\"/images/_global/icons/icon-getflash.gif\" /></a></div>" +
            "</div>");
    };
};

// Functions for tracking the tour
function TrackEvent (sEvent , iBenchmark) {

    sEvent = sEvent.toUpperCase();

    switch (sEvent) {
        case 'START':
            var sLinkName = currentVideoTrackingName + ' START';
            s.linkTrackVars = 'prop20,prop37,eVar19,eVar20,events';
            s.linkTrackEvents = 'event19,event18';
            s.prop20 = currentVideoTrackingName;
            s.eVar19 = 'VIDEO START';
            s.eVar20 = s.prop20;
            s.events = 'event19,event18';
            CallSDotTL(s , true , sLinkName , 'o');
            
            break;
        case 'BENCHMARK':
            var sLinkName = currentVideoTrackingName + ' BENCHMARK';
            s.linkTrackVars = 'prop20,prop37,eVar19,eVar20,events';
            s.linkTrackEvents = 'event18';
            s.prop20 = currentVideoTrackingName;
            s.eVar19 = iBenchmark + ' SECONDS COMPLETE';
            s.eVar20 = s.prop20;
            s.events = 'event18';
            CallSDotTL(s , true , sLinkName , 'o');

			break;
        case 'END':
            var sLinkName = currentVideoTrackingName + ' COMPLETE';
            s.linkTrackVars = 'prop20,prop37,eVar19,eVar20,events';
            s.linkTrackEvents = 'event20,event18';
            s.prop20 = currentVideoTrackingName;
            s.eVar19 = 'VIDEO COMPLETE';
            s.eVar20 = s.prop20;
            s.events = 'event20,event18';
            CallSDotTL(s , true , sLinkName , 'o');
			
            break;
    }

};
