﻿//*************************************************************************************
// File     : semi_functions.js
// Author   : Kyle Weems (ksw) of Mindfly Web Design Studio
// Requires : jquery.js (version 1.3.2+), braingnat.js (version 0.3.0+), braingnat Google functions
// Origin   : mindfly.com
// Created  : Jul 29, 2009
// Modified : Jul 19, 2010
// Purpose  : Provides additional front-end interactivity for semiahmoo's website.
//*************************************************************************************

$(document).ready(function() {
    loadResortMap();
    thingsToDoCarousel();
    // specialsSlider();
    specialsFader();
    employmentReveals();
    galleries();
    alignDates();
    seeRooms();
    sizeGalleryIcons();
    offerCodes();
    populateSpecials();
    setSliderClick();
    parseCheckIn();
});

var currentThing = 0;
var currentSpecial = 0;
var slideShow = true;
var endOfMonth = new Array();
endOfMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var now = new Date();
var month = now.getMonth();
var day = now.getDate();
var year = now.getFullYear();
var galleryHeight = new Array();


function loadResortMap() {
    BrainGnat.Google.map.loadFromHCard('.footer .vcard', '#googleMap', 11, false);
    BrainGnat.Google.map.addMarkerByAddress('8720 Semiahmoo Parkway, Blaine, WA 98230'/*, '<div style="color:black;text-align:center;"><br/>Semiahmoo Golf &amp; Country Club</div>'*/);
    BrainGnat.Google.map.addMarkerByAddress('4342 Loomis Trail Road, Blaine, WA 98230'/*, '<div style="color:black;text-align:center;"><br/>Loomis Trail Golf Club</div>'*/);
}

function galleries() {
    for (i = 0; i < $('ul.gallery').length; i++) {
        $('ul.gallery:eq(' + i + ') a').lightBox({ fixedNavigation: true });
    }
    $('.webcam a').lightBox({ fixedNavigation: true });
    $('.photoGalleries a').click(function() {
        var gallery = $(this).parent().attr('class').split("_")[1] * 1;
        loadLightboxGallery(gallery);
        return false;
    });
}

function thingsToDoCarousel() {
    $('.things.right').bind('click', function() {
        c = currentThing;
        if (c < ($('.thingsToDo .carousel li').length - 6)) {
            c = c + 1;
            $('.thingsToDo .carousel').animate({ marginLeft: -(c * 121) }, 300);
        }
        currentThing = c;
    });
    $('.things.left').bind('click', function() {
        c = currentThing;
        if (c > 0) {
            c = c - 1;
            $('.thingsToDo .carousel').animate({ marginLeft: -(c * 121) }, 300);
        }
        currentThing = c;
    });
}

function specialsSlider() {
    $('.specialsControl li a:eq(0)').addClass('selected');
    $('.specialsControl li a').bind('click', function() {
        s = $('.specialsControl li a').index(this);
        $('.specialsControl li a').removeClass('selected');
        $(this).addClass('selected');
        $('.specials.slider').animate({ marginLeft: -(s * 729) }, 1000);
        currentSpecial = s;
        return false;
    });
}

function specialsFader() {
    $('.specials li').css({ 'position': 'absolute', 'left': 0, 'top': 0, 'opacity':0 });
    $('.specials li:eq(0)').addClass('current').css({ 'opacity': 1 });

    if ($('.specials li').length < 2) {
        slideShow = false;
    } else {
        $('.specialsControl').append('<ul></ul>');
        for (i = 0; i < $('.specials li').length; i++) {
            $('.specialsControl ul').append('<li><a href="#">' + (i+1) + '</a></li>');
        }
    }
    $('.specialsControl li a:eq(0)').addClass('selected');
    $('.specialsControl li a').bind('mouseover', function() {
        $('#slideWrapper .mainPhoto').stop();
        $('#slideWrapper .mainPhoto').slideToggle();
        s = $('.specialsControl li a').index(this);
        o = currentSpecial
        if (s != o) {
            currentSpecial = s;
            $('.specials li:eq(' + o + ')').animate({ opacity: 0 }, 950);
            $('.specials li.current').removeClass('current');
            $('.specialsControl li a.selected').removeClass('selected');
            $('.specials li:eq(' + s + ')').animate({ opacity: 1 }, 1000, function() { $('.specials li:eq(' + currentSpecial + ')').addClass('current'); $('.specialsControl li a:eq(' + currentSpecial + ')').addClass('selected'); });
        }
        slideShow = false;
    });
    $('.specialsControl li a').bind('mouseleave', function() {
        $('#slideWrapper .mainPhoto').stop();   
        $('#slideWrapper .mainPhoto').slideToggle();
    });
    $('.specialsControl li a').bind('mouseleave', function() {
        slideShow = true;
    });
    if ($('#slideWrapper .mainPhoto').length > 0) {
        slideShow = false;
        $('.specialsControl li a.selected').removeClass('selected');
    }
    if (slideShow == true) { setTimeout(slideSpecials, 6000); } 
}

function slideSpecials(){
    if (slideShow == true) {
        o = currentSpecial
        s = o + 1;
        if (s > ($('.specials li').length - 1)) {
            s = 0;
        }
        currentSpecial = s;
        $('.specials li:eq(' + o + ')').animate({ opacity: 0 }, 950);
        $('.specials li.current').removeClass('current');
        $('.specialsControl li a.selected').removeClass('selected');
        $('.specials li:eq(' + s + ')').animate({ opacity: 1 }, 1000, function() { $('.specials li:eq(' + currentSpecial + ')').addClass('current'); $('.specialsControl li a:eq(' + currentSpecial + ')').addClass('selected'); });

    }
    setTimeout(slideSpecials, 7000);
}

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 seeRooms() {
    $('.bookRoom .seeRooms').click(function() {
        loadLightboxGallery(13);
        return false;
    });
}

function loadLightboxGallery(g) {
    $.get('/js/GalleryLightBox.ashx?gallery=' + g, function(data) {
        json = eval('(' + data + ')');
        var gallery = '<div class="hidden lightBox">';
        for (i = 0; i < json.gallery.images.length; i++) {
            var gallery = gallery + '<a href="' + json.gallery.images[i].filename + '" title="' + json.gallery.images[i].title + '">&nbsp;</a>';
        }
        gallery = gallery + '</div>';
        $('.footer').append(gallery)
        $('.hidden.lightBox a').lightBox({ fixedNavigation: true });
        $('.hidden.lightBox a:eq(' + 0 + ')').click();
    }); 
}


function alignDates() {
    // 1. set initial date to today and tomorrow.
    // set the year
    $('#inyear option:eq(' + (year - 2009) + ')').attr('selected', 'selected');
    $('#outyear option:eq(' + (year - 2009) + ')').attr('selected', 'selected');    
    // set the month
    $('#inmonth option:eq(' + month + ')').attr('selected', 'selected');
    $('#outmonth option:eq(' + month + ')').attr('selected', 'selected');
    // set the day
    $('#inday option:eq(' + (day - 1) + ')').attr('selected', 'selected');
    if (day >= endOfMonth[month]) {
        if ((month + 1) > 11) {
            $('#outyear option:eq(' + (year - 2008) + ')').attr('selected', 'selected');
            $('#outmonth option:eq(0)').attr('selected', 'selected');
            $('#outday option:eq(0)').attr('selected', 'selected');
        } else {
            $('#outmonth option:eq(' + (month + 1) + ')').attr('selected', 'selected');
            $('#outday option:eq(0)').attr('selected', 'selected');
        }
    } else {
        $('#outday option:eq(' + day + ')').attr('selected', 'selected');
    }
    
    // 2. Event handlers for when the date is altered. Prevent the date from going before today, or the out date from preceding the in date.
    $('#inmonth').change(function() { maintainDates(); });
    $('#inday').change(function() { maintainDates(); });
    $('#inyear').change(function() { maintainDates(); });
    $('#outmonth').change(function() { maintainDates(); });
    $('#outday').change(function() { maintainDates(); });
    $('#outyear').change(function() { maintainDates(); });
    
}

function maintainDates() {
    // 1. Collect day/month/year for in and out.
    id = $('#inday option').index($('#inday option:selected'));
    im = $('#inmonth option').index($('#inmonth option:selected'));
    iy = $('#inyear option').index($('#inyear option:selected'));0
    od = $('#outday option').index($('#outday option:selected'));
    om = $('#outmonth option').index($('#outmonth option:selected'));
    oy = $('#outyear option').index($('#outyear option:selected'));
    // 2. Make sure in date is not before today, if it is, set it to today.
    if(iy < (year - 2009)) {
        iy = (year - 2009);
    } else if (iy == (year - 2009)) {
        if (im < month) {
            iy = (year - 2008);
        } else if (im == month) {
            if (id < (day - 1)) {
                im = month + 1;
                if (im > 11) {
                    im = 0;
                    iy = (year - 2008);
                }
            }
        }
    }
    // 3. Make sure out date is not before in date. If it is, set it after in date.
    if (oy < iy) {
        oy = iy;
    }
    if (oy == iy) {
        if (om < im) {
            om = im;
        }
        if (om == im) {
            if (od < (id+1)) {
                od = (id + 1);
            }
            if (od > endOfMonth[month]) {
                od = 0;
                om = om + 1;
                if (om > 11) {
                    om = 0;
                    oy = oy + 1;
                }
            }
        }
    }
    // 4. Update the dates
    $('#inday option:eq(' + id + ')').attr('selected', 'selected');
    $('#inmonth option:eq(' + im + ')').attr('selected', 'selected');
    $('#inyear option:eq(' + iy + ')').attr('selected', 'selected');
    $('#outday option:eq(' + od + ')').attr('selected', 'selected');
    $('#outmonth option:eq(' + om + ')').attr('selected', 'selected');
    $('#outyear option:eq(' + oy + ')').attr('selected', 'selected');
}

function sizeGalleryIcons() {
    for (i = 0; i < $('.photoGalleries a').length; i++) {
        w = $('.photoGalleries img:eq(' + i + ')').width();
        h = $('.photoGalleries img:eq(' + i + ')').height();
        wFactor = w / 96;
        hFactor = h / 77;
        if (wFactor < hFactor) {
            $('.photoGalleries img:eq(' + i + ')').css('width', '96px');
        } else {
            $('.photoGalleries img:eq(' + i + ')').css('height', '77px');
        }
    }
}


function slideGalleries() {
    for (i = 0; i < $('.gallery').length; i++) {
        galleryHeight[i] = $('.gallery:eq(' + i + ')').height();
    }
    $('.gallery').animate({ height: '117px' }, 1000, function() {
        $(this).after('<a class="seeMoreGallery" href="#">see more</a>');
        $('.seeMoreGallery').click(function() {
            $(this).prev().toggleClass('open');
            if ($(this).prev().hasClass('open')) {
                $(this).prev().stop();
                $(this).prev().animate({ height: galleryHeight[$('.seeMoreGallery').index(this)] + 'px' }, 1000);
            } else {
                $(this).prev().stop();
                $(this).prev().animate({ height: '117px' }, 1000);
            }
            return false;
        });
    });
    
}

function offerCodes() {
    $('.bookRoom form input[type="submit"]').click(function() {
        var act = $('.bookRoom form').attr('action');
        var promo = $('.bookRoom input[name="resCorp"]').attr('value');
        var group = $('.bookRoom input[name="resGroup"]').attr('value');
        if (promo != "") {
            act = act + "&corpcode=" + promo;
        }
        if (group != "") {
            act = act + "&offercode=" + group;
        }
        $('.bookRoom form').attr('action', act);
    });
}

function populateSpecials() {
    $('.offercode').after('<a class="offer seemore" href="#">book now</a>');
    $('.offer.seemore:not(.press)').click(function() {
        $('input[name="resCorp"]').attr('value', $(this).prev().html());
        $('html,body').stop();
        $('.bookRoom').stop();
        $('html,body').animate({ scrollTop: 0 }, 500, function() {
            $('.bookRoom').animate({ top: '58px' }, 250, function() {
                $('.bookRoom').animate({ top: '68px' }, 250);
            });
        });
        return false;
    });
}

// hooks an event to the slider's list items so that the details anchor is clicked when the slider is clicked.
function setSliderClick() {
    $('.specials li').live('click', function() {
        window.location = $(this).children('.details').attr('href');
    });
}

function parseCheckIn() {
    var signposts = 0;
    var q = window.location.search.substring(1);
    var segments = q.split("&");
    var qName = new Array();
    var qValue = new Array();
    if (segments.length > 0) {
        for (i = 0; i < segments.length; i++) {
            qName[i] = segments[i].split("=")[0];
            qValue[i] = segments[i].split("=")[1];
        }
        for (i = 0; i < qName.length; i++) {
            switch (qName[i]) {
                case "checkin":
                    var im = (qValue[i].split("/")[0] * 1) - 1;
                    var id = (qValue[i].split("/")[1] * 1) - 1;
                    var iy = (qValue[i].split("/")[2] * 1) - 2009;
                    $('#inmonth option:eq(' + im + ')').attr('selected', 'selected');
                    $('#inday option:eq(' + id + ')').attr('selected', 'selected');
                    $('#inyear option:eq(' + iy + ')').attr('selected', 'selected');
                    signposts++;
                    break;
                case "checkout":
                    var om = (qValue[i].split("/")[0] * 1) - 1;
                    var od = (qValue[i].split("/")[1] * 1) - 1;
                    var oy = (qValue[i].split("/")[2] * 1) - 2009;
                    $('#outmonth option:eq(' + om + ')').attr('selected', 'selected');
                    $('#outday option:eq(' + od + ')').attr('selected', 'selected');
                    $('#outyear option:eq(' + oy + ')').attr('selected', 'selected');
                    signposts++;
                    break;
                case "group":
                    $('#resGroup').attr('value', qValue[i]);
                    signposts++;
                    break;
                default:
                    break;
            }
        }
        if (signposts > 2) {
            document.forms["resrvForm"].submit();
        }
    }
}

