var debug_mode = false;
var debug_div;
var debug_counter = 0;

var left_block;

var cycle_previews = true;
var delay          = 5000;
var preview_delay  = 8000;
var current_index  = 0;
var last_index     = 0;
var objects        = [];
var cycle_timer;
var resize_timer;

$(document).ready(function() {
    if ( typeof current_page != 'undefined' && current_page != null ) initPage(current_page);
    
    if ( debug_mode ) {
        debug_div = document.createElement('DIV');
        debug_div.id = 'debug';
        document.body.appendChild(debug_div);
    }
    
    externalLinks();
    
    if ( $('body').hasClass('description') ) return true;
        
    $('#nav a.tooltip').each( function(){
        $(this).qtip({
            content: $(this).attr('title') || $(this).text(),
            show: 'mouseover',
            hide: 'mouseout',
            position: { corner: {target: 'topMiddle', tooltip: 'bottomMiddle'} },
            style: {
                name: 'dark',
                border: { radius: 5 },
                tip: { corner: 'bottomMiddle', size: {x: 5, y: 5} }
            }
        });
    });
}); // End document.ready

function initPage(page) {
    $('div.menu li.active a').click(function(event){ event.preventDefault(); });
    switch (page) {
    case 'home':
        /*
        $(left_block).removeClass('nojs');

        $(window).resize(function(){
            $(left_block).height( $(window).height() - $(left_block).offset().top - 20 );
            $(left_block).css('left', $('#header').offset().left);
            $('div.accordion').accordion('resize');
        });

        $('div.accordion').accordion({ fillSpace: true });
        $(left_block).css({
            position: 'fixed',
            top     : $('#header').offset().top + $('#header').height() + 10,
            left    : $('#header').offset().left
        });

        $(left_block).height( $(window).height() - $(left_block).offset().top -20 );
        $('div.accordion').accordion('resize');
        */
        
        $('#services').tabs();

        break;
    case 'portfolio':
        $('ul.previews li>a').each(function(){
            var demo = $(this).attr('href');
            demo = demo.substr(0,demo.indexOf('/'));

            var title = $(this).attr('title') + ' : ';
            title    += '<a href="' + demo + '" ';
            title    += 'onclick="window.open(\'' + demo + '\'); return false;">Demo</a>';
            $(this).colorbox({
                slideshow    : true,
                slideshowAuto: false,
                title        : title,
                onComplete   : function(){ $('#cboxTitle a').attr('target','_blank'); }
            });
        });
        break;
        
    case 'hosting':
        //$('#hosting-frame').load(function(){ $('#loading').remove(); });
        break;
        
    case 'projects':
        break;

    default:
        break;
    }
} // End initPage function

function fadeElements() {
    var element = objects[current_index];
    last_index = current_index;
    
    if ( current_index == (objects.length-1) ) {
        current_index = 0;
    } else {
        current_index = Math.floor( Math.random() * objects.length );
        
        while ( current_index == last_index ) {
            current_index = Math.floor( Math.random() * objects.length );
        }
    }
    
    objects.not(":hidden").fadeOut("slow",
        function() { $(element).fadeIn("slow"); }
    );

    setTimeout( function(){ fadeElements(); }, delay );
} // End fadeElements function


function previewPage( link ) {
    $('#portfolio-links a').removeClass('current');
    $('#preview').fadeOut('fast', function(){
        var description = $('#preview .description');
        var img         = $('#preview img');
        
        $(link).addClass('current');
        $(img).attr('src', $(link).attr('href') + '/snapshot.jpg' );
        
        $(description).attr( 'data', $(link).attr('href') + '/description.html' );
        var newdescription = $(description).clone();
        var container      = $(description).parent();
        $(description).remove();
        $(container).append( $(newdescription) );

        $(img).width('auto');
        
        $(img).bind('load', previewImgSize);

        $('#preview').fadeIn('fast');
    });

}

function cyclePreview() {
    if ( current_index >= objects.length ) current_index = 0;
    previewPage( $(objects[current_index]) );
    current_index++;
}

function resizePageParts() {
    var window_height = $(window).height();
    
    var header_height = $('#header img.logo:first').height() + $('#header img.logo:first').position().top*2;
    var nav_height    = $('#nav .button:first').height() + $('#nav .button:first').position().top*2;
    var menu_bottom   = $('#nav .button:first').height();
    var content_height = window_height - header_height - nav_height;
    
    $('#nav').height(nav_height);
    $('#nav .menu:not(.button)').css('bottom', menu_bottom);
    $('#header').height(header_height);
    $('#main').height(content_height);
    $('#content').height(content_height);
    
    if ( current_page == 'portfolio' ) {
        $('#preview').height('100%');
        previewImgSize();
    }
    
    if ( debug_mode ) {
        ++debug_counter;
        var newp = document.createElement('P');
        newp.innerHTML = debug_counter + ': ' + window_height + '-' + header_height + '-' + nav_height + '=' + content_height;
        newp.innerHTML = debug_counter + ': ' + $('#content').height();
        debug_div.appendChild(newp);
        debug_div.scrollTop = debug_div.scrollHeight;
    }
} // End resizePageParts function

function previewImgSize() {
    $('#preview img').width('auto');
    
    var image_block_width  = $('#preview .preview-img').width();
    var image_block_height = $('#preview .preview-img').height();
    
    var imgtop    = 0;
    var imgleft   = 0;
        
    var imgwidth  = $('#preview img').width();
    var imgheight = $('#preview img').height();

    if ( imgwidth < image_block_width ) {
        imgleft = (image_block_width - imgwidth)/2;
    } else {
        imgwidth = image_block_width;
        $('#preview img').width( imgwidth );
    }

    if ( imgheight < image_block_height ) {
        imgtop = (image_block_height - imgheight)/2;
    }

    $('#preview img').css({
        'top'  : imgtop,
        'left' : imgleft
    });
} // End previewImgSize function

function externalLinks() {
    var external_links = $('a[rel="external"]');
    for (var i=0; i<external_links.length; i++) {
        $(external_links[i]).attr('target', '_blank');
    }
} // End externalLinks function

function loadMenu(divname) {
    var block = document.getElementById(divname);

    request = new XMLHttpRequest();

    request.open("GET",'http://www.ghodmode.com/menu.php',false);
    request.send(null);
    block.innerHTML = request.responseText;
} // End loadMenu function

function loadAd(divname, adfile) {
    var block = document.getElementById(divname);
    request = new XMLHttpRequest();

    request.open('GET', 'http://www.ghodmode.com/'+adfile, true);
    block.innerHTML = request.responseText;
} // End loadAd function
