﻿/*
DeltaSky Methods
author: Eric Freeberg, bswing
www.bswing.com
*/

var DeltaSky = 
{
    Init:function(){
        // TODO: Only call what needs to be called for the current page.
        
        $("div.ds-logo").click(function(){
            document.location = "http://deltaskymag.delta.com/";
        });
        
        DeltaSky.RoundedSet();
        DeltaSky.FontReplace();
        DeltaSky.PanelSet();
        DeltaSky.DestinationCarouselSet();
        
        $('#home-slideshow-container').flash(
            { src: 'http://deltaskymag.delta.com/flash/HomePageSlides.swf',width: 540, height: 350,wmode:'opaque' }, 
            { update: false }
        );

        if ($('.detail-slide-show-container').length > 0) {
            // TODO: Refactor slideshow methods so all slideshows can use the same methods.
            DeltaSky.SlideShowSet();
        }

        DeltaSky.VoteSet();

        if (($('#listings-map').length > 0) && ($('.map-data').length > 0)) {    
            window.setTimeout("Map.Init('listings-map', 10);Map.Load();", 2000);
        }
        
        if (($('#listing-map').length > 0) && ($('.map-data').length > 0)) {
            window.setTimeout("Map.Init('listing-map', 15);Map.Load();", 2000);
        }
        
        if ($('#subscribe-form').length > 0) {
            this.InitSubscriptionForm();
        }
    }
    ,
    InitSubscriptionForm: function() {
        // Ref: http://docs.jquery.com/Plugins/Validation/validate#options
        // Ref: http://bassistance.de/jquery-plugins/jquery-plugin-validation/
        // Set up validation requirements
        $("form").validate({
            rules: {
                Subscribe: {
                    required: false
                },
                Unsubscribe: {
                    required: false
                },
                EmailAddress: {
                    required: true,
                    email: true
                },
                FirstName: {
                    required: true
                },
                LastName: {
                    required: true
                },
                Address1: {
                    required: true
                },
                Address2: {
                    required: false
                },
                City: {
                    required: true
                },
                Country: {
                    required: true
                },
                StateProvince: {
                    required: {
                        depends: function(element) {
                            var isRequired = false;
                            if (($("#Country").val() === "UNITED STATES") || ($("#Country").val() === "CANADA")) {
                                isRequired = true;
                            }
                            return isRequired;
                        }
                    }
                },
                PostalCode: {
                    required: {
                        depends: function(element) {
                            var isRequired = false;
                            if (($("#Country").val() === "UNITED STATES") || ($("#Country").val() === "CANADA")) {
                                isRequired = true;
                            }
                            return isRequired;
                        }
                    }
                },
                PID: {
                    required: true
                },
                HouseholdIncome: {
                    required: false
                },
                Age: {
                    required: false,
                    maxlength: 2,
                    digits: true
                },
                Gender: {
                    required: false
                },
                Source: {
                    required: false
                }
            },
            submitHandler: function(form) {
                // Set the email address field ("Email Address") expected by ExactTarget.
                $("#ET_EmailAddress").attr("value", $("#EmailAddress").val());
                
                // Set the timestamp value.
                var date = new Date();
                $("#Timestamp").attr("value", date.toLocaleString());

                // Proceed to submit the form.
                form.submit();
            }
        });
        
        // Populate the email field, if an email was passed to this page.
        var url = new String(window.location);
        var querystringIndex = url.indexOf("?");
        if (querystringIndex != -1) {
            var querystring = url.substr(querystringIndex + 1);
            var emailQuerystringIndex = querystring.indexOf("email=");
            if (emailQuerystringIndex != -1) {
                var emailQuerystring = querystring.substr(emailQuerystringIndex + 1);
                var emailValueIndex = emailQuerystring.indexOf("=");
                var emailValue = emailQuerystring.substr(emailValueIndex + 1);
                $("#uxEmailAddress").val(emailValue);
            }
        }
    }
    ,
    IsIE6:function(){
        if ( $.browser.msie ){
            if(parseInt($.browser.version) < 7){
                return true;
            }
        }
        return false;
    }
    ,    
    PanelShow:function(){
        if (DeltaSky.IsIE6()) {
            $(this).find(".panel").show();
        } else {
            $(this).find(".panel").slideDown(400);
        }
    }
    ,
    PanelHide:function(){
        if (DeltaSky.IsIE6()) {
            $(this).find(".panel").hide();
        } else {
            $(this).find(".panel").slideUp(300);  
       }
    }
    ,    
    PanelShowButton:function(){
       // rollover main nav bar. 
       $(this).find(".nav-destinations").attr("src","http://blogs.deltaskymag.com/images/menu/nav-destinations-on.gif");
       $(this).find(".nav-blogs").attr("src","http://blogs.deltaskymag.com/images/menu/nav-blogs-on.gif");
       $(this).find(".nav-contests").attr("src","http://blogs.deltaskymag.com/images/menu/nav-contests-on.gif");
       $(this).find(".nav-magazine").attr("src","http://blogs.deltaskymag.com/images/menu/nav-magazine-on.gif");
    }
    ,
    PanelHideButton:function(){
       // rolloff main nav bar.
       $(this).find(".nav-destinations").attr("src","http://blogs.deltaskymag.com/images/menu/nav-destinations.gif");
       $(this).find(".nav-blogs").attr("src","http://blogs.deltaskymag.com/images/menu/nav-blogs.gif");
       $(this).find(".nav-contests").attr("src","http://blogs.deltaskymag.com/images/menu/nav-contests.gif");
       $(this).find(".nav-magazine").attr("src","http://blogs.deltaskymag.com/images/menu/nav-magazine.gif");
    }
    ,
    PanelSet:function(){
        $("#main-nav li").hoverIntent( DeltaSky.PanelShow, DeltaSky.PanelHide );
        $("#main-nav li").hover( DeltaSky.PanelShowButton, DeltaSky.PanelHideButton );
    }
    ,
    RoundedSet:function(){
        // home page
        $(".rounded-current-issue").corners("11px anti-alias transparent top-right bottom-left");
        $(".video-highlight").corners("11px anti-alias transparent top-right bottom-left");
        
        // destination pages        
        $(".rounded-listing-group").corners("11px anti-alias transparent top-right bottom-left");
        
        $(".detail-slide-show-container").corners("11px anti-alias transparent top-right bottom-left");
        $(".detail-video-container").corners("11px anti-alias transparent top-right bottom-left");
        
        // panel rounding gets messed up in IE ...
        //$("#main-nav li .panel").corners("11px anti-alias transparent top-right bottom-left");
        
        //$("div.vote").corners("11px anti-alias transparent top-right bottom-left");
        
        // Left-Hand Navigation
        $("ul#left-navigation .rounded-list-item").corners("11px anti-alias transparent top-right bottom-left");
        //$("div#left-navigation-div .rounded-list-item").corners("11px anti-alias transparent top-right bottom-left");
        
        $(".left-nav-item").corners("11px anti-alias transparent top-right bottom-left");
        $(".left-nav-item-selected").corners("11px anti-alias transparent top-right bottom-left");
        
        
        $(".blog-about").corners("11px anti-alias transparent top-right bottom-left");
        
        
    }   
    ,
    DestinationCarouselSet:function(){
        if($(".posts-carousel .posts-group .posts").length > 2)
        {
            var pgs = $(".posts-carousel .posts-group");
            var pagination = $(".pagination"); 

            for(var g = 0; g < pgs.length; g++)
            {
                if(g == 0)
                {
                    $(pgs[g]).css("display","block");  
                }

                $(pagination[g]).attr("pageid",g);

                $(pagination[g]).click( function(){ 
                    DeltaSky.DestinationCarouselGoToPage($(this).attr("pageid")); 
                });
            }
        }
    }
    ,
    DestinationCarouselFadeIn:function(id) {
        var pgs = $(".posts-carousel .posts-group");
        for(var g = 0; g < pgs.length; g++)
        {
            if(g == id)
            {
                 $(pgs[g]).fadeIn(200);
            }                
        }
    }
    ,
    DestinationCarouselGoToPage:function(id){
        var pgs = $(".posts-carousel .posts-group");
        var pagination = $(".pagination");
        for(var g = 0; g < pgs.length; g++)
        {
            if($(pgs[g]).css("display") == "block")
            {
                 $(pgs[g]).fadeOut(200,function(){DeltaSky.DestinationCarouselFadeIn(id)});
            }
            
            if(g == id)
            {
                $(pagination[g]).css("color","#999999");
            }
            else
            {
                $(pagination[g]).css("color","#333333");
            }
        }
    }
    ,
    FontReplace:function(){
        $("h1 a span.city-name").each(function(){           
            var text = $(this).html(); 
            if(text.length > 0){
                          
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=6eadb5&font=NEOSANSM.TTF&height=30px&size=30px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        $("h1 a span.city-tag").each(function(){           
            var text = $(this).html();            
            if(text.length > 0){   
            
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSL.TTF&height=30px&size=30px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        $("h3 span.post-label").each(function(){           
            var text = $(this).html();     
            if(text.length > 0) {       
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSM.TTF&height=16px&size=16px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        $("p span.post-label").each(function(){           
            var text = $(this).html();     
            if(text.length > 0) {       
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSM.TTF&height=16px&size=16px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        $("h3 span.post-features").each(function(){           
            var text = $(this).html();     
            if(text.length > 0) {       
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSM.TTF&height=18px&size=18px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });        
            
        $("h2.detail-title").each(function(){           
            var text = $(this).html();            
            if(text.length > 0){   
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=transparent&color=000000&font=NEOSANSM.TTF&height=20px&size=20px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        if(!DeltaSky.IsIE6()){
            $(".rounded-listing-group a.listing-name").each(function(){           
                var text = $(this).html();            
                if(text.length > 0)
                {   
                    $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=transparent&color=ffffff&font=NEOSANSM.TTF&height=22px&size=22px&text=' + escape(text) + '" alt="' + text + '"/>');
                }
            });
        }

        $(".home-destination-post-label .post-label").each(function(){           
            var text = $(this).html();            
            if(text.length > 0)
            {   
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSM.TTF&height=16px&size=16px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        $("h1.section-title a").each(function(){           
            var text = $(this).html();            
            if(text.length > 0)
            {   
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSL.TTF&height=30px&size=30px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        
        $("div.page-title a").each(function(){           
            var text = $(this).html();            
            if(text.length > 0)
            {   
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSL.TTF&height=30px&size=30px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        $("div.page-title h1").each(function(){           
            var text = $(this).html();            
            if(text.length > 0)
            {   
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSL.TTF&height=30px&size=30px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        $("h1.page-title a").each(function(){           
            var text = $(this).html();            
            if(text.length > 0)
            {   
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSL.TTF&height=30px&size=30px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        $("h1.page-title span.img-replace").each(function(){           
            var text = $(this).html();            
            if(text.length > 0)
            {   
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=ffffff&color=000000&font=NEOSANSL.TTF&height=30px&size=30px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
        $("h2.listing-title").each(function(){           
            var text = $(this).html();            
            if(text.length > 0){   
                $(this).html('<img src="http://deltaskymag.delta.com/DeltaSky/FontWriter/fontwriter.ashx?bcolor=transparent&color=000000&font=NEOSANSM.TTF&height=20px&size=20px&text=' + escape(text) + '" alt="' + text + '"/>');
            }
        });
        
    }
    ,
    SlideShowSet:function(){
        var images = $(".slide-image");
        var texts = $(".slide-text");
        var pages = $(".slide-page"); 
        
        for(var s = 0; s < pages.length; s++)
        {
            if(s == 0)
            {
                $(images[s]).css("display","block");
                $(texts[s]).css("display","block");
            }
        
            $(pages[s]).attr("pageid",s);
          
            $(pages[s]).click( function(){ 
                DeltaSky.SlideShowGetPage($(this).attr("pageid")); 
            }); 
        }
        
        $(".slide-back").click(function(){
            DeltaSky.SlideShowBack();
        });

        $(".slide-next").click(function(){
            DeltaSky.SlideShowNext();
        });
    }
    ,
    SlideShowBack:function(){
        // first find current slide
        var images = $(".slide-image");
        var current = null;
        for(var g = 0; g < images.length; g++){
        
            if($(images[g]).css("display") == "block"){
            
                if(g > 0){
                    DeltaSky.SlideShowGetPage(g - 1);
                }
            }
        }        
    }
    ,
    SlideShowNext:function(){
        // first find current slide
        var images = $(".slide-image");
        var current = null;
        for(var g = 0; g < images.length; g++){
        
            if($(images[g]).css("display") == "block"){
            
                if(g < images.length - 1){
                    DeltaSky.SlideShowGetPage(g + 1);
                }
            }
        }        
    }
    ,
    SlideShowGetPage:function(id){
        var images = $(".slide-image");
        var texts = $(".slide-text");
        var pages = $(".slide-page"); 
        
        for(var g = 0; g < images.length; g++)
        {
            if($(images[g]).css("display") == "block")
            {
                 $(images[g]).fadeOut(200,function(){DeltaSky.SlideShowFadeIn(id)});
                 $(texts[g]).fadeOut(200);
            }
            
            if(g == id)
            {
                $(pages[g]).css("color","#999999");
            }
            else
            {
                $(pages[g]).css("color","#FFFFFF");
            }
        }
    }
    ,
    SlideShowFadeIn:function(id) {
            
        var images = $(".slide-image");
        var texts = $(".slide-text");
        
        for(var g = 0; g < images.length; g++)
        {
            if(g == id)
            {
                 $(images[g]).fadeIn(200);
                 $(texts[g]).fadeIn(200)
            }                
        } 
    }
    ,
    HomeSlideShowSet:function(){
        var images = $(".slide-image");
        //var texts = $(".slide-text");
        var pages = $(".slide-page"); 
        
        for(var s = 0; s < pages.length; s++)
        {
            if(s == 0)
            {
                $(images[s]).css("display","block");
                //$(texts[s]).css("display","block");
            }
        
            $(pages[s]).attr("pageid",s);
          
            $(pages[s]).click( function(){ 
                DeltaSky.HomeSlideShowGetPage($(this).attr("pageid")); 
            }); 
        }
    }
    ,
    HomeSlideShowGetPage:function(id){
        var images = $(".slide-image");
        //var texts = $(".slide-text");
        var pages = $(".slide-page"); 
        
        for(var g = 0; g < images.length; g++)
        {
            if($(images[g]).css("display") == "block")
            {
                 $(images[g]).fadeOut(200,function(){DeltaSky.HomeSlideShowFadeIn(id)});
                 //$(texts[g]).fadeOut(200);
            }
            
            if(g == id)
            {
                $(pages[g]).css("color","#000");
            }
            else
            {
                $(pages[g]).css("color","#000");
            }
        }
    }
    ,
    HomeSlideShowFadeIn:function(id) {
        var images = $(".slide-image");
        //var texts = $(".slide-text");
        
        for(var g = 0; g < images.length; g++)
        {
            if(g == id)
            {
                 $(images[g]).fadeIn(200);
                 //$(texts[g]).fadeIn(200);
            }                
        } 
    }
    ,
    VoteSet:function(){    
        $("#your-vote").click(function(){
        
            if($("#vote").css("display") == "none")
            {
                $("#vote").slideDown(400);
            }
            else
            {
                //$("#vote").slideUp(400);
            }
        });
        
        $(".close-rate").click(function(){
            $("#vote").slideUp(400);                       
        });
        
        $(".vote0").click(function(){
            DeltaSky.VoteNow(0);
        });

        $(".vote1").click(function(){
            DeltaSky.VoteNow(1);                        
        });

        $(".vote2").click(function(){            
             DeltaSky.VoteNow(2);           
        });

        $(".vote3").click(function(){
            DeltaSky.VoteNow(3);
        });

        $(".vote4").click(function(){
            DeltaSky.VoteNow(4);
        });

        $(".vote5").click(function(){
            DeltaSky.VoteNow(5);
        });
    }
    ,
    VoteNow:function(rating) {
        $.ajax({
            url: "http://deltaskymag.delta.com/DeltaSky/actions/Rate.aspx?nodeid=" + $(".choice .node").html() + "&rating=" + rating,
            cache: false,
            success: function(){
                $("#vote .container .options").html("<p>Thank you for your rating.</p>");
            }
        });
    }
}

var Map =
{
    MapElementId: '',
    DefaultZoom: 10,
    
    AddMarker: function(map, point, icon, infoWindowHtml, address) {
        var marker = new GMarker(point, icon);
        map.addOverlay(marker);
        
        // Add listener for info window.        
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(infoWindowHtml);
            
            $("#GoogleSubmit").click(function() {
                var googleMapsUrl = 'http://maps.google.com/maps?';
                
                var spaceRegex = /\s/;
                var saddr = $("#StartAddress").attr("value");
                while (spaceRegex.test(saddr) == true) {
                    saddr = saddr.replace(spaceRegex, '+');
                }
                
                var daddr = address;
                while (spaceRegex.test(daddr) == true) {
                    daddr = daddr.replace(spaceRegex, '+');
                }
                
                var href = googleMapsUrl + 'saddr=' + saddr + '&' + 'daddr=' + daddr;    

                window.open(href, "window");
            });
        });
        
        // HACK: window.open event was not occurring on the first opening of the info window,
        // so we force the info window to open and close when the map loads.
        marker.openInfoWindowHtml(infoWindowHtml);
        marker.closeInfoWindow();
    },
    
    Init: function(mapElementId, zoom) {
        this.MapElementId = mapElementId;
        this.DefaultZoom = zoom;
        
        // Dismantles all registered event handlers in order to prevent memory leaks.
        $("body").unload(function () { Map.unload(); });
        
        // By default, the map element has 'display: none', so we need to change that on init.
        $('#' + this.MapElementId).show();
    },
    
    /**
     * Returns a GLatLng point for the first item with usable latitude/longitude values.
     *
     * @return GLatLng point
     */
    GetCenterPoint: function() {
        var point = null;
        
        $('.map-data').each(function(i) {
            if (point === null) {
                var latitude = $(this).find('div.latitude').text();
                var longitude = $(this).find('div.longitude').text();
                point = new GLatLng(latitude, longitude);
            }
        });
        
        return point;
    },
    
    /**
     * Returns a GIcon
     *
     * @return GIcon
     */
    GetIcon: function() {
        var icon = new GIcon();

        icon.image = "http://blogs.deltaskymag.com/images/maps/i-pointer.png";
        icon.iconSize = new GSize(55, 53);
        icon.iconAnchor = new GPoint(0, 0);

		icon.shadow = "http://blogs.deltaskymag.com/images/maps/i-shadow.png";
		icon.shadowSize = new GSize(55, 53);

        icon.infoWindowAnchor = new GPoint(0, 0);

		return icon;
    },
    
    /**
     * Finds data for the map within the page and plots the items on the map.
     *
     * @return void
     */
    Load: function() {
        if ($('.map-data').length > 0) {
            // Instantiate the Google Map.        
            var map = new GMap2(document.getElementById(this.MapElementId));
            //map.setUIToDefault();
            map.addControl(new GSmallMapControl());
            
            // Center the map on the first location.
            map.setCenter(this.GetCenterPoint(), this.DefaultZoom);
            
            // Map each location.
            $('.map-data').each(function(i) {
                var latitude = $(this).find('div.latitude').text();
                var longitude = $(this).find('div.longitude').text();
                var point = new GLatLng(latitude, longitude);
                
                var infoWindowHtml = '';
                infoWindowHtml += '<div class="map-info-window" style="">';
                infoWindowHtml += '<div class="name">' + $(this).find('div.name').text(); + '</div>';
                
                infoWindowHtml += '<div class="address">';
                infoWindowHtml += '<span>' + $(this).find('div.addressline1').text() + '</span>';
                if ($(this).find('div.addressline2').text() !== '') {
                    infoWindowHtml += '<br /><span>' + $(this).find('div.addressline2').text() + '</span>';
                }
                infoWindowHtml += '<br /><span>' + $(this).find('div.city').text() + ', ' + $(this).find('div.region').text() + ' ' + $(this).find('div.postalcode').text() + '</span>';
                infoWindowHtml += '</div>';
                infoWindowHtml += '<div class="phone">' + $(this).find('div.phone').text() + '</div>';
                
                //infoWindowHtml += '<div class="prompt"><strong>Get directions</strong> by entering your starting address below:</div>';
                infoWindowHtml += '<div class="prompt"><strong>Enter starting address to get directions.</strong></div>';
                infoWindowHtml += '<input id="StartAddress" class="text" type="text" /> ';
                infoWindowHtml += '<input id="GoogleSubmit" class="button" type="button" value="Go" />';
                infoWindowHtml += '</div>';
                
                var address = $(this).find('div.addressline1').text() + ' ' + $(this).find('div.addressline2').text() + ' ' + $(this).find('div.city').text() + ' ' + $(this).find('div.region').text() + ' ' + $(this).find('div.postalcode').text();
                
                Map.AddMarker(map, point, Map.GetIcon(), infoWindowHtml, address);
            });
        }
    },
    
    Unload: function() {
        GUnload();
    }
}

$(document).ready(function() {
    DeltaSky.Init();
});

// EOF
