/* Copyright: Legion Digital, LLC 2010 */
$(document).ready(function () {
    if (Modernizr.canvas !== true) {}
    setTimeout("fx_throwIn('main-header-area')", 1300);
    setTimeout("fx_throwIn('main-text-area')", 1000);
    $('.main-features-list li').click(function () {
        var feature = $(this).attr('fid');
        
        // Track Click
        _gaq.push(['_trackEvent', 'Features', 'Click', feature]);
        
        main_showFeature(feature);
        main_selectFeature(this);
    });
    main_initShowcase();
    $('#content-images li').live('click', function () {
        var image = $(this).attr('img');
        var title = $(this).attr('title');
        var link  = $(this).attr('lnk');
        var type  = $(this).attr('type');
        
        // Track Click
       _gaq.push(['_trackEvent', 'Content', 'Click', image]);
        
        if(link != '' && link != 'undefined'){
            title = title + '|' + link + '|' + type
        }
        
        var client = $(this).parent().attr('client');
        image = 'content/' + client + '/' + image + '-full.png';
        $.fancybox({
            'type': 'image',
            'title': title,
            'content': image,
            'showCloseButton': false,
            'transitionIn': 'fade',
            'titleShow': true,
            'titlePosition': 'over',
            'titleFormat': main_formatTitle,
            'overlayColor': '#fff',
            'transitionOut': 'none',
            'hideOnContentClick': true,
            'scrolling': 'no',
            'onComplete': function () {
                $("#fancybox-wrap").hover(function () {
                    $("#fancybox-title").fadeIn('slow');
                }, function () {
                    $("#fancybox-title").fadeOut('slow');
                });
            }
        });
    });
});

function main_getBoxContent(id, image, title) {
    image = 'content/' + id + '/' + image + '-full.png';
    return '<a class="cimg" rel="cimg" title="' + title + '" href="#"><img alt="cimg" src="' + image + '"></a>';
}

function main_showFeature(id) {
    $('#main-loader').show('blind', 200);
    $('#content-list').fadeOut('slow', function () {
        $.getJSON('action.php?a=get&d=' + id, function (data) {
            main_populateInfo(data[0]);
            $('#main-loader').delay(500).hide('blind', 200);
            $('#content-list').delay(500).fadeIn('slow');
        });
    });
}

function main_formatTitle(title, currentArray, currentIndex, currentOpts) {

    var info = title.split('|'); 

    var first = info[0].substring(0, 1);
    var html = '<p class="content-title"><span>' + first + '</span>' + info[0].substring(1);
    
    if(info.length == 3){
        html += '<br/><br/><a class="content-link" href="'+info[1]+'" target="_blank">Click here to view the '+info[2]+'</a></p>';
    } else {
        html += '</p>'
    }
    
    return html;
}

function main_populateInfo(data) {
    var d_title = data['caption'];
    var d_desc = data['desc'];
    d_title = d_title.replace('(', '<span>');
    d_title = d_title.replace(')', '</span>');
    $('#main-header-area').html('<p>' + d_title + '</p>');
    $('#main-text-area').html('<p>' + d_desc + '</p>');
    $('#content-images').attr('client', data['id']);
    $('#content-images').html('');
    for (var i = 0; i < data['items'].length; i++) {
        var item = data['items'][i];
        var tags = item['tags'];
        var tag_html = '';
        if (tags.indexOf('facebook') >= 0) tag_html = '<div class="tags"><img alt="Facebook Application" src="./images/tag-fb.png"/></div>';
        else if (tags.indexOf('apple') >= 0) tag_html = '<div class="tags"><img alt="iOS Application" src="./images/tag-apple.png"/></div>';
        else if (tags.indexOf('ios') >= 0) tag_html = '<div class="tags"><img alt="iOS Application" src="./images/tag-ios.png"/></div>';
        $('#content-images').append('<li img="' + item['img'] + '" lnk="'+item['link']+'" type="'+item['type']+'" title="' + item['title'] + '" desc="' + item['desc'] + '">' + tag_html + '</li>');
    }
    main_refreshShowcase();
    var type_arr = Array();
    for (var i = 0; i < data['types'].length; i++) {
        var type = data['types'][i];
        var type_text = '<p class="tech">' + type + '</p>';
        if (type.indexOf('(') >= 0) {
            type = type.replace('(', '');
            type = type.replace(')', '');
            type_text = '<p class="art">' + type + '</p>';
        }
        type_arr.push(type_text);
    }
    var type_list = type_arr.join('<p class="spacer">+</p>');
    $('#content-list').html(type_list);
}

function main_selectFeature(elm) {
    $('.main-features-list li').removeClass('feature-on');
    $(elm).addClass('feature-on');
}

function main_initShowcase() {
    main_refreshShowcase();
    $('#content-images li .overlay').live('mouseover mouseout', function (event) {
        if (event.type == 'mouseover') {
            $(this).parent().animate({
                top: '+=15'
            }, 300);
            $(this).animate({
                opacity: '0.0'
            }, 300);
        } else {
            $(this).parent().animate({
                top: '-=15'
            }, 500);
            $(this).animate({
                opacity: '0.6'
            }, 300);
        }
    });
}

function main_refreshShowcase() {
    var client = $('#content-images').attr('client');
    var w_cont = $('#content-images').width();
    var i_count = $('#content-images li').size();
    var i_width = Math.round((w_cont - (i_count * 10)) / i_count);
    $('#content-images li').css('width', i_width + 'px');
    $('#content-images li').each(function () {
        var img = $(this).attr('img');
        $(this).css('background', 'url(content/' + client + '/' + img + '-tile.png) top left no-repeat');
        $(this).append('<div class="overlay"></div>');
    });
}
