﻿

var timer;
var axel = Math.random() + "";
var a = axel * 10000000000000;

var addthis_config = {
    ui_cobrand: "GimmeTheGig2",
    data_track_clickback: true,
    ui_click: true
};

var vvqparams = {
    wmode: "transparent", allowfullscreen: "true", allowscriptacess: "always"
};


function addCommas(nStr) {
    nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2;
}

function addSlashes(nStr) {
    nStr = nStr.replace(/\'/g, '\\\'');
    nStr = nStr.replace(/\"/g, '\\"');
    nStr = nStr.replace(/\\/g, '\\\\');
    nStr = nStr.replace(/\0/g, '\\0');
    return nStr;
}

function ReinitializeAddThis() {
    if (window.addthis) {
        window.addthis.ost = 0;
        window.addthis.ready();
    }
}

function moveOnMax(field, nextFieldID) {
    if (field.value.length >= field.maxLength) {
        document.getElementById(nextFieldID).focus();
    }
}

function getNewUser() {
    var S4 = function() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function userExists() {
    var exists = false;
    var userCookie = ReadCookie("userFor");
    
    //alert("userCookie exists = " + userCookie);
    
    if(userCookie.length > 0) {
        exists = true;
    }
    return exists;
}

function getUser() {
    if (userExists() == false) {
        var newUser = getNewUser();
        //alert("new user = " + newUser);
        return newUser
    } else {
        var userCookie = ReadCookie("userFor");
        //alert("get userCookie = " + userCookie);
        return userCookie
    }
}

function hasAlreadyVoted(submissionId) {
    var voted = false;
    var voteCookie = ReadCookie("votedFor");
    var voteArray = voteCookie.split(',');

    //alert("voteCookie = " + voteCookie);
    //alert("voteArray.length = " + voteArray.length);
    
    for (var v = 0; v < voteArray.length; v++) {
        //alert('value Id = ' + voteArray[v]);
        if (voteArray[v] == submissionId) {
            voted = true;
        }
    }
    return voted;
}

function hasAlreadyShared(submissionId) {
    var shared = false;
    var shareCookie = ReadCookie("sharedFor");
    var shareArray = shareCookie.split(',');

    //alert("shareCookie = " + shareCookie);
    //alert("shareArray.length = " + shareArray.length);

    for (var v = 0; v < shareArray.length; v++) {
        //alert('value Id = ' + shareArray[v]);
        if (shareArray[v] == submissionId) {
            shared = true;
        }
    }
    return shared;
}

function hasAlreadyTrivia(submissionId) {
    var trivia = false;
    var triviaCookie = ReadCookie("triviaFor");
    var triviaArray = triviaCookie.split(',');

    //alert("triviaCookie = " + triviaCookie);
    //alert("triviaArray.length = " + triviaArray.length);

    for (var v = 0; v < triviaArray.length; v++) {
        //alert('does array value of ' + triviaArray[v] + ' = submissionId of ' + submissionId);
        if (triviaArray[v] == submissionId) {
            trivia = true;
        }
    }
    return trivia;
}

function CookieTheUser(userName) {
    //alert("userFor = " + userName);
    SetCookie("userFor", userName, 1);
}

function CookieTheVote(submissionId) {
    var voteCookie = ReadCookie("votedFor");

    if (voteCookie.length > 0) {
        voteCookie = voteCookie + "," + submissionId;
    } else {
        voteCookie = submissionId;
    }

    //alert("votedFor = " + voteCookie);
    
    SetCookie("votedFor", voteCookie, 1);
}

function CookieTheShare(submissionId) {
    var shareCookie = ReadCookie("sharedFor");

    if (shareCookie.length > 0) {
        shareCookie = shareCookie + "," + submissionId;
    } else {
        shareCookie = submissionId;
    }

    //alert("shareCookie = " + shareCookie);

    SetCookie("sharedFor", shareCookie, 1);
}

function CookieTheTrivia(submissionId) {
    var triviaCookie = ReadCookie("triviaFor");

    if (triviaCookie.length > 0) {
        triviaCookie = triviaCookie + "," + submissionId;
    } else {
        triviaCookie = submissionId;
    }

    //alert("triviaFor = " + triviaCookie);

    SetCookie("triviaFor", triviaCookie, 1);
}

function SetCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue)
                 + ";expires=" + expire.toGMTString();

    //alert("\nName: " + cookieName + "\nValue: " + cookieValue + "\nExpires On: " + expire.toGMTString());
}

function ReadCookie(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}

/* JQuery Load/Empty Pages */
function LoadContentAreas() {
    $(".navigation").load('i-navigation.aspx');
    $(".tour").load('i-tour.aspx');
    $(".video-gallery").load('i-gallery.aspx?x=' + a);
    $(".fans").load('i-fan.aspx');
    $(".tweets").load('i-tweet.aspx');
    $(".twitpic").load('i-twitpic.aspx');
    $(".blog").load('i-blog.aspx');
}

function EmptyContentAreas() {
    $(".navigation").empty();
    $(".tour").empty();
    $(".video-gallery").empty();
    $(".fans").empty();
    $(".tweets").empty();
    $(".twitpic").empty();
    $(".blog").empty();
}

function LoadCurture(iCulture) {
    var srcCulture = "";
    var youTubeId = "";
    switch (iCulture) {
        case "1":
            srcCulture = "spanish";
            //youTubeId = "aGFA7rIcD7k";
            youTubeId = "tDtGTbr1y7Y";
            $("#header-contents .language-links .english").show();
            $("#header-contents .enter").attr('style', 'top:217px;');
            $("#more-info .link").attr('style', 'left:355px;top:185px;');
            $("#footer-contents .focus-link").html('<a href="http://www.focusexperience.com/focus/eng" onclick="javascript:pageTracker._trackPageview(\'/gtg2/exit_footer_focus_spanish_image_click\');" target="_blank" title="2012 Ford Focus"><img src="images/spacer.gif" border="0" alt="2012 Ford Focus" width="700" height="200" /></a>');
            $("#footer-contents .focus-optin").html('<a href="http://www.focusexperience.com/focus/eng" onclick="javascript:pageTracker._trackPageview(\'/gtg2/exit_footer_focus_moreInfo_spanish_button_click\');" target="_blank" title="Click here for more information"><img src="images/more_info.png" border="0" alt="Click here for more information" /></a>');
            break;
        case "2":
            srcCulture = "korean";
            //youTubeId = "E1PjnGgmFSY";
            youTubeId = "9fsRf2qeZxU";
            $("#header-contents .language-links .english").show();
            $("#header-contents .enter").attr('style', 'top:234px;');
            $("#more-info .link").attr('style', 'left:430px;top:180px;');
            $("#footer-contents .focus-link").html('<a href="http://www.focusexperience.com/focus/kor" onclick="javascript:pageTracker._trackPageview(\'/gtg2/exit_footer_focus_korean_image_click\');" target="_blank" title="2012 Ford Focus"><img src="images/spacer.gif" border="0" alt="2012 Ford Focus" width="700" height="200" /></a>');
            $("#footer-contents .focus-optin").html('<a href="http://www.focusexperience.com/focus/kor" onclick="javascript:pageTracker._trackPageview(\'/gtg2/exit_footer_focus_moreInfo_korean_button_click\');" target="_blank" title="Click here for more information"><img src="images/more_info.png" border="0" alt="Click here for more information" /></a>');
            break;
        case "3":
            srcCulture = "chinese";
            //youTubeId = "Ck5_gG7AcWc";
            youTubeId = "83gkNCkLI3M";
            $("#header-contents .language-links .english").show();
            $("#header-contents .enter").attr('style', 'top:222px;');
            $("#more-info .link").attr('style', 'left:474px;top:196px;');
            $("#footer-contents .focus-link").html('<a href="http://www.focusexperience.com/focus/chi" onclick="javascript:pageTracker._trackPageview(\'/gtg2/exit_footer_focus_chinese_image_click\');" target="_blank" title="2012 Ford Focus"><img src="images/spacer.gif" border="0" alt="2012 Ford Focus" width="700" height="200" /></a>');
            $("#footer-contents .focus-optin").html('<a href="http://www.focusexperience.com/focus/chi" onclick="javascript:pageTracker._trackPageview(\'/gtg2/exit_footer_focus_moreInfo_chinese_button_click\');" target="_blank" title="Click here for more information"><img src="images/more_info.png" border="0" alt="Click here for more information" /></a>');
            break;
        default:
            srcCulture = ""; //english. no value needed.
            youTubeId = "JcKBJBD7JQo";
            $("#header-contents .language-links .english").hide();
    }
    if (srcCulture.length > 0) {
        //swap images
        $("#header").css("background", "url(images/header_" + srcCulture + ".jpg) transparent");
        //$("#header-contents .enter img").attr('src', 'images/VBAGYFBTG_' + srcCulture + '.png');
        $("#header-contents .more-info img").attr('src', 'images/CHFMI_link_' + srcCulture + '.jpg');
        $("#footer").css("background", "url(images/focus_footer_" + srcCulture + ".jpg) transparent");
        $("#footer-contents .focus-optin img").attr('src', 'images/more_info_' + srcCulture + '.png');
        $("#more-info .header img").attr('src', 'images/AYAAM_header_' + srcCulture + '.jpg');
        $("#more-info .text img").attr('src', 'images/AYAAM_text_' + srcCulture + '.png');
        $("#more-info .bottom-close-button img").attr('src', 'images/AYAAM_close_window_' + srcCulture + '.png');
        $("#disclaimer .text img").attr('src', 'images/disclaimer_text_' + srcCulture + '.png');
        $("#header-contents .gimmethegear").hide();
        //swap video
        $('#header-video').html('<object width="415" height="236" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"><param name="movie" value="http://www.youtube.com/v/' + youTubeId + '&fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' + youTubeId + '&fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="415" height="236" wmode="transparent"></embed></object>');
        //track
        pageTracker._trackPageview('/gtg2/change_language_to_' + srcCulture + '_click');
    } else {
        //default video
        $('#header-video').html('<object width="415" height="236" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"><param name="movie" value="http://www.youtube.com/v/' + youTubeId + '&fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' + youTubeId + '&fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="415" height="236" wmode="transparent"></embed></object>');
    }
}

function SearchVideo(byValLetter) {
    //track
    pageTracker._trackPageview('/gtg2/search_bands_letter_' + byValLetter + '_click');
    //post
    $.post("i-gallery.aspx", { letter: byValLetter },
    function(data) {
        $('.video-gallery').fadeOut('slow').html(data).fadeIn('slow');
    });
}

function SearchTour() {
    //get zipcode
    var byValZipcode = $("#Homepage_Tour_txtZip").val();
    //track
    pageTracker._trackPageview('/gtg2/search_tour_' + byValZipcode + '_click');
    //post
    $.post("i-tour.aspx", { zipcode: byValZipcode },
    function(data) {
        $('.tour').fadeOut('slow').html(data).fadeIn('slow');
    });
}

function SearchTourOnTourPage() {
    //get zipcode
    var byValZipcode = $("#Tour_txtZip").val();
    //track
    pageTracker._trackPageview('/gtg2/search_tour_on_tour_page_' + byValZipcode + '_click');
    //post
    $.post("i-tour-big.aspx", { zipcode: byValZipcode },
    function(data) {
        $('.tour-grid').fadeOut('slow').html(data).fadeIn('slow');
    });
}

/* Validation */
function ValidateRegChecked(oSrc, args) {
    if (document.getElementById("frmRegistration_chkRulesRead").checked == false) {
        args.IsValid = false;
    }
}
function ValidateGroupieChecked(oSrc, args) {
    if (document.getElementById("frmGroupie_chkGroupieRulesRead").checked == false) {
        args.IsValid = false;
    }
}
function ValidateGroupie2Checked(oSrc, args) {
    if (document.getElementById("GroupieFormPostTriviaShare1_chkGroupie2RulesRead").checked == false) {
        args.IsValid = false;
    }
}

/* Pop-Up Pages */
function PopPrivacy() {
    var winURL = 'http://www.ford.com/help/privacy/';
    var winName = 'Rules';
    var winWidth = 1024;
    var winHeight = 550;
    var winLeft = (screen.width - winWidth) / 2;
    var winTop = (screen.height - winHeight) / 2;
    var winProps = 'height=' + winHeight + ',width=' + winWidth + ',top=' + winTop + ',left=' + winLeft + ',scrollBars=1,resizable=1,toolbar=0,directories=0,status=0,menuBar=0';
    var win = window.open(winURL, winName, winProps);
    win.focus();

    pageTracker._trackPageview('/gtg2/exit_to_ford_privacy_policy');
}

function PopRegistrationExit() {
    var winURL = 'http://www.fordvehicles.com/focus';
    var winName = 'ExitFocus';
    var winWidth = 1024;
    var winHeight = 550;
    var winLeft = (screen.width - winWidth) / 2;
    var winTop = (screen.height - winHeight) / 2;
    var winProps = 'height=' + winHeight + ',width=' + winWidth + ',top=' + winTop + ',left=' + winLeft + ',status=yes,resizable=yes';
    var win = window.open(winURL, winName, winProps);
    win.focus();

    pageTracker._trackPageview('/gtg2/close_registration_exit_to_fordvehicles.com');

    return false;
}

function PopRegistrationRules() {
    var winURL = 'RegistrationRules.html';
    var winName = 'Rules';
    var winWidth = 560;
    var winHeight = 350;
    var winLeft = (screen.width - winWidth) / 2;
    var winTop = (screen.height - winHeight) / 2;
    var winProps = 'height=' + winHeight + ',width=' + winWidth + ',top=' + winTop + ',left=' + winLeft + ',scrollBars=1,resizeable=1,toolbar=0,directories=0,status=0,menuBar=0';
    var win = window.open(winURL, winName, winProps);
    win.focus();

    pageTracker._trackPageview('/gtg2/open_registration_rules');
}

function PopGroupieRules() {
    var winURL = 'GroupieRules.html';
    var winName = 'Rules';
    var winWidth = 560;
    var winHeight = 350;
    var winLeft = (screen.width - winWidth) / 2;
    var winTop = (screen.height - winHeight) / 2;
    var winProps = 'height=' + winHeight + ',width=' + winWidth + ',top=' + winTop + ',left=' + winLeft + ',scrollBars=1,resizeable=1,toolbar=0,directories=0,status=0,menuBar=0';
    var win = window.open(winURL, winName, winProps);
    win.focus();

    pageTracker._trackPageview('/gtg2/open_fan_rules');
}

/* Open Div Layers */
function OpenYouTubeError() {

    show_dialog('Invalid YouTube URL', 'YouTube video URL must be in the correct format<br/>Please use a format like the following:<br/>http://www.youtube.com/watch?v=');
    
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";
    $('#registration').fadeIn('slow');

    scroll(0, 0);
    
    pageTracker._trackPageview('/gtg2/reopen_registration_form_youtube_error');
}

function OpenDonWasNews() {
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";

    $('#Don-Was-News').fadeIn('slow');

    scroll(0, 0);

    EmptyContentAreas();

    pageTracker._trackPageview('/gtg2/open_DonWasNews');
}

function OpenMoreInfo() {
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";
    
    $('#more-info').fadeIn('slow');
    
    scroll(0, 0);
    
    EmptyContentAreas();
    
    pageTracker._trackPageview('/gtg2/open_moreinfo_form');
}

function OpenTake5(youTubeId) {
    document.getElementById("overlay").style.visibility = "visible";
    $('#take5').fadeIn('slow');
    $('#take5 .form .video').html('<object width="415" height="270" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"><param name="movie" value="http://www.youtube.com/v/' + youTubeId + '&fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' + youTubeId + '&fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="415" height="270" wmode="transparent"></embed></object>');
    scroll(0, 0);
    EmptyContentAreas();
    pageTracker._trackPageview('/gtg2/open_take5_video_' + youTubeId);
}

function CloseTake5() {
    document.getElementById("overlay").style.visibility = "hidden";
    $('#take5').hide();
    $('#take5 .form .video').html('');
    document.getElementById('take5-top').scrollIntoView(true);
    LoadContentAreas();
    pageTracker._trackPageview('/gtg2/close_take5_video');
}
function OpenGroupieClosedDialog() {
    show_dialog('Fan Giveaway', 'This contest has ended. Please check back again for other exciting opportunities to win prizes!');
}
function OpenBandClosedDialog() {
    show_dialog('Not A Finalist Band', 'This band has not been picked as a finalist. Please check out some of the other exciting bands!');
}
function OpenGroupieForm() {
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";

    //document.getElementById("groupie").style.visibility = "visible";
    $('#groupie').fadeIn('slow');

    scroll(0, 0);

    EmptyContentAreas();
    
    pageTracker._trackPageview('/gtg2/open_groupie_form');
}
function OpenPostTriviaGroupieForm(thankyouMessage, whichForm, bandName, iSubmissionId, user) {
    scroll(0, 0);
    EmptyContentAreas();
    
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";
    $('#groupie2').fadeIn('slow');
    $('#GroupieFormPostTriviaShare1_lblGroupie2ThankYouMsg').html(thankyouMessage);

    document.getElementById("GroupieFormPostTriviaShare1_WhichForm").value = whichForm;
    document.getElementById("GroupieFormPostTriviaShare1_BandName").value = bandName;
    document.getElementById("GroupieFormPostTriviaShare1_SubmissionID").value = iSubmissionId;
    document.getElementById("GroupieFormPostTriviaShare1_User").value = user;
        
    pageTracker._trackPageview('/gtg2/open_posttriviagroupie_form_from_' + whichForm + '_form');
}
function ClosePostTriviaGroupieForm(whichForm) {
    scroll(0, 0);
    EmptyContentAreas();
    
    document.getElementById("overlay").style.visibility = "visible";
    document.getElementById("header-video").style.visibility = "hidden";
    $('#youtube').show();
    $('#groupie2').hide();
    if (whichForm == 'trivia') {
        $('#trivia-button').hide();
    } else {
        $('#share-button').hide();
    }
    
    pageTracker._trackPageview('/gtg2/close_posttriviafan_form_from_' + whichForm + '_form');
}
function OpenGroupieThankYou() {
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";
    document.getElementById("groupie-thankyou").style.visibility = "visible";
    
    scroll(0, 0);
    
    EmptyContentAreas();
    
    pageTracker._trackPageview('/gtg2/open_groupie_thankyou');
}

function OpenRegistrationForm() {
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";
    //document.getElementById("registration").style.visibility = "visible";
    $('#registration').fadeIn('slow');

    scroll(0, 0);

    EmptyContentAreas();
    
    pageTracker._trackPageview('/gtg2/open_registration_form');
}

function CloseMoreInfoOpenRegistrationForm() {
    $('#more-info').hide();
    OpenRegistrationForm();
    pageTracker._trackPageview('/gtg2/close_moreinfo_form');
}

function OpenVoteAndPlayOptInForm() {
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("play-video").innerHTML = "";
    document.getElementById("overlay").style.visibility = "visible";
    $('#youtube').hide();

    //document.getElementById("optin").style.visibility = "visible";
    $('#optin').fadeIn('slow');

    scroll(0, 0);

    EmptyContentAreas();

    pageTracker._trackPageview('/gtg2/open_new_optin_form');
}

function OpenVoteAndPlayOptInThankYou() {
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";
    document.getElementById("optin-thankyou").style.visibility = "visible";

    scroll(0, 0);

    EmptyContentAreas();    
    
    pageTracker._trackPageview('/gtg2/open_optin_thankyou');
}

function OpenRegistrationThankYou() {
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";
    document.getElementById("registration-thankyou").style.visibility = "visible";

    scroll(0, 0);

    EmptyContentAreas();
    
    pageTracker._trackPageview('/gtg2/open_registration_thankyou');
}

/* Close Div Layers */
function CloseRegistationForm() {
    document.getElementById("header-video").style.visibility = "visible";
    $('#youtube').hide();
    document.getElementById("overlay").style.visibility = "hidden";
    $('#registration').hide();
    scroll(0, 0);
    LoadContentAreas();
    pageTracker._trackPageview('/gtg2/close_registration_form');
}

function CloseRegistationThankYouForm() {
    document.location.href = 'Default.aspx';
    //PopRegistrationExit();
}

function CloseGroupieForm() {
    document.getElementById("header-video").style.visibility = "visible";
    $('#youtube').hide();
    document.getElementById("overlay").style.visibility = "hidden";
    $('#groupie').hide();
    scroll(0, 0);
    LoadContentAreas();
    pageTracker._trackPageview('/gtg2/close_fan_form');
}

function CloseGroupieThankYouForm() {
    document.location.href = 'Default.aspx';

    pageTracker._trackPageview('/gtg2/close_fan_thankyou');
}

function CloseDonWasNews() {
    document.getElementById("header-video").style.visibility = "visible";
    document.getElementById("overlay").style.visibility = "hidden";
    $('#Don-Was-News').hide();
    scroll(0, 0);
    LoadContentAreas();
    pageTracker._trackPageview('/gtg2/close_DonWasNews');
}


function CloseMoreInfoForm() {
    document.getElementById("header-video").style.visibility = "visible";
    document.getElementById("overlay").style.visibility = "hidden";
    $('#more-info').hide();
    scroll(0, 0);
    LoadContentAreas();
    pageTracker._trackPageview('/gtg2/close_moreinfo_form');
}

function CloseVoteAndPlayOptInForm() {
    document.getElementById("header-video").style.visibility = "visible";
    $('#youtube').hide();
    document.getElementById("overlay").style.visibility = "hidden";
    $('#optin').hide();
    scroll(0, 0);
    LoadContentAreas();
    pageTracker._trackPageview('/gtg2/close_optin_form');
}

function CloseVoteAndPlayOptInThankYouForm() {
    document.getElementById("header-video").style.visibility = "visible";
    $('#youtube').hide();
    document.getElementById("overlay").style.visibility = "hidden";
    document.getElementById("optin-thankyou").style.visibility = "hidden";
    $('#optin').hide();
    
    scroll(0, 0);

    LoadContentAreas();

    pageTracker._trackPageview('/gtg2/close_optin_thankyou');
}


function OpenVideoForm(submissionId, youTubeId, videoTitle, bandName, votes, facebook, twitter, myspace, bandBio) {
    var user = getUser();
    var moreInfo = "";
    var trackingInfo = "";
    var cookieInfo = "";

    //Show Play and Vote Layer
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";
    $('#youtube').fadeIn('slow');

    //alert("user = " + user);
    EmptyContentAreas();
    document.getElementById("frmVote_SubmissionID").value = submissionId;
    document.getElementById("frmVote_User").value = user;

    //Build Learn More Info
    moreInfo = "Check out the following links to learn more about <strong>" + bandName + "</strong><br/><div id='band-links'>";
    if (facebook != "") {
        if (facebook.indexOf("http://") != -1) { "http://" + facebook }
        moreInfo = moreInfo + '<li><a target="_blank" href="' + facebook + '" onclick="pageTracker._trackPageview(\'/gtg2/exit/facebook_bandname_' + escape(bandName) + '\');">Facebook Page</a></li>';
    } else { 
        moreInfo = moreInfo + '<br/>';
    }
    if (twitter != "") {
        if (twitter.indexOf("http://") != -1) { "http://" + twitter }
        moreInfo = moreInfo + '<li><a target="_blank" href="' + twitter + '" onclick="pageTracker._trackPageview(\'/gtg2/exit/twitter_bandname_' + escape(bandName) + '\');">Twitter Page</a></li>';
    } else {
        moreInfo = moreInfo + '<br/>';
    }
    if (myspace != "") {
        if (myspace.indexOf("http://") != -1) { "http://" + myspace }
        moreInfo = moreInfo + '<li><a target="_blank" href="' + myspace + '" onclick="pageTracker._trackPageview(\'/gtg2/exit/myspace_bandname_' + escape(bandName) + '\');">Myspace Page</a></li>';
    } else {
        moreInfo = moreInfo + '<br/>';
    }
    moreInfo = moreInfo + '</div>';

    //Set Values of Play and Vote Layer
    document.getElementById("play-video").innerHTML = 'loading video...';
    document.getElementById("video-title").innerHTML = videoTitle;
    document.getElementById("band-name").innerHTML = bandName;
    //document.getElementById("band-bio").innerHTML = bandBio;
    document.getElementById("current-votes").innerHTML = '<span class="total-votes">Total Votes:</span> ' + addCommas(votes);
    document.getElementById("more-info-band").innerHTML = moreInfo;
    
    //Open fun fact
    OpenFunFact();

    //Set timeout for Fun Fact - once timeout has passed - write the YouTube video
    timer = setTimeout(function() { CloseFunFact(youTubeId) }, 10000);

    //Test to see if user has already voted for this video
    if (hasAlreadyVoted(submissionId) == true) {
        document.getElementById("vote-button").innerHTML = '<img src="images/you_like_this.png" /> You like this!';
    }

    //Test to see if user has already shared this video
    if (hasAlreadyShared(submissionId) == true) {
        $('#share-button').hide();
    } else {
        $('#share-button').show();
    }

    //Test to see if user has already played trivia for this video
    if (hasAlreadyTrivia(submissionId) == true) {
        $('#trivia-button').hide();
    } else {
        $('#trivia-button').show();
    }
    
    //Move play video form into view
    scroll(0, 0);

    pageTracker._trackPageview('/gtg2/open_youtube_video_' + youTubeId);
}

function OpenFunFact() {
    var n = 12;
    var randomNumber = Math.floor(Math.random() * n + 1);

    if (randomNumber > n) {
        randomNumber = 1;
    }

    //Write the fun fact to the video layer
    var div = document.getElementById("play-video");
    div.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="369" height="230" id="ViewFunFact" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="wmode" value="transparent"><param name="FlashVars" value="imageFunFact=fun_facts/' + randomNumber + '.jpg"><param name="movie" value="ViewFunFact.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="ViewFunFact.swf" FlashVars="imageFunFact=fun_facts/' + randomNumber + '.jpg" quality="high" bgcolor="#ffffff" width="369" height="230" wmode="transparent" name="ViewFunFact" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" /></object>';
    
    pageTracker._trackPageview('/gtg2/open_fun_fact' + randomNumber);
}

function CloseFunFact(youTubeId) {
    //Timeout has expired - write the selected YouTube video to the video layer
    var div = document.getElementById("play-video");
    div.innerHTML = '';
    div.innerHTML = '<object width="370" height="230" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"><param name="movie" value="http://www.youtube.com/v/' + youTubeId + '&hl=en&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' + youTubeId + '&hl=en&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="370" height="230" wmode="transparent"></embed></object>';
}

function CastVote() {
    document.getElementById("vote-image").src = "images/async.gif";
    var bandName = document.getElementById("band-name").innerHTML;
    var iSubmissionId = document.getElementById("frmVote_SubmissionID").value;
    var user = document.getElementById("frmVote_User").value;
    
    $.post("i-vote.aspx", { sid: iSubmissionId, band: escape(bandName) },
    function(data) {
        $('#vote-button').html(data);
    });

    CookieTheVote(iSubmissionId);
    CookieTheUser(user);
        
    pageTracker._trackPageview('/gtg2/vote_for_submission_' + iSubmissionId);
}

function CloseVideoForm() {
    document.getElementById("header-video").style.visibility = "visible";
    document.getElementById("play-video").innerHTML = "";
    document.getElementById("overlay").style.visibility = "hidden";
    $('#youtube').hide();
    
    clearTimeout(timer);

    LoadContentAreas();
    LoadCurture('0');

    document.getElementById("vote-button").innerHTML = '<a href="javascript:;" onclick="javascript:CastVote();"><img id="vote-image" src="images/video_form_vote_button.png" border="0" /></a>';
}

/* DrawContent */
function DrawContent(sContent, sContentHeight, sContentWidth, sContentType, sOptions) {    
    if (sContentType.indexOf("x-shockwave-flash") >= 0) {
        document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" height="' + sContentHeight + '" width="' + sContentWidth + '">');
        document.write('	<param name="allowScriptAccess" value="sameDomain" />');
        document.write('	<param name="movie" value="' + sContent + '?' + sOptions + '">');
        document.write('	<param name="quality" value="high">');
        document.write('	<param name="play" value="true">');
        document.write('	<param name="id" value="flashmovie">');
        document.write('	<param name="wmode" value="transparent">');
        document.write('	<param name="bgcolor" value="#ffffff">');
        document.write('	<embed bgcolor="#ffffff" height="' + sContentHeight + '" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' + sContent + '?' + sOptions + '" type="application/x-shockwave-flash" width="' + sContentWidth + '" allowScriptAccess="sameDomain" quality="high" wmode="transparent" play="true">');
        document.write('</object>');
    } else if (sContentType.indexOf("image") >= 0) {
        document.write('<img src="' + sContent + '" width="' + sContentWidth + '" height="' + sContentHeight + '" border="0" usemap="#headerMap"/>');
    } else {
        document.write(sContent);
    }
}

/* JQuery Dialog */
function show_dialog(sTitle, sMessage) {
    $('<p style="text-align:left;">' + sMessage + '</p>').dialog({ modal: true, width: 400, height: 300, title: sTitle, buttons: { Ok: function() { $(this).dialog("close"); } } });
}

function show_dialog_empty_panels(sTitle, sMessage) {
    $('<p style="text-align:left;">' + sMessage + '</p>').dialog({ modal: true, width: 400, height: 300, title: sTitle, buttons: { Ok: function() { EmptyContentAreas(); $(this).dialog("close"); } } });
}

function show_dialog_load_panels(sTitle, sMessage) {
    $('<p style="text-align:left;">' + sMessage + '</p>').dialog({ modal: true, width: 400, height: 300, title: sTitle, buttons: { Ok: function() { LoadContentAreas(); $(this).dialog("close"); } } });
}

/* Trivia */
function do_trivia() {
    var user = document.getElementById("frmVote_User").value;
    var iSubmissionId = document.getElementById("frmVote_SubmissionID").value;
    var bandName = document.getElementById("band-name").innerHTML;

    try {
        pageTracker._trackPageview('/gtg2/open_play_trivia_click_for_submission_' + iSubmissionId);
    } catch (err) { }

    //Show Trivia Layer
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";
    
    EmptyContentAreas();
    $('#youtube').hide();
    $('#trivia').fadeIn('slow');

    document.getElementById("frmTrivia_BandName").value = bandName;
    document.getElementById("frmTrivia_SubmissionID").value = iSubmissionId;
    document.getElementById("frmTrivia_User").value = user;
}

function CloseTriviaForm() {
    $('#youtube').fadeIn('slow');
    $('#trivia').hide();
}

/* Share To Facebook */
function OpenSharingForm() {
    var user = document.getElementById("frmVote_User").value;
    var iSubmissionId = document.getElementById("frmVote_SubmissionID").value;
    var bandName = document.getElementById("band-name").innerHTML;

    try {
        pageTracker._trackPageview('/gtg2/open_play_trivia_click_for_submission_' + iSubmissionId);
    } catch (err) { }

    //Show Trivia Layer
    document.getElementById("header-video").style.visibility = "hidden";
    document.getElementById("overlay").style.visibility = "visible";

    EmptyContentAreas();
    $('#youtube').hide();
    $('#sharing').fadeIn('slow');

    document.getElementById("frmShare_BandName").value = bandName;
    document.getElementById("frmShare_SubmissionID").value = iSubmissionId;
    document.getElementById("frmShare_User").value = user;
}

function CloseSharingForm() {
    $('#youtube').fadeIn('slow');
    $('#sharing').hide();
}

function CheckTriviaAnswer() {
    var user = document.getElementById("frmTrivia_User").value;
    var iSubmissionId = document.getElementById("frmTrivia_SubmissionID").value;
    var bandName = document.getElementById("frmTrivia_BandName").value;
    var iQuestionID = document.getElementById("frmTrivia_dlQuestion_ctl00_QuestionID").value;
    var iRightAnswer = document.getElementById("frmTrivia_dlQuestion_ctl00_RightAnswer").value;
    var iSelectedAnswer = $("input[name='frmTrivia$rbAnswerList']:checked").val();

    var captchaInfo = {
        challengeValue: Recaptcha.get_challenge(), responseValue: Recaptcha.get_response()
    };
    
    //show_dialog('Debug Output', 'user = ' + user + '<br/>iSubmissionId = ' + iSubmissionId + '<br/>iQuestionID = ' + iQuestionID + '<br/>iSelectedAnswer = ' + iSelectedAnswer + '<br/>iRightAnswer=' + iRightAnswer);

    if (hasAlreadyTrivia(iSubmissionId) == false) {
        $.ajax({
            type: 'POST',
            url: 'captcha.asmx/ValidateCaptcha',
            data: JSON.stringify(captchaInfo),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(msg) {
                alert(msg.d); // Either true o false, true indicates CAPTCHA is validated successfully.
                if(msg.d == false) {
                    show_dialog('Captcha Validation', 'You have typed the validation code incorrectly. Please verify try again!');
                }
            }
        });
        if (iSelectedAnswer == iRightAnswer) {
            //right answer and not played
            $.post("i-bonus.aspx", { sid: iSubmissionId, aid: 2, uid: user },
            function(data) {
                show_dialog('Thank You', 'You have successfully answered the trivia question and added votes for ' + addSlashes(bandName) + '! Come back tomorrow and do it again!');
            });
            //cookie
            CookieTheTrivia(iSubmissionId);
            CookieTheUser(user);
            //close trivia layer - show youtube layer - remove trivia button
            $('#youtube').fadeIn('slow');
            $('#trivia').hide();
            document.getElementById("trivia-button").innerHTML = "";
        } else {
            //did not answer correctly
            show_dialog('Answered Incorrectly', 'You have not answered the question correctly.<br />The good news is, you can try again!');
        }
    } else {
        //already played trivia
        show_dialog('Already Answered Trivia', 'You have already answered the Trivia question for the day.<br />Come back tomorrow and try again!');
        CookieTheUser(user);
        //close trivia layer - show youtube layer - remove trivia button
        $('#youtube').fadeIn('slow');
        $('#trivia').hide();
        document.getElementById("trivia-button").innerHTML = "";
    }
}

/* Facebook Share */ 
function do_share() {
    var user = document.getElementById("frmShare_User").value;
    var iSubmissionId = document.getElementById("frmShare_SubmissionID").value;
    var bandName = document.getElementById("frmShare_BandName").value;
    var shareMsg = 'I just voted for ' + addSlashes(bandName) + ' @ GimmeTheGigII and you should too!';

    //alert('user = ' + user);
    //alert('iSubmissionId = ' + iSubmissionId);
    //alert('bandName = ' + addSlashes(bandName));
    //alert('shareMsg = ' + shareMsg);

    //show_dialog('Debug Output', 'user = ' + user + '<br/>iSubmissionId = ' + iSubmissionId + '<br/>bandName = ' + bandName + '<br/>shareMsg = ' + shareMsg);
    
    try {
        pageTracker._trackPageview('/gtg2/share_to_facebook_click_for_submission_' + iSubmissionId);
    } catch (err) { }
    //
    //Pop sharing dialog
    /*
        http://www.facebook.com/dialog/feed?
        app_id=123050457758183&
        link=http://developers.facebook.com/docs/reference/dialogs/&
        picture=http://fbrell.com/f8.jpg&
        name=Facebook%20Dialogs&
        caption=Reference%20Documentation&
        description=Dialogs%20provide%20a%20simple,%20consistent%20interface%20for%20applications%20to%20interact%20with%20users.&
        message=Facebook%20Dialogs%20are%20so%20easy!&
        redirect_uri=http://www.example.com/response
    */
    FB.ui({
        app_id: '183937014988800',
        method: 'stream.publish',
        display: 'popup',
        name: shareMsg,
        link: 'http://www.gimmethegig.com',
        picture: 'http://www.gimmethegig.com/images/gimmethegig2_logo.jpg',
        caption: 'GimmeTheGig II: 3x Bigger, Badder, & Better. 3x the Winners. 3x the Shot at Fame.',
        description: 'Come vote for your favorite band at GimmeTheGig.com',
        message: shareMsg
    },
   function(response) {
       if (response && response.post_id) {
           //publish success
           //OpenPostTriviaGroupieForm('You have successfully shared and added votes to <strong>' + addSlashes(bandName) + '</strong>! Come back tomorrow and do it again!', 'share', bandName, iSubmissionId, user);
           show_dialog('Thank You', 'You have successfully shared and added votes to <strong>' + addSlashes(bandName) + '</strong>! Come back tomorrow and do it again!');
           //hide share button
           document.getElementById("share-button").innerHTML = "";
       } else {
           //publish cancel
           //OpenPostTriviaGroupieForm('Come back tomorrow and add votes to <strong>' + addSlashes(bandName) + '</strong>! by sharing the band with your friends on Facebook.', 'share', bandName, iSubmissionId, user);
       }
   });
}


function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function MM_openBrWindow(theURL, winName, features) { //v2.0
    window.open(theURL, winName, features);
}
