﻿/// <reference path="jquery-vsdoc.js" />
//*************************************************************************************
// File     : svcr_functions.js
// Requires : jquery.js (version 1.3.2+), jquery-custom-ui.js, braingnat.js (version 0.4.0+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : Mar 31, 2010
// Modified : Aug 18, 2010
//*************************************************************************************

var carouselTimer = 0;
var month = new Array();
month[1] = "January";
month[2] = "February";
month[3] = "March";
month[4] = "April";
month[5] = "May";
month[6] = "June";
month[7] = "July";
month[8] = "August";
month[9] = "September";
month[10] = "October";
month[11] = "November";
month[12] = "December";
month["January"] = 0;
month["February"] = 1;
month["March"] = 2;
month["April"] = 3;
month["May"] = 4;
month["June"] = 5;
month["July"] = 6;
month["August"] = 7;
month["September"] = 8;
month["October"] = 9;
month["November"] = 10;
month["December"] = 11;

$(document).ready(function() {
    if ($('#snipe').length < 1) {
        // not using ie6

    } else {
        // using ie6
        ie6Stuff();
    }
    backdrop();
    adBanner();
    revealPopup();
    $('#snipe .closeme').bind('click', function() { $('#snipe').slideUp(); return false; });
    carousel();
    $('.gallerywrap .gallery a').lightBox({ fixedNavigation: true });
    carouselGallery();
    fixHeaderPadding();
    printEvent();
    employmentReveals();
    BrainGnat.Google.map.loadFromHCard('.casino.vcard', "#map", 10, false);
    footerPopClick();
});

function ie6Stuff() {
        $('#footerBannerMenu').css({ display: 'none' });
        $('.gallerywrap.icon li:first-child').addClass('first');
        $('.gallerywrap.icon .first img').css({ width: '246px', height: '150px' });
        $('#footer .main:eq(1)').css({ display: 'none' });
        $('.footerLogo').attr('src', '/App_Themes/svcr/images/SVCR_LogoFooter.gif');
}

function adBanner() {
    datePick();
    $('.adBanner ul').css('opacity', '0');
    var bannerconfig = {
        over: function() { openAdBanner($(this)); },
        interval: 500,
        timeout: 250,
        out: function() { closeAdBanner($(this)); }
    }
    $('.adBanner').hoverIntent(bannerconfig);
}

function makeNubs() {
    $('.adBanner').append('<div class="nub">&nbsp;</div>');
}

function openAdBanner(e) {
    var elem = e.children('ul');
    elem.css({ display: 'block', zIndex: 99 });
    elem.animate({ opacity: '1' }, function() { positionPopup($(this)); });
}

function closeAdBanner(e) {
    var elem = e.children('ul');
    elem.animate({ opacity: '0' }, function() { $(this).css({ display: 'none' }); $(this).css({ zIndex: 1 }); });
}

function carousel() {
    if ($('#carousel li').length > 1) {
        $('#carousel').after('<div id="doppleganger">' + $('#carousel li:first-child').html() + '</div>');
        $('#carouselWrapper .max').html($('#carousel li').length);
        for (i = 0; i < $('#carousel li').length; i++) {
            var e = $('#carousel li:eq(' + i + ')');
            e.css('left', (960 * i) + 'px');
        }
        carouselTimer = setTimeout('slideTimer()', 10000);
        $('#carouselWrapper .previous').bind('click', function() { flipCarousel(-1); $(this).blur(); });
        $('#carouselWrapper .next').bind('click', function() { flipCarousel(1); $(this).blur(); });
        $('#carouselWrapper .pause').bind('click', function() { $('#carouselWrapper').addClass('paused'); clearTimeout(carouselTimer); $(this).blur(); });
        $('#carouselWrapper .play').bind('click', function() { $('#carouselWrapper').removeClass('paused'); flipCarousel(1); $(this).blur(); });
    } else if ($('#carousel li').length < 1) {
        $('#carouselWrapper').css('display', 'none');
    } else {
        $('#carouselWrapper button').css({ display: 'none' });
        $('#carouselWrapper .counter').css({ display: 'none' });
    }
}

function slideTimer() {
    var margin = $('#carousel').css('margin-left').substr(0, $('#carousel').css('margin-left').length - 2);
    var returnToStart = false;
    margin = margin - 960;
    if (margin < ((($('#carousel li').length - 1) * 960) * -1)) {
        returnToStart = true;
    }
    if (returnToStart) {
        $('#carousel').animate({ 'marginLeft': margin + 'px' }, 1000);
        $('#doppleganger').animate({ left: '0' }, 1000, function() {
        $(this).css({ left: '960px' });
            carouselTimer = setTimeout('slideTimer()', 10000);
            $('#carousel').css({ marginLeft: '0' });
        });
        $('#carouselWrapper .current').html('1');
    } else {
        $('#carousel').animate({ 'marginLeft': margin + 'px' }, 1000, function() { carouselTimer = setTimeout('slideTimer()', 10000); });
        $('#carouselWrapper .current').html(Math.abs(Math.floor(margin / 960)) + 1);
    }        
}

function flipCarousel(d) {
    $('#carousel').stop();
    clearTimeout(carouselTimer);
    var margin = $('#carousel').css('margin-left').substr(0, $('#carousel').css('margin-left').length - 2);
    var slide = Math.abs(Math.floor(margin / 960));
    var returnToStart = false;
    var skipToEnd = false;
    var slide = slide + d;
    if (slide < 0) {
        slide = ($('#carousel li').length - 1);
        skipToEnd = true;
    }
    margin = -(slide * 960);
    if (margin < ((($('#carousel li').length - 1) * 960) * -1)) {
        returnToStart = true;
    }
    if (returnToStart) {
        $('#carousel').animate({ 'marginLeft': margin + 'px' }, 1000);
        $('#doppleganger').animate({ left: '0' }, 1000, function() {
            $(this).css({ left: '960px' });
            carouselTimer = setTimeout('slideTimer()', 10000);
            $('#carousel').css({ marginLeft: '0' });
        });
        $('#carouselWrapper .current').html('1');
    } else if (skipToEnd) {
        $('#doppleganger').html($('#carousel li:last-child').html()).css({ left: '-960px' });
        $('#carousel').animate({ 'marginLeft': '960px' }, 1000);
        $('#doppleganger').animate({ left: '0' }, 1000, function() {
            $(this).css({ left: '960px' }).html($('#carousel li:first-child').html());
            carouselTimer = setTimeout('slideTimer()', 10000);
            $('#carousel').css({ marginLeft: (($('#carousel li').length - 1) * -960) + 'px' });
        });
        $('#carouselWrapper .current').html(Math.abs(Math.floor(margin / 960)) + 1);
    } else {
        $('#carousel').animate({ 'marginLeft': margin + 'px' }, 1000, function() { carouselTimer = setTimeout('slideTimer()', 10000); });
        $('#carouselWrapper .current').html(Math.abs(Math.floor(margin / 960)) + 1);
    }
}

function positionPopup(e) {
    var x = e.css("left").substring(0, e.css("left").length - 2) * 1;
    var y = e.css("top").substring(0, e.css("top").length - 2) * 1;
    var ymax = $(window).scrollTop() + $(window).height();
    var ybottom = (e.offset().top + e.outerHeight() + 20) - ymax;
    if (ybottom > 0) {
        y = y - ybottom;
    } else {
        ybottom = 0;
    }
    if (ydiff(e, ybottom) > 0) {
        y = y + 10 + ydiff(e, ybottom);
    }
    if ((e.parent('.adBanner').offset().left - $('#wrapper').offset().left) > 600) {
        x = 100 - e.outerWidth();
    } else {
        if ((xdiff(e) + 20) > -20) {
            x = x - xdiff(e);
        }
    }
    e.animate({ top: y + 'px', left: x + 'px' });
}


function ydiff(e, y) {
    return $(window).scrollTop() - e.offset().top + y;
}

function xdiff(e) {
    return (e.width() + (e.offset().left - $('#wrapper').offset().left)) - $('#wrapper').width();
}

function findNubHome(e, y) {
    var arrow = e.siblings('.nub');
    var ay = arrow.css("top").substring(0, arrow.css("top").length - 2) * 1;
    var bottom = (y + e.offset().top) - 97;
    if (ay >= bottom) {
        ay = bottom;
    } else if (ay <= y) {
        ay = y;
    } else {
        ay = 35;
    }
    return ay;
}

function revealPopup() {
    
    // footerBannerMenu popup //
    $('#footerBannerMenu').append('<div class="cover">&nbsp;</div>');
    $('#footerBannerMenu .cover').css({ display: 'none', position: 'absolute', top: 0, left: 0, width: '958px', height: $('#footerBannerMenu').outerHeight() + 'px', backgroundColor: 'black', opacity: 0 });
    
    $('#footerBannerMenu .popup').css({ opacity: '0', display: 'none' });
    for (i = 0; i < $('#footerBannerMenu .popup').length; i++) {
        var e = $('#footerBannerMenu .popup:eq(' + i + ')');
        var diff = $(window).width() - e.outerWidth();
        e.css({ left: (diff / 2) + 'px' });
    }
    $('#footerBannerMenu a').not('.popup a').bind('click', function() {
        $('#footerBannerMenu .reveal').css({ display: 'block' });
        $('#footerBannerMenu .cover').css({ display: 'block' });
        $('#footerBannerMenu .cover').animate({ opacity: 0.7 });
        $('#footerBannerMenu').css({ border: 'solid 1px #000000' });
        $('#footerBannerMenu .reveal').animate({ opacity: '0' }, function() {
            $(this).css({ display: 'none' }).removeClass('reveal');
        });
        $('#backdrop').appendTo('#wrapper').css({ display: 'block' }).animate({ opacity: '0.7' });
        $('#footerBannerMenu').css({ zIndex: '9999' });
        $(this).siblings('.popup').css({ display: 'block' }).animate({ opacity: '1' }, function() {
            $(this).css({ opacity: 'auto' }).addClass('reveal');
        });
        return false;
    });
    $('.popup .close').live('click', function() {
        $('#backdrop').appendTo('#wrapper').animate({ opacity: '0' });
        $('#footerBannerMenu').css({ zIndex: 'auto' });
        $('#footerBannerMenu .cover').animate({ opacity: '0' }, function() {
            $(this).css({ display: 'none' });
        });
        $('#footerBannerMenu .reveal').animate({ opacity: '0' }, function() {
            $(this).css({ display: 'none' }).removeClass('reveal');
            $('#backdrop').css({ display: 'none' });
            $('#footerBannerMenu').css({ border: 'solid 1px #999999' });
        });
    });

    // eventpop //
    if ($('.eventpop').length > 0) {
        $('.calendar .vevent').live('click', function() {
            $(this).children('.eventpop').css({ display: 'block', opacity: '0' });
            $('.nav.sub').css({ zIndex: '1' });
            $('#header').css({ paddingBottom: '60px', zIndex: '1' });
            $('#header .bookNow').css({ display: 'none' });
            $('#backdrop').appendTo('#primary').css({ display: 'block' }).animate({ opacity: '0.7' });
            $(this).children('.eventpop').animate({ opacity: '1' }, function() {
                $('this').css({ opacity: 'auto' });
            });
            $(this).addClass('up');
        });
        $('.calendar .vevent .eventpop').not('a').live('click', function() {
            return false;
        });
        $('.calendar .vevent object').live('click', function() { return false; });
        $('.calendar .vevent embed').live('click', function() { return false; });
        
        $('.calendar .buttons .close').live('click', function() {
            $(this).parents('.eventpop').css({ opacity: '1' });
            $('#backdrop').animate({ opacity: '0' }, function() {
                $(this).css({ display: 'none' }); $('.nav.sub').css({ zIndex: '2' });
                $('#header').css({ paddingBottom: '0', zIndex: '3' });
                $('#header .bookNow').css({ display: 'block' });
            });
            $(this).parents('.eventpop').animate({ opacity: '0' }, function() {
                $(this).css({ display: 'none', opacity: 'auto' });
            });
            $(this).parents('.vevent').removeClass('up');
            return false;
        });
    } else {
        $('.calendar').addClass('nopop');
    }
}

function datePick() {
    $(".bookNow .date").datepicker({ showOn: 'button', buttonImage: '/App_Themes/svcr/images/calendarBtn.png', buttonImageOnly: false });
    hiddenDates();
    setDates();
}

function setDates() {
    var today = new Date();
    var tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1);
    var todayDay = today.getDate();
    var tomorrowDay = tomorrow.getDate();
    var todayMonth = today.getMonth() + 1;
    var tomorrowMonth = tomorrow.getMonth() + 1;
    if (todayDay < 10) todayDay = '0' + todayDay;
    if (tomorrowDay < 10) tomorrowDay = '0' + tomorrowDay;
    if (todayMonth < 10) todayMonth = '0' + todayMonth;
    if (tomorrowMonth < 10) tomorrowMonth = '0' + tomorrowMonth;
    $('.bookNow .date:eq(0)').attr('value', todayMonth + '/' + todayDay + '/' + today.getFullYear());
    $('.bookNow .date:eq(1)').attr('value', tomorrowMonth + '/' + tomorrowDay + '/' + tomorrow.getFullYear());
    todayMonth = todayMonth * 1;
    tomorrowMonth = tomorrowMonth * 1;
    $('#inmonth').attr('value', month[todayMonth]);
    $('#inday').attr('value', todayDay);
    $('#inyear').attr('value', today.getFullYear());
    $('#outday').attr('value', tomorrowDay);
    $('#outmonth').attr('value', month[tomorrowMonth]);
    $('#outyear').attr('value',tomorrow.getFullYear());
}

function hiddenDates() {
    $('.bookNow .date').change(function() {
        var day = $(this).attr('value').split('/');
        var i = $('.bookNow .date').index($(this));
        if (i == 0) {
            $('#inmonth').attr('value', month[day[0] * 1]);
            $('#inday').attr('value', day[1]);
            $('#inyear').attr('value', day[2]);
        } else {
            $('#outmonth').attr('value', month[day[0] * 1]);
            $('#outday').attr('value', day[1]);
            $('#outyear').attr('value', day[2]);
        }
        ensureOutFollowsIn();
    });
}

function ensureOutFollowsIn() {
    var today = new Date();
    var inDate = new Date();
    inDate.setFullYear($('#inyear').attr('value'), month[$('#inmonth').attr('value')], $('#inday').attr('value'));
    var outDate = new Date();
    outDate.setFullYear($('#outyear').attr('value'), month[$('#outmonth').attr('value')], $('#outday').attr('value'));
    if (inDate < today) {
        inDate.setDate(today.getDate());
    }
    if (outDate <= inDate) {
        var m = inDate.getMonth();
        var d = inDate.getDate();
        var y = inDate.getFullYear();
        d = d + 1;
        outDate.setFullYear(y);
        outDate.setMonth(m);
        outDate.setDate(d);
    }
    var todayDay = inDate.getDate();
    var tomorrowDay = outDate.getDate();
    var todayMonth = inDate.getMonth() + 1;
    var tomorrowMonth = outDate.getMonth() + 1;
    if (todayDay < 10) todayDay = '0' + todayDay;
    if (tomorrowDay < 10) tomorrowDay = '0' + tomorrowDay;
    if (todayMonth < 10) todayMonth = '0' + todayMonth;
    if (tomorrowMonth < 10) tomorrowMonth = '0' + tomorrowMonth;
    $('.bookNow .date:eq(0)').attr('value', todayMonth + '/' + todayDay + '/' + inDate.getFullYear());
    $('.bookNow .date:eq(1)').attr('value', tomorrowMonth + '/' + tomorrowDay + '/' + outDate.getFullYear());
    todayMonth = todayMonth * 1;
    tomorrowMonth = tomorrowMonth * 1;
    $('#inmonth').attr('value', month[todayMonth]);
    $('#inday').attr('value', todayDay);
    $('#inyear').attr('value', inDate.getFullYear());
    $('#outday').attr('value', tomorrowDay);
    $('#outmonth').attr('value', month[tomorrowMonth]);
    $('#outyear').attr('value', outDate.getFullYear());
}

function backdrop() {
    $('#primary').append('<div id="backdrop"></div>');
    $('#backdrop').css({ display: 'none', position: 'fixed', zIndex: '9998', top: '0', left: '0', opacity: '0', width: $(window).width() + 'px', height: $(window).height() + 'px', backgroundColor: '#000' });
}


function carouselGallery() {
    $('.gallerywrap.carousel').before('<button type="button" class="left galnav">left</button>').after('<button type="button" class="right galnav">right</button');
    var i;
    for (i = 0; i < $('.gallerywrap.carousel').length; i++) {
        var g = $('.gallerywrap.carousel:eq(' + i + ') .gallery');
        g.css({ width: ((g.children('li').length * 130) - 8) + 'px' });
        lightCarouselButtons(i);
    }
    $('.galnav').live('click', function() {
        var mult = -1;
        var index = $('.galnav.right').index($(this));
        if ($(this).hasClass('left')) {
            mult = 1;
            index = $('.galnav.left').index($(this));
        }
        var gallery = $('.gallerywrap.carousel:eq(' + index + ') .gallery');
        var difference = mult * 390;
        var galx = gallery.css('margin-left').substr(0, gallery.css('margin-left').length - 2);
        galx = (galx * 1) + difference;
        if (galx > 0) galx = 0;
        if (galx < ((gallery.width() - 512) * -1)) galx = ((gallery.width() - 512) * -1)
        gallery.animate({ marginLeft: galx + 'px' }, function() {
            var index = $('.gallerywrap.carousel .gallery').index($(this));
            lightCarouselButtons(index);
        });
        $(this).blur();
    });
}

function lightCarouselButtons(i) {
    var gallery = $('.gallerywrap.carousel:eq(' + i + ') .gallery');
    var galx = gallery.css('margin-left').substr(0, gallery.css('margin-left').length - 2);
    if (galx == 0) {
        $('.galnav.left:eq(' + i + ')').addClass('no');
    } else {
        $('.galnav.left:eq(' + i + ')').removeClass('no');
    }
    if (galx <= ((gallery.width() - 512) * -1)) {
        $('.galnav.right:eq(' + i + ')').addClass('no');
    } else {
        $('.galnav.right:eq(' + i + ')').removeClass('no');
    }
}

function fixHeaderPadding() {
    var i;
    var w = 0;
    for (i = 0; i < $('#header .nav.main > li').length; i++) {
        w = w + $('#header .nav.main > li:eq(' + i + ')').outerWidth();
    }
    var d = $('#header .nav.main').width() - w;
    if (d > 0) {
        d = Math.floor(d / 2) - 1;
        $('#header .nav.main > li:first-child a').css({ width: ($('#header .nav.main > li:first-child a').width() + d) + 'px' });
        $('#header .nav.main > li:last-child a').css({ width: ($('#header .nav.main > li:last-child a').width() + d) + 'px' });
    }
}

function printEvent() {
    $('.vevent button.print').live('click', function() {
        window.open("http://svcr.mindfly.net/Print/EventInfo.aspx?calendar=" + $(this).siblings('.lookup').attr('value'));
    });
    if ($('body').hasClass('print')) {
        window.print();
    }
}


function employmentReveals() {
    for (j = 0; j < $('.employment.list .categoryTitles a').length; j++) {
        $('.employment.list .categoryTitles a:eq(' + j + ')').html('+ ' + $('.employment.list .categoryTitles a:eq(' + j + ')').html());
    }
    $('.employment.list .categoryTitles a').live('click', function() {
        var i = $('.employment.list .categoryTitles a').index(this);
        var state = $(this).html().substring(0, 1);
        if (state == '+') {
            $(this).html('-' + $(this).html().substring(1));
        } else {
            $(this).html('+' + $(this).html().substring(1));
        }
        $('.employment.list .reveal.box:eq(' + i + ')').slideToggle(function() {
            $('#secondary .adBanner a:last-child').focus();
            $('#secondary .adBanner a:last-child').blur();
        });
    });
}

function ShowPop(sURL, windowName, width, height) {
    // Purpose: Launches File in new window 
    myRemote = launch(sURL, windowName,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,alwaysRaised=yes,width=" + width + ",height=" + height + ",left=100,top=50","parentWin");
}

function launch(newURL, newName, newFeatures, orgName) {
    var remote = open(newURL, newName, newFeatures);
    if (remote.opener == null)
    remote.opener = window;
    remote.opener.name = orgName;
    return remote;
}

function footerPopClick() {
    $('#footer .nav.main:eq(1) li:a').click(function() {
        var i = $('#footer .nav.main:eq(1) li:a').index($(this));
        $('#footerBannerMenu li:eq(' + i + ') a:eq(0)').click();
        return false;
    });
}
 