/**
 * @author enm
 */
$(document).ready(function(){
    if ($("#uiTabs").length > 0) {
        $("#uiTabs").hide();
        var $tabs = $("#uiTabs > ul").tabs();
        $("#uiTabs").show();
        
        $('.ui-tabs-nav').bind('tabsselect', function(event, ui){
            if (ui.panel.id == 'tour') {
                loadFlash();
            }
        });
    }

    $('.swatchClr').each(function(){
        var id = this.id;
        var clrs = id.split('-');
        var code = clrs[0];
        var rgb   = clrs[1].split('_');
        var hex  = clrs[2];
        $(this).css('background-color','#'+hex);
        $(this).hover(
            function(){
                $(this).css('cursor','pointer');
            },
            function(){
                $(this).css('cursor','auto');
            }
        );
        $(this).bind('click',function(){
            //see if this is the first time
            if($('#origDetailPic').length == 0){
                var origSrc = $('#detailPic1 img').attr('src');
                $('body#pg-vspecs div#Exterior').append('&nbsp;<span id="origDetailPic"><a rel="'+origSrc+'">original image</a></span>');

                //bind click event
                $('#origDetailPic').click(function() {doShowOrigPic();});
            }

            doShowColorPic(code);
            $('#addPicInfo').html('('+this.title+')');
        });
    });

    $('.swatchClr_evox').each(function(){
        var id = this.id;
        var clrs = id.split('-');
        var code = clrs[0];
        var hex  = clrs[1];
        $(this).css('background-color','#'+hex);
        $(this).hover(
            function(){
                $(this).css('cursor','pointer');
            },
            function(){
                $(this).css('cursor','auto');
            }
        );
        $(this).bind('click',function(){
            //see if this is the first time
            if($('#origDetailPic').length == 0){
                var origSrc = $('#detailPic1 img').attr('src');
                $('body#pg-vspecs div#vehicle_colors').append('&nbsp;<span id="origDetailPic"><a rel="'+origSrc+'">original image</a></span>');

                //bind click event
                $('#origDetailPic').click(function() {doShowOrigPic();});
            }

            doShowColorPic(code);
            $('#addPicInfo').html('('+this.title+')');
        });
    });

    $('#ext').click(function() { // bind click event to link
        //$tabs.tabs('select', 3); // switch to third tab
        //return false;
    });

    //$('#gallery').prepend('');
    //$('#gallery').prepend('');
});

function doShowOrigPic(){;
    var src = $('#origDetailPic a').attr('rel');
    $('#detailPic1 img').attr('src',src);
    $('#addPicInfo').html('');
}

function doShowColorPic(code){
    if(evox != ''){
        var src = 'http://media.pmmsystem.net/assets/vndr/evox/color_0320_032/'+evox+'/'+evox+'_cc0320_032_'+code+'.jpg';
        //$('#detailPic1').html('<img src="'+b+'" />');
        $('#detailPic1 img').attr('src',src);
    }
    else{
        return false;
    }
}

function loadFlash(){
    loadFlashExt("Ext360");
    loadFlashInt("Int360");
}

function loadFlashExt(id){
    var exterior = 'http://media.pmmsystem.net/assets/vndr/evox/exterior_036_swf_480/' + evox + '_ex036_480.swf';
    var flashObjExt = new FlashObject (exterior, "360_Ext", "480", "360", 8, "#FFFFFF", true);
    flashObjExt.addParam ("quality", "high");
    flashObjExt.write (id);
}

function loadFlashInt(id){
    var interior  = 'http://media.pmmsystem.net/assets/vndr/evox/interior_036_swf_480/' + evox + '_in036_480.swf';
    var flashObjInt = new FlashObject (interior, "360_Int", "480", "360", 8, "#FFFFFF", true);
    flashObjInt.addParam ("quality", "high");
    flashObjInt.write (id);
}