$(document).ready(function(){
    
    // Menu
    $(".industry strong").click(function(){
        wrapper = $(this).parent();
        $("ul", wrapper).slideDown("slow");
        $("ul", $(wrapper).siblings() ).slideUp("slow");
        
        // Track
        if (typeof(pageTracker) != 'undefined') {
            pageTracker._trackEvent("Portfolio", "View Category", 'portfolio category: ' + $(this).attr('id'));
        }
    });
    
    // Client Select
    $(".clients a").click(function(){
        client = $(this).attr('href');
        client = client.substr(1);
        load_client(client);
        
        // Track
        parent_id = $('strong', $("a[href='#" + client + "']").parents('.industry') ).attr('id');
        if (typeof(pageTracker) != 'undefined') {
            pageTracker._trackEvent("Portfolio", "View Project", 'portfolio project: ' + client);
        }
        
    });
    
    // Load first client
    var myFile = document.location.toString();
    if (myFile.match('#')) {
      var client = myFile.split('#')[1];
      load_client(client);
      $('strong', $("a[href='#" + client + "']").parents('.industry')).click();
    } else {
      client = $('a.start-client').attr('href');
      client = client.substr(1);
      load_client(client);
      $('strong', $("a[href='#" + client + "']").parents('.industry')).click();
    }
    
});

function load_client(client){
    
    // Menu selection
    $(".clients a").removeClass('client-selected');
    $("a[href='#" + client + "']").addClass('client-selected');
    
    // FadeOut
    $('#client-visual').hide();
    $('#client-title').hide();
    $('#client-visual-opts').html('');
    
    // Title Load and FadeIn
    title = '/wp-content/themes/insivia/images/portfolio/title-' + client +'.gif';
    $('<img />').attr('src', title).load(function(){
        $('#client-title').css('backgroundImage','url(' + title +')').fadeIn('slow');
    });
    
    explore = $("a[href='#" + client + "']").attr('explore');
    if( explore != undefined ){
        $('#client-title').html('<a class="text-shift explore" href="http://' + explore + '" target="_blank" onClick="trackOutbound(\'http://' + explore + '\');">' + explore + '</a>');
    }else{
        $('#client-title').html('');
    }

    // Begin Visual Load
    $('.portfolio-promo-right').addClass('iLoader');
    mtype = $("a[href='#" + client + "']").attr('mtype');
    switch (mtype) {
        case 'photography':
        
            $('#client-visual').html('');
            numimgs = $("a[href='#" + client + "']").attr('numimgs');
            photo = '/wp-content/themes/insivia/images/portfolio/';
            
            for(i=1;i<=numimgs;i++){
                $('#client-visual').append(
                '<a href="' + photo + client + i + '.jpg"><img src="' + photo + 'thumb-' + client + i + '.png" /></a>'
                ).fadeIn('slow');
            }
            
            $('#client-visual a').mouseenter(function(){
        		$(this).animate({marginTop: -10}, {queue:false, duration:200});
        	}).mouseleave(function(){
        		$(this).animate({marginTop: 0}, {queue:false, duration:200});
        	});
                
            $('#client-visual a').lightBox({
                overlayBgColor: '#074578',
                imageLoading: '/wp-content/themes/insivia/images/lightbox/lightbox-ico-loading.gif',
    			imageBtnPrev: '/wp-content/themes/insivia/images/lightbox/lightbox-btn-prev.gif',
    			imageBtnNext: '/wp-content/themes/insivia/images/lightbox/lightbox-btn-next.gif',	
    			imageBtnClose: '/wp-content/themes/insivia/images/lightbox/lightbox-btn-close.gif',
    			imageBlank: '/wp-content/themes/insivia/images/lightbox/lightbox-blank.gif'
            });
            
            $('.portfolio-promo-right').removeClass('iLoader');
            break;
        case 'video':
        
            load_video(client);
            
            var altvids = $("a[href='#" + client + "']").attr('altvids');
            if (altvids) {
                var altvidsArr = altvids.split(',');
                
                if (altvidsArr.length > 0) {
                
                    $('#client-visual-opts').append('<span class="text-shift altvids altvids_selected" altvid="' + client + '"></span>');
                    
                    $.each(altvidsArr, function(intIndex, objValue){
                        $('#client-visual-opts').append('<span class="text-shift altvids" altvid="' + objValue + '" ></span>');
                    });
                    
                    // Video Select
                    $(".altvids").click(function(){
                        load_video($(this).attr('altvid'));
                        $(".altvids").removeClass('altvids_selected');
                        $(this).addClass('altvids_selected');
                    });
                    
                }
            }
            $('.portfolio-promo-right').removeClass('iLoader');
            
            break;
            
        default:

            $('.portfolio-promo-right').addClass('iLoader');
            $('<img />').attr('src', '/wp-content/themes/insivia/images/portfolio/screen-' + client +'.png').load(function(){
                $('#client-visual').html($(this)).fadeIn('slow');
                $('.portfolio-promo-right').removeClass('iLoader'); 
            });
            
            var altimgs = $("a[href='#" + client + "']").attr('altimgs');
            
            if( altimgs ) {
                var altimgsArr = altimgs.split(',');
                if( altimgsArr.length > 0 ){
                    
                    $('#client-title').append(
                        '<span class="text-shift altimgs altimgs_selected" altimg="' + client + '">-1</span>'
                    );
                    
                    $.each(altimgsArr, function(intIndex, objValue ) {
                        $('#client-title').prepend(
                            '<span class="text-shift altimgs" altimg="' + objValue + '" >' + intIndex + '</span>'
                        );
                    }); 
                       
                    // Image Select
                    $(".altimgs").click(function(){
                        load_image( $(this).attr('altimg') );
                        $(".altimgs").removeClass('altimgs_selected');
                        $(this).addClass('altimgs_selected');
                    });
         
                }
            }
            
            break;
            
            
    }
    
}

function load_video(client){
    
    if( $("a[href='#" + client + "']").attr('vwidth') ){
        var vidsize = 'width:' + $("a[href='#" + client + "']").attr('vwidth') + 'px;height:' + $("a[href='#" + client + "']").attr('vheight') + 'px;';
    }else{
        var vidsize = 'width:480px;height:297px;';
    }
            
    $('#client-visual').html(
    '<div class="video-player-bg">' +
    '<a href="http://www.insivia.com/wp-content/themes/insivia/videos/portfolio/' + client +'.flv" class="video-player" id="player" style="display:block;' + vidsize + '"></a>' +
    '</div>'
    ).fadeIn('slow');
    
    flowplayer("player", "/wp-content/media/flowplayer-3.1.0.swf", { 
        clip:  { 
            autoPlay: false, 
            autoBuffering: true 
        } 
    });
    
}

function load_image(image){
    
    // Create URLs
    pscreen = '/wp-content/themes/insivia/images/portfolio/screen-' + image +'.png';
    
    // FadeOut
    $('#client-visual').hide();
    
    // Begin Screen Load
    $('<img />').attr('src', pscreen).load(function(){
        $('#client-visual').html($(this)).fadeIn('slow');    
    });
    
}

function trackOutbound(url){
    if (typeof(pageTracker) != 'undefined') {
        pageTracker._trackEvent("Portfolio", "Visit Website", url);
    }
}
