$(document).ready(function () {


    $('div.nav a.active').hover(imagelinkOver, imagelinkOut).css('cursor', 'pointer').click(imagelinkOut);

    $('a.in-active').hover(function () {

        var thePosition = $(this).position();
        var theOffset = thePosition.left;
        var theWidth = $(this).find('img').width();
        var theShiv = Math.floor((theWidth - 162) / 2);
        theOffset = theOffset + theShiv;
        $('div.coming-soon').removeClass('hidden').css('left', theOffset + "px");

    }, function () {

        $('div.coming-soon').addClass('hidden');

    });

    $('a.listen-now').click(function (ev) {
        if ($(this).hasClass('active')) {
            window.open("https://www.customchannels.net/streaming/virgin_mobile/live/", "Virgin_Live", "height=390,width=280,modal=yes,alwaysRaised=yes");
            ev.preventDefault();
            return false;
        }
    });

    $("#fb-like").hover(function () {
        $(this).find('img').attr('src', '/images/ICN_fb_like_over.png');
    }, function () {
        $(this).find('img').attr('src', '/images/ICN_fb_like.png');
    });


    /*if ($.browser.webkit) { $("#header ul.nav").addClass("web-kit"); }*/

    /* accordion */
    var lastBlock = $("li#anchor-3 a");
    $(lastBlock).addClass('active').find("img").attr('src', '/images/BNR_accordion_3.png');
    var maxWidth = 776;
    var minWidth = 51;

    $(".accordion ul li a").click(
    function () {
        //alert($(this).attr("class"));
        if ($(this).hasClass('active')) {
            return true;
        } else {
            var theIndex = $("a").index(this) - 1;
            var newSrc = "/images/BNR_accordion_" + theIndex + ".png";
            $('.accordion ul li a.active').removeClass('active').animate({ width: minWidth + "px" }, { queue: false, duration: 400 }).find('img').attr('src', '/images/BNR_accordion_0.png');
            //$(lastBlock).removeClass('active').animate({ width: minWidth + "px" }, { queue: false, duration: 400 }).find('img').attr('src', '/images/BNR_accordion_0.png');
            $(this).addClass('active').find('img').attr('src', newSrc);
            $(this).animate({ width: maxWidth + "px" }, { queue: false, duration: 400 });
            lastBlock = this;
            return false;
        }
    }
    );

    autoRotate = setInterval("rotateTouts()", 5000);

    $("#header .accordion").mouseover(function () {
        clearInterval(autoRotate)
    }).mouseout(function () {
        autoRotate = setInterval("rotateTouts()", 5000);
    });

    //var browser = jQuery.uaMatch(navigator.userAgent).browser;
    //alert(browser);

    $("#sign-up input#email").click(function () {
        if ($(this).val() == "YourEmail@Address.com")
            $(this).val("");
    });
    $("#sign-up input#email").blur(function () {
        if ($(this).val() == "")
            $(this).val("YourEmail@Address.com");
    });


    $("#sign-up input.submit").click(function () {
        var successFlag = "";
        //var errorFlag = "";
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if ($("body").hasClass('main')) {
            successFlag = "<img src='/images/mailing_list_confirm_main.png' alt='' width='218' height='85' />";
            //errorFlag = "<img src='/images/mailing_list_error_main.png' alt='' width='218' height='27' />";
        } else {
            successFlag = "<img src='/images/mailing_list_confirm.png' alt='' width='289' height='112' />";
            //errorFlag = "<img src='/images/mailing_list_error.png' alt='' width='289' height='38' />";
        }
        var emailAddress = $("#sign-up input#email").val();
        /*var emailVal = emailPattern.test(emailAddress);
        if (emailAddress == "" || emailAddress == "YourEmail@Address.com" || emailVal == false) {
        $('#sign-up').append("<div class='error'></div>");
        $('div.error').html(errorFlag).hide().fadeIn('slow').delay(5000).fadeOut('slow');
        return false;
        }*/
        var isChecked = $('#sign-up #age_confirm').attr('checked');
        if (isChecked == false) { alert('Please confirm that you are over 13 years old'); return false; }

        var dataString = 'email=' + emailAddress;
        $.ajax({
            type: "POST",
            data: dataString,
            url: "emma.php",
            success: function (result) {

                if (result == 1 || result == 2) {
                    $('#sign-up').append("<div class='success'></div>");
                    $('div.success').html(successFlag).hide().fadeIn('slow').delay(5000).fadeOut('slow');
                } else if (result == -1 || result == -2 || result == -3) {
                    $("#age-check").hide();
                    $('#sign-up fieldset').append("<p class='error'>Failed to add memberr.</p>");
                    $('p.error').hide().fadeIn('slow').delay(5000).fadeOut('slow');
                    $("#age-check").delay(6000).fadeIn('slow');
                    $('p.error').delay(300).queue(function () {
                        $(this).remove();
                    });
                } else if (result == -6) {
                    $("#age-check").hide();
                    $('#sign-up fieldset').append("<p class='error'>Invalid email address.</p>");
                    $('p.error').hide().fadeIn('slow').delay(5000).fadeOut('slow');
                    $("#age-check").delay(6000).fadeIn('slow');
                    $('p.error').delay(300).queue(function () {
                        $(this).remove();
                    });
                }

            }
        });

        return false;
    });

    addExternalTracking();

});

function imagelinkOver() {
    $(this).find('img').stop(true, true).animate({ 'margin-top': -($(this).height()) + 'px' }, 800, 'easeOutExpo');
}

function imagelinkOut() {
    $(this).find('img').stop(true, true).animate({ 'margin-top': '0px' }, 800, 'easeOutExpo');
}

function rotateTouts() {
    var currentTout = $(".accordion ul li a.active");
    var nextTout = $(".accordion ul li a").index(currentTout) + 2;

    if (nextTout > 4) { nextTout = 1; }
      

    var newSrc = "/images/BNR_accordion_" + nextTout + ".png";
    $(currentTout).removeClass('active').animate({ width: 51 + "px" }, { queue: false, duration: 400 }).find('img').attr('src', '/images/BNR_accordion_0.png');
    $("li#anchor-" + nextTout + " a").find('img').attr('src', newSrc);
    $("li#anchor-" + nextTout + " a").animate({ width: 776 + "px" }, { queue: false, duration: 400 });
    $("li#anchor-" + nextTout + " a").addClass('active');
    //lastBlock = $("li#anchor-" + nextTout + " a");
    //currentTout = this;
}

function addExternalTracking() {
	$('a[href*="http://"]:not([href^="http://virginmobilefestival.com"]):not([href^="mailto:"]),[href*="https://"]:not([href^="https://virginmobilefestival.com"])').not("[href^=#]").click(function() { trackExternalLink( 'outgoing', 'click', $(this).attr('href') ); } );
	$('a[href^="mailto:"]').click(function() { trackExternalLink( 'mailto', 'click', $(this).attr('href') ); } );
}

function trackExternalLink(category, action, link) {
	try {
	var myTracker = _gat._getTrackerByName();
		_gaq.push(['_trackEvent', category, action, link]);
	} catch(err){}
} 

stLight.options({publisher:'8509cbcb-5399-4af8-823d-d423c5d17b55'});

