$(document).ready(function() {

    $('.home_theme').innerfade({
        speed: 'slow',
        timeout: 6000,
        type: 'sequence',
        containerheight: '278px'
    });


    if (!($.browser.msie && $.browser.version == "6.0")) {
        $(".megamenu").megamenu();
    }
    else {
        $(document).pngFix({
            blankgif: '/images/blank.gif'
        });
    }    

    $(".screenshots > div.item a").lightBox({
        imageLoading: '/css/lightbox/images/lightbox-ico-loading.gif',
        imageBtnClose: '/css/lightbox/images/lightbox-btn-close.gif',
        imageBtnPrev: '/css/lightbox/images/lightbox-btn-prev.gif',
        imageBtnNext: '/css/lightbox/images/lightbox-btn-next.gif'
    });
    

    $(".screenshots > div div.item a").lightBox({
        imageLoading: '/css/lightbox/images/lightbox-ico-loading.gif',
        imageBtnClose: '/css/lightbox/images/lightbox-btn-close.gif',
        imageBtnPrev: '/css/lightbox/images/lightbox-btn-prev.gif',
        imageBtnNext: '/css/lightbox/images/lightbox-btn-next.gif'
    });


});

var signup_page;

function BuyTemplate(colorId, evt) {
    var newUrl = signup_page + "?id=" + colorId;
    window.document.location.href = newUrl;

    evt.preventDefault;
    return false;
}

function dynamicLightBoxinit(nodeId,evt) {

    var ajaxURL = "/TemplateLightBox.aspx?id=" + nodeId;
    $.ajax({
        type: "GET",
        url: ajaxURL,
        success: function(data, textStatus) {
            var obj = $.evalJSON(data);

            //$.dump(obj);
            
            var imageBuilder = '';
            for (var i = 0; i < obj.images.length; i++) {
                imageBuilder += '<a href="' + obj.images[i].Url + '" title="' + obj.images[i].Title + '">';
                imageBuilder += '<img src="/images/blank.gif" \></a>';
            }

            var lb = $(imageBuilder);
            lb.lightBox({
                imageLoading: '/css/lightbox/images/lightbox-ico-loading.gif',
                imageBtnClose: '/css/lightbox/images/lightbox-btn-close.gif',
                imageBtnPrev: '/css/lightbox/images/lightbox-btn-prev.gif',
                imageBtnNext: '/css/lightbox/images/lightbox-btn-next.gif'
            });
            lb.filter('a:first').click();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //alert(textStatus);
        }
    });   //end of .ajax function


    evt.preventDefault;
    return false;
}


function MakeEqual() {

    var sections = $(".template_items .item");
    for (var i = 0; i < sections.length; i += 4) {
    
        SetEqual(sections.eq(i), sections.eq(i + 1),
            sections.eq(i + 2), sections.eq(i + 3), ".title");
        SetEqual(sections.eq(i), sections.eq(i + 1),
            sections.eq(i + 2), sections.eq(i + 3), ".preview .img");
        SetEqual(sections.eq(i), sections.eq(i + 1),
            sections.eq(i + 2), sections.eq(i + 3), ".preview .colors");       
        
    }

}

function SetEqual(obj1, obj2, obj3, obj4, selector) {

    var height1, height2, height3, height4;

    height1 = obj1.find(selector).eq(0).height();
    height2 = height1;
    height3 = height1;
    height4 = height1;
    
    if (obj2) height2 = obj2.find(selector).eq(0).height();
    if (obj3) height3 = obj3.find(selector).eq(0).height();
    if (obj4) height4 = obj4.find(selector).eq(0).height();

    var max_height = height1;
    if (max_height < height2) max_height = height2;
    if (max_height < height3) max_height = height3;
    if (max_height < height4) max_height = height4;

    obj1.find(selector).height(max_height);
    if (obj2) obj2.find(selector).height(max_height);
    if (obj3) obj3.find(selector).height(max_height);
    if (obj4) obj4.find(selector).height(max_height);
    
}


function LoadColor(p,caller_id,evt,caller)
{
    var parent = 'item_' + p;
    var color = 'img_' + caller_id;
    var trigger = 'trigger_' + caller_id;
    var signbtn = 'buy_' + caller_id;

    //Show hide the appropriate sign up button
    $("#" + parent + " .signin").hide();
    $("#" + signbtn).show();

    //Show hide the appropriate trigger
    $("#" + parent + " .screenshots").hide();
    $("#" + trigger).show();
    
    //Show hide the appropriate template
    $("#" + parent + " .img").hide();
    $("#" + color).show();
    
    //Show hide the link
    $("#" + parent + " .colors a").removeClass('sel');
    $(caller).addClass('sel');
    
    //prevent default behavior
    evt.preventDefault;
    return false;
}
