$(document).ready(function(){

    $('.mv_tab').click(function() {
        var temp_link = $(this).attr("id");
        var hash_id = temp_link.replace("mv_id_", "");
        
        $('#media_viewer .mv_tabs ul li a').removeClass('selected');
        $(this).addClass('selected');
        $('#media_viewer .mv_main').children().fadeOut();
        $('#media_viewer .mv_main #mv_c_'+hash_id).delay(500).fadeIn();
        $('#media_viewer .mv_side').children().slideUp();
        $('#media_viewer .mv_side #mv_s_'+hash_id).delay(500).slideDown();
        return false;
    });
    
    /////////////////////////////////////////////////// PHOTOS
    
    
    $('#mv_c_1 .mv_n_link').click(function() {
        $('#mv_c_1').hide();
        $('.mv_main .load').show();
        
        if($(this).attr("id") != ''){
            $.getJSON('../../include/media_viewer.php?get=photo&id='+$(this).attr("id"), function(photo) {
                $('#mv_c_1 h4').html('<a href="portfolio/'+photo.category_link+'" title="'+photo.category_name+'">'+photo.category_name+'</a>');
                if(photo.stone){
                    $('<a href="materials/'+photo.stone_type_link+'/'+photo.stone_link+'" title="'+photo.stone+'"> '+photo.stone+'</a> <a href="materials/'+photo.stone_type_link+'" title="'+photo.stone_type+'">'+photo.stone_type+'</a>AA').appendTo('#mv_c_1 h4');
                }
                $('#mv_c_1 .desc').html(photo.description);
                $('#mv_c_1 .mv_counter').text(photo.category_name+': '+photo.p_actual+' / '+photo.p_all);
                $('#mv_c_1 .photo_link').attr('href', 'portfolio/'+photo.category_link+'/'+photo.id);
                
                
                if (photo.id_next) {$('#mv_c_1 .next').attr('id', photo.id_next); $('#mv_c_1 .next').show();}
                    else{$('#mv_c_1 .next').hide();}
                if (photo.id_prev) {$('#mv_c_1 .prev').attr('id', photo.id_prev); $('#mv_c_1 .prev').show();}
                    else{$('#mv_c_1 .prev').hide();}
                
                $('#mv_photo').load(function(){
                    $('#mv_c_1').fadeIn();
                    $('.mv_main .load').hide();
                }).attr('src','http://www.marble.com/gallery/'+photo.category_link+'/medium/'+photo.id+'.jpg');
            });
        }
        return false;
    });
    
    
    $('#mv_s_1 .mv_g_link').click(function() {
        $('#mv_c_1').hide();
        $('.mv_main .load').show();
        $('#mv_s_1 ul li a').removeClass('selected');
        $(this).addClass('selected');
        
        var temp_link = $(this).attr("id");
        var hash_id = temp_link.replace("mv_g_", "");
        
        $.getJSON('../../include/media_viewer.php?get=photo_category&id='+hash_id, function(photo) {
            $('#mv_c_1 h4').html('<a href="portfolio/'+photo.category_link+'" title="'+photo.category_name+'">'+photo.category_name+'</a>');
            if(photo.stone){
                $('<a href="materials/'+photo.stone_type_link+'/'+photo.stone_link+'" title="'+photo.stone+'"> '+photo.stone+'</a> <a href="materials/'+photo.stone_type_link+'" title="'+photo.stone_type+'">'+photo.stone_type+'</a>AA').appendTo('#mv_c_1 h4');
            }
            $('#mv_c_1 .desc').html(photo.description);
            $('#mv_c_1 .mv_counter').text(photo.category_name+': '+photo.p_actual+' / '+photo.p_all);
            $('#mv_c_1 .photo_link').attr('href', 'portfolio/'+photo.category_link+'/'+photo.id);
            
            if (photo.id_next) {$('#mv_c_1 .next').attr('id', photo.id_next); $('#mv_c_1 .next').show();}
                else{$('#mv_c_1 .next').hide();}
            if (photo.id_prev) {$('#mv_c_1 .prev').attr('id', photo.id_prev); $('#mv_c_1 .prev').show();}
                else{$('#mv_c_1 .prev').hide();}
            
            $('#mv_photo').load(function(){
                $('#mv_c_1').fadeIn();
                $('.mv_main .load').hide();
            }).attr('src','http://www.marble.com/gallery/'+photo.category_link+'/medium/'+photo.id+'.jpg');
        });
        
        return false;
    });
    
    
    
    /////////////////////////////////////////////////// VIDEOS
    
    
    $('#mv_c_2 .mv_n_link').click(function() {
        $('#mv_c_2').hide();
        $('.mv_main .load').show();
        
        if($(this).attr("id") != ''){
            $.getJSON('../../include/media_viewer.php?get=video&id='+$(this).attr("id"), function(video) {
                $('#mv_c_2 h4').html('<a href="video/'+video.category_link+'/'+video.id+'" title="'+video.title+'">'+video.title+'</a>');
                $('#mv_c_2 .desc').html(video.description);
                $('#mv_c_2 .mv_counter').text(video.category_name+': '+video.p_actual+' / '+video.p_all);
                
                
                if (video.id_next) {$('#mv_c_2 .next').attr('id', video.id_next); $('#mv_c_2 .next').show();}
                    else{$('#mv_c_2 .next').hide();}
                if (video.id_prev) {$('#mv_c_2 .prev').attr('id', video.id_prev); $('#mv_c_2 .prev').show();}
                    else{$('#mv_c_2 .prev').hide();}
                
                
                $('#mv_video').empty();
                if(video.category_link == 'granite-shorts'){
                    $('#mv_video').html('<iframe title="{$mv_video.title}" width="484" height="300" src="http://www.youtube.com/embed/'+video.link+'" frameborder="0" allowfullscreen></iframe>');
                }
                else{
                    $('#mv_video').html('<embed id="mv_player" name="mv_player" src="../theme/js/player.swf" width="484" height="272" allowscriptaccess="always" allowfullscreen="true" flashvars="image=../gallery/'+video.category_link+'/big/'+video.id+'.jpg&streamer=http://www.1granite.com/xmoov.php&file='+video.category_link+'/'+video.id+'.flv&type=http&skin=../theme/js/overlay.swf&frontcolor=ffffff&lightcolor=cc9900&controlbar=over&stretching=uniform&bufferlength=5&plugins=gapro-1&dock=true&gapro.accountid=UA-131177-1&gapro.trackstarts=true&gapro.trackpercentage=true&gapro.tracktime=true&gapro.idstring=||file||"/>');
                }

                $('#mv_c_2').fadeIn();
                $('.mv_main .load').hide();
            });
        }
        return false;
    });
    
    
    $('#mv_s_2 .mv_v_link').click(function() {
        $('#mv_c_2').hide();
        $('.mv_main .load').show();
        $('#mv_s_2 ul li a').removeClass('selected');
        $(this).addClass('selected');
        
        var temp_link = $(this).attr("id");
        var hash_id = temp_link.replace("mv_v_", "");
        
        $.getJSON('../../include/media_viewer.php?get=video_category&id='+hash_id, function(video) {
            $('#mv_c_2 h4').html('<a href="video/'+video.category_link+'/'+video.id+'" title="'+video.title+'">'+video.title+'</a>');
            $('#mv_c_2 .desc').html(video.description);
            $('#mv_c_2 .mv_counter').text(video.category_name+': '+video.p_actual+'/'+video.p_all);

            
            if (video.id_next) {$('#mv_c_2 .next').attr('id', video.id_next); $('#mv_c_2 .next').show();}
                else{$('#mv_c_2 .next').hide();}
            if (video.id_prev) {$('#mv_c_2 .prev').attr('id', video.id_prev); $('#mv_c_2 .prev').show();}
                else{$('#mv_c_2 .prev').hide();}

            $('#mv_video').empty();
            if(video.category_link == 'granite-shorts'){
                $('#mv_video').html('<iframe title="{$mv_video.title}" width="484" height="300" src="http://www.youtube.com/embed/'+video.link+'" frameborder="0" allowfullscreen></iframe>');
            }
            else{
                $('#mv_video').html('<embed id="mv_player" name="mv_player" src="../theme/js/player.swf" width="484" height="272" allowscriptaccess="always" allowfullscreen="true" flashvars="image=../gallery/'+video.category_link+'/big/'+video.id+'.jpg&streamer=http://www.1granite.com/xmoov.php&file='+video.category_link+'/'+video.id+'.flv&type=http&skin=../theme/js/overlay.swf&frontcolor=ffffff&lightcolor=cc9900&controlbar=over&stretching=uniform&bufferlength=5&plugins=gapro-1&dock=true&gapro.accountid=UA-131177-1&gapro.trackstarts=true&gapro.trackpercentage=true&gapro.tracktime=true&gapro.idstring=||file||"/>');
            }

            $('#mv_c_2').fadeIn();
            $('.mv_main .load').hide();            
        });
        
        return false;
    });
    
    
    
    /////////////////////////////////////////////////// MATERIALS
    
    
    $('#mv_c_3 .mv_n_link').click(function() {
        $('#mv_c_3').hide();
        $('.mv_main .load').show();
        
        if($(this).attr("id") != ''){
            $.getJSON('../../include/media_viewer.php?get=material&id='+$(this).attr("id"), function(material) {
                $('#mv_c_3 h4').html('<a href="materials/'+material.category_link+'" title="'+material.category_name+'">'+material.category_name+'</a> - <a href="materials/'+material.category_link+'/'+material.material_link+'#'+material.id+'">'+material.name+'</a>');
                $('#mv_c_3 .desc').html(material.description);
                $('#mv_c_3 .mv_counter').text(material.category_name+': '+material.p_actual+'/'+material.p_all);
                $('#mv_c_3 .material_link').attr('href', 'materials/'+material.category_link+'/'+material.material_link+'#'+material.id);
                
                
                if (material.id_next) {$('#mv_c_3 .next').attr('id', material.id_next); $('#mv_c_3 .next').show();}
                    else{$('#mv_c_3 .next').hide();}
                if (material.id_prev) {$('#mv_c_3 .prev').attr('id', material.id_prev); $('#mv_c_3 .prev').show();}
                    else{$('#mv_c_3 .prev').hide();}
                
                $('#mv_material').load(function(){
                    $('#mv_c_3').fadeIn();
                    $('.mv_main .load').hide();
                }).attr('src','http://www.marble.com/upload/stone/closeup/'+material.id+'.jpg');
            });
        }
        return false;
    });
    
    
    $('#mv_s_3 .mv_m_link').click(function() {
        $('#mv_c_3').hide();
        $('.mv_main .load').show();
        $('#mv_s_3 ul li a').removeClass('selected');
        $(this).addClass('selected');
        
        var temp_link = $(this).attr("id");
        var hash_id = temp_link.replace("mv_m_", "");
        
        $.getJSON('../../include/media_viewer.php?get=material_category&id='+hash_id, function(material) {
            $('#mv_c_3 h4').html('<a href="materials/'+material.category_link+'" title="'+material.category_name+'">'+material.category_name+'</a> - <a href="materials/'+material.category_link+'/'+material.material_link+'#'+material.id+'" title="'+material.name+'">'+material.name+'</a>');
            
            $('#mv_c_3 .desc').html(material.description);
            $('#mv_c_3 .mv_counter').text(material.category_name+': '+material.p_actual+'/'+material.p_all);
            $('#mv_c_3 .material_link').attr('href', 'materials/'+material.category_link+'/'+material.material_link+'#'+material.id);
            
            if (material.id_next) {$('#mv_c_3 .next').attr('id', material.id_next); $('#mv_c_3 .next').show();}
                else{$('#mv_c_3 .next').hide();}
            if (material.id_prev) {$('#mv_c_3 .prev').attr('id', material.id_prev); $('#mv_c_3 .prev').show();}
                else{$('#mv_c_3 .prev').hide();}
            
            $('#mv_material').load(function(){
                $('#mv_c_3').fadeIn();
                $('.mv_main .load').hide();
            }).attr('src','http://www.marble.com/upload/stone/closeup/'+material.id+'.jpg');
        });
        return false;
    });

    
});
