﻿var currentTab = 'articles';
function ChangeTab(tab) {
    if (currentTab != tab) {
        $('#' + currentTab).fadeOut('fast', function () {
            $('#' + currentTab + 'Tab').removeClass('navButtonSelected');
            $('#' + tab + 'Tab').addClass('navButtonSelected');
            $('#' + tab).fadeIn('fast');

            currentTab = tab;
        });
    }
}

function ShowStories(filter) {
    $(".video").hide();
    $(".article").hide();

    if (filter == 0) {
        $(".article").show('slow');
    }
    else if (filter == 1) {
        $(".video").show('slow');
    }
    else {
        $(".video").show('slow');
        $(".article").show('slow');
    }
}

function ShowActions(node) {
    $("#" + node.id).find('.action').show();
}

function HideActions(node) {
    $("#" + node.id).find('.action').hide();
}

function HandleEnterContest(context) {

    // Load and deserialize the returned JSON data  
    var json = context.get_data();
    var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);

    if (data.Success) {
        alert('Thanks! You successfully entered the contest.');
    }
    else {
        alert('There was an error entering you in the contest. Please try again later.');
    }
    
    Redirect(window.location);
}

function HandleMarkAsVideo(context) {

    // Load and deserialize the returned JSON data  
    var json = context.get_data();
    var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);

    $('#mav-' + data.Id).remove();
    $('#article-' + data.Id).removeClass("article");
    $('#article-' + data.Id).addClass("video");
}

function HandleMarkAsArticle(context) {

    // Load and deserialize the returned JSON data  
    var json = context.get_data();
    var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);

    $('#maa-' + data.Id).remove();
    $('#video-' + data.Id).removeClass("video");
    $('#video-' + data.Id).addClass("article");
}

function S() {
    var sb = document.getElementById("searchBox");
    if (sb.value != '') {
        window.location = "/search?q=" + escape(sb.value);
    }
    else {
        alert("Please enter a search query.");
    }
}

function SKP() {
    if (event.keyCode == 13) { S(); }
}

function ShowStory(url)
{
    $('#page').hide('slow');
    setTimeout('Redirect("' + url + '")', 1000);
}

function Redirect(url)
{
    window.location.href = url;
}

function HideStory() {
    $('#message_box_content').hide('slow');
    $('#message_box').hide();
    
    SetFrameSource('');
    window.document.title = "Skatematic.com";
}

function SetFrameSource(url) {
    var cf = document.getElementById("contentFrame");
    cf.src = url;
}

function ReinitializeAddThis() {
    if (window.addthis) {
        window.addthis.ost = 0;
        window.addthis.ready();
    }
}

/* Modal PopUp */
function ShowPopUp(popID, popWidth) {
    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('<a href="#" onclick="ClosePopUp()" class="close"><img src="/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top': -popMargTop,
        'margin-left': -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
}

function ClosePopUp() {
    $('#fade , .popup_block').fadeOut(function () {
        $('#fade, a.close').remove();  //fade them both out
    });
    return false;
}
