function submitShare() {
    var contactName = $("#fromShareName").val();
    var contactEmail = $("#fromShare").val();
    var message = $("#messageShare").val();
    var recipients = [];
    var names = [];
    $("#toShare1,#toShare2,#toShare3").each(function() {
        var el = $(this);
        if (el.val() != "") {
            recipients.push(el.val());
        }
    });
    $("#toShareName1,#toShareName2,#toShareName3").each(function() {
        var el = $(this);
        if (el.val() != "") {
            names.push(escape(el.val()));
        }
    });
    $.ajax({
        type: "POST",
        url: "share.aspx/Submit",
        data: "{" +
                "'contactName':'" + contactName + "'," + //name
                "'contactEmail':'" + contactEmail + "'," + //email
                "'names':" + (names.length ? " ['" + names.join("','") + "']" : "[]") + "," + //names
                "'emails':" + (recipients.length ? " ['" + recipients.join("','") + "']" : "[]") + "," + //emails
                "'message':'" + escape(message) + "'" +
                "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            var o = result.d;
            if (!o.status) {
                //$("#statusShare").removeClass("success").text(o.message);
                $("#successsharemessage").hide();
                $("#backtocoupon").hide();
                $("#errorsharemessage").show();
                $("#sharemessage").removeClass("success").text(o.message);
                $("#sharemessagebox").show();
            }
            else if (o.status == 2) {
                alreadySubmittedCoupon();
            }
            else {//success
                //window.location(o.url);
                //$("#statusShare").addClass("success").html("Thank you! Your message was successfully sent.<br/> <a target='_blank' href='"+o.url+"'>Click here for your savings</a>!");
                $("#errorsharemessage").hide();
                $("#backtocoupon").hide();
                $("#successsharemessage").show();
                $("#successmessage").addClass("success").html("Thank you! Your message was successfully sent.<br/> <a onClick=\"openCoupon('" + o.url + "','/thankyou1.aspx');\">Click here for your savings</a>!");
                $("#sharemessagebox").show();
            }
        }
    });
}

function alreadySubmittedCoupon() {
    $("#successsharemessage").hide();
    $("#errorsharemessage").show();
    $("#sharemessage").removeClass("success").text("Looks like you've already shared this coupon.");
    $("#backtoform").hide();
    $("#backtocoupon").show();
    $("#sharemessagebox").show();
}

function showProductLinks() {
    hideInsBox();
    $("#productlinks").show();
}
function showIns(titleId, insId, headerTextId) {
    $("#productlinks").hide();
    hideIns();
    $("#ins_title_" + titleId).show();
    //$("#ins_text_" + insId).show();
    $("#ins_header_" + headerTextId).show();
    if (headerTextId == 1) {
        innerHTML = $("#ins_text_" + insId).html() + $("#btn_ins_back").html() + '<br /><br />';
        $("#btn_ins_back").hide()
    } else {
        innerHTML = $("#ins_text_" + insId).html();
        $("#btn_ins_back").show()
    }
    $('#scroll_container').html(innerHTML);
    $("#insbox").show();
    $('#scroll_container').jScrollPane({ showArrows: false, scrollbarWidth: 9 });
    $('#scroll_container')[0].scrollTo(0);

}
function hideInsBox() {
    $("#insbox").hide();
    $("#btn_ins_back").hide()
}
function hideIns() {
    for (x = 1; x <= 26; x++) {
        $("#ins_title_" + x).hide();
    }
    for (x = 1; x <= 11; x++) {
        $("#ins_text_" + x).hide();
    }
    $("#ins_header_1").hide();
    $("#ins_header_2").hide();
    $("#ins_header_3").hide();
    $("#ins_header_4").hide();
    $("#ins_btn_back").hide();
}

function openCoupon(couponURL, redirectURL) {
    window.open(couponURL);
    window.location = redirectURL;
}
function hideShareMessage() {
    $("#sharemessagebox").hide();
}

function clickShare() {
    $("body").removeClass().addClass("sharesocial");
}

function cancelShare() {
    $("body").removeClass().addClass("sharemain");
}

var footerWindow;
function openWin(url, w, h) { //if f passed, opens new window w/h size of window object
    if (w && h) {
        var n = window.open(url, 'cWin', 'height=' + h + ',width=' + w + ',scrollbars=yes,toolbar=yes,menu=yes,location=yes');
    }
    else {
        footerWindow = window.open(url, 'fWin', 'height=678,width=610,scrollbars=yes');
        if (window.focus) { footerWindow.focus() }
    }
}

function track() {
    var axel = Math.random() + "";
    var num = axel * 1000000000000000000;
    var spotpix = new Image();
    spotpix.src = "http://ad.doubleclick.net/activity;src=12345;type=foo;cat=bar;ord=" + num;
}
/*
function filedownload(href) {
var axel = Math.random() + "";
var num = axel * 1000000000000000000;
var spotpix = new Image();
spotpix.src = "http://ad.doubleclick.net/activity;src=12345;type=foo;cat=bar;ord=" + num;
spotpix.onload = passed();
}

function passed() {
top.location.href = "MyCoolFile.pdf";
}
*/
function dimTvAddsBackground() {
    $('#wrapper').css('background-position', 'top right');
}

var jsReady = false;

function isReady() { return jsReady; }
function pageInit() { jsReady = true; }

//Detecting the browser type
function getSWF(movieName) {

    return document[movieName] || window[movieName];

    /*
    if (navigator.appName.indexOf("Microsoft") != -1) {
    alert(window);
    return document[movieName];
    } else {
    alert('2');
    return document[movieName];
    }
    */
}

function changeVideo(value) {
    getSWF("flashContent").sendToActionScript(value);
}

$(document).ready(function() {
    pageInit();
    dimTvAddsBackground();
    // Expands the article share options
    $('#dont_upgrade').click(function() {
        $('#ie6_overlay').css('display', 'none');
        $('#ie6_message').css('display', 'none');
        return false;
    }
	);

    $("#load_videos li a").click(function(event) {
        event.preventDefault();

        var tvVideo = $(this).attr('href').replace('#', '');
        changeVideo(tvVideo);
    });

    $('#flashContent').hover(function() {
        dimTvAddsBackground();
    });

    hideShareMessage();
    hideInsBox();

    // Handles Mom's Slice Nav Arrows
    $('#nav li, #nav h3').click(function() {
        $('.arrow').removeClass('arrow');
        $(this).children('span').addClass('arrow');
    }
	);

	// New TV ADs sub Nav
    if ($('body.tvadds').length > 0)
	{
		callYoutubeXml();
	}

    // Tab on Box Tops Page
    $('#boxtops_tab_content1').css('display', 'block');

});

function dimTvAddsBackground() {
    $('#wrapper').css('background-position', 'top right');
}

function callYoutubeXml()
{
	var params      = { allowScriptAccess: 'always', wmode: 'transparent', allowfullscreen: 'true' };
    var atts        = { id: 'myytplayer' };
    var baseUrl     = 'http://www.youtube.com/v/';
    var ads =
    [
        { id: 'hoops', title: 'Hoops', code: 'uA32fsad8DY' },
        { id: 'music', title: 'Music', code: 'x2_LWLNetIQ' }
    ];

    for (var i = 0; i < ads.length; i++)
    {
        var id = ads[i].id;
        var title = ads[i].title;
        var code = ads[i].code;
        
        $('#load_videos').append('<li><a id="' + id + '" href="javascript:void(0);">' + title + '</a></li>');
        $('.tvadd_video').append('<div id="flashWrapper' + i + '" class="flashWrapper"><div id="flashContent' + i + '"></div></div>');
        swfobject.embedSWF(baseUrl + code + "&feature=youtube_gdata?enablejsapi=1&playerapiid=ytplayer", "flashContent" + i, "472", "301", "9", null, null, params, atts);

        if (i != 0)
        {
            $('#flashWrapper' + i).css('display','none');
        }
        else
        {
            $('#load_videos li:first-child').children('a').addClass('active');
        }
    }

    $('#load_videos li').each(function(index)
    {
        $(this).click(function()
        {
            $('.flashWrapper').css('display','none');
            $('.tvadd_video #flashWrapper'+index).css('display','block');

            $('.active').removeClass('active');
            $(this).children('a').addClass('active');
        });
    });
}
