﻿
// Waypoints buttons
function windowWidth() {
	window_width = $(window).width();
	if (window_width > 1040) {
		$(".waypoints .forward").css("right", -44);
		$(".waypoints .back").css("left", -44);
	} else {
		$(".waypoints .forward").css("right", -23);
		$(".waypoints .back").css("left", -23);
	}
}

var themeIndex = 1;

$(document).ready(function () {

    var window_width = 0;

    //carousel

    if (hj.home) {
        $('.home .waypoints-journeys').hcCarousel();
        $('.home .waypoints-themes').hcCarousel().hide();
        $('.imageoftheweek_c').hcCarousel(".imageoftheweek_c");

        $('.waypoints-themes li').hover(function () {
            var _themeimg = $('.teaser-hover-image', this).val();
            if (_themeimg && _themeimg.length > 0) {
                showGlobe(_themeimg);
            }
        }, function () { showGlobe($('.globe-themes').val()); });

    }
    else if (hj.page == 'theme') {

        $('.home .waypoints-themes').hcCarousel({ index: themeIndex });

    }
    else {
        $('.jpoints').hcCarousel({ index: hj.currentItemIndex });
    }

    if (hj.home) {
        if (hj.journeys_count <= 4)
            $(".waypoints .arrow").hide();

        if (hj.images_of_the_week_count <= 4) {
            $(".imageoftheweek .arrow").hide();
        }
    }
    else if (hj.page == 'theme') {
        if (hj.themescount <= 4)
            $(".waypoints .arrow").hide();
    }
    else {
        //carousel arrows
        if (hj.journey_points_count <= 11) {
            $(".waypoints .arrow").hide();
        }
    }

    $(".viewmaplink").fancybox({
        'hideOnContentClick': false,
        'callbackOnClose': function () {
            $("#fancy_content").empty();
        }
    });


    //WAYPOINTS LI HOVER
    $(".homewaypoints li").hover(function () {

        $('.image_mask', $(this).siblings()).show();

        $(this).siblings(":not('.active')").removeClass('selected');
        cufSel = $('a.focus', $(this).siblings(":not('.active')"));
        //setTimeout(function () { Cufon.replace(cufSel, { color: '#1a6d77' }) }, 0);
        Cufon.replace(cufSel, { color: '#1a6d77' });

        $(this).addClass('selected');


        Cufon.replace($('a.focus', this), { color: '#ffffff' });

    }, function () {

        if ($("li.active", $(this).parent()).length == 0) {

            $(".image_mask", $(this).parent()).hide();

            $(this).removeClass('selected');

            Cufon.replace($('a.focus', this), { color: '#1a6d77' });

        }
        else {

            $("li:not('.active')", $(this).parent()).removeClass('selected');

            if (!$(this).hasClass("active"))
                Cufon.replace($('a.focus', this), { color: '#1a6d77' });
        }

    });






    windowWidth();
    $(window).resize(windowWidth);

    // journeys/themes tabs
    if (hj.home) {
        $(".stripe .home .choose-selector a").click(function () {

            if (!$(this).hasClass("selected")) {

                var obj = $(this).attr("class");
                $(".choose-title, .stripe .waypoints").hide();
                $(".choose-" + obj).show();
                $(".waypoints-" + obj).show();
                $(".stripe .home .choose-selector a").removeClass("selected");
                $(this).addClass("selected");
                //Cufon.refresh(); -- seems slow - and seems to make no difference                

                $(".waypoints-" + obj).hcCarousel();

                $('.copy').hide();
                $('.copy-' + obj).show();
                showGlobe($('.globe-' + obj).val());

                //show imageoftheweek
                $(".imageoftheweek").show();

                //show globe image
                $(".mainjourneyindex .globe").show();

                //hide google map
                $('.map_container').hide();

                //hide all journey summaries
                $(".journey_summary .journeyindex").hide();

                //reset main carousel
                $(".homewaypoints li").removeClass('selected').removeClass('active');

                setTimeout(function () { Cufon.replace($('.homewaypoints li a.focus'), { color: '#1a6d77' }); }, 0);

                $(".homewaypoints li .image_mask").hide();

                //analytics
                try {
                    hj.trackEvent("pageTracker._trackEvent('Home page Journeys/themes TAB click', '" + obj + "')");
                } catch (err) { };


            }
            return false;
        });
    }

    //journey details
    if (!hj.home) {
        $(".jpoints li").mouseenter(function () {
            $(".jpoints li").removeClass("selected");
            $(this).addClass("selected");
        });

        $(".jpoints li").mouseleave(function () {

            $(".jpoints li").each(function (index) {
                if ($(this).hasClass("active"))
                    $(this).addClass("selected");
                else
                    $(this).removeClass("selected");
            });
        });

    }


    Cufon.replace('h1, h2, .waypoints_large a.focus, .choose-journeys, .choose-themes, .choose-selector .journeys, .choose-selector .themes', { hover: true });

    //SIGNUP FIELD
    $(".register .input_field").focus(function () {
        $(this).val("");
    });



    //PLACES OF INTEREST
    $(".places li").mouseenter(function () {
        $(".places .image_mask").show();
        $(".places li").removeClass("selected");
        $(this).addClass("selected");
    });
    $(".places li").mouseleave(function () {
        $(".places .image_mask").hide();
    });



    //NORMAL HEADER OVERLAY PAGES
    $(".header ul a.regular, .footer ul a.regular").fancybox(settings.regular);

    //LARGE HEADER OVERLAY PAGES
    $(".header ul a.large, .footer ul a.large").fancybox(settings.contributeview);

    //CONTACT HEADER OVERLAY PAGES
    $(".header ul a.contact, .footer ul a.contact").fancybox(settings.contact);


    //SHOW/HIDE HEADER
    $(".header .toggle").click(function () {
        $(".header .copy").toggle("blind", {}, 500);
        $(this).toggleClass("selected");
    });

    //imageoftheweek
    $(".imageoftheweek .thumb a").fancybox(settings.imagesotw);

    $(".imageoftheweek .thumb").mouseenter(function () {
        $(".imageoftheweek .image_mask").show();
        $(".imageoftheweek .thumb").removeClass("selected");
        $(this).addClass("selected");
    });
    $(".imageoftheweek .thumb").mouseleave(function () {
        $(".imageoftheweek .image_mask").hide();
    });


    InitSummaryPanel();
    InitControlPanel();
    InitResultsPanel();

});

function InitSummaryPanel() {

    //SHOW/HIDE WAYPOINT SUMMARY
	$(".waypoints_summary .close").click(function () {
		$(".waypoints_summary .copy").slideUp();
		$(this).parent().next().show();
	});

    $(".waypoints_summary .load").click(function() {
        $(this).prev().slideDown();
        $(this).fadeOut();
    });
}

function InitControlPanel() {

    //CONTRIBITE OVERLAY PAGE
    $(".control_panel .add").fancybox(settings.contributeview);

    //SELECT/DESELECT FILTERS
    $(".control_panel .panel_1 a").click(function () {
    	$(".control_panel .panel_1 li").removeClass("selected");
    	$(this).parent().addClass("selected");
    });

    $(".control_panel .toggle").click(function () {
    	$(".control_panel a").removeClass("toggle_selected");
    	$(".control_panel .content").slideUp();

    	if ($(this).parent().next().css("display") == "block") {
    		$(this).parent().next().slideUp();
    		$(this).removeClass("toggle_selected");
    	}
    	else {
    		$(this).parent().next().slideDown();
    		$(this).addClass("toggle_selected");
    	}

    });

    //DESELECT FILTERS
    $(".top").click(function() {
        $(".control_panel .panel_1 li").removeClass("selected");
    });

    
}

function resizeFancybox(width, height) {
 
    $('#fancy_outer').css('width', width);
 
    if (height)
        $('#fancy_outer').css('height', height);
}


function fancybox_view_details(href) {
    var tmp = settings.detailsview;
    tmp.href = href;
    $.fancybox(tmp);
    return false;
}
function fancybox_map_details(href) {
    var tmp = settings.mapview;
    tmp.href = href;
    $.fancybox(tmp);
    return false;
}

function InitResultsPanel() {
	//SLIDESHARE OVERLAY PAGE
	$(".results .slideshow a").fancybox(settings.slideshow);

	//IMAGE OVERLAY PAGE
	$(".results .thumb a").fancybox(settings.thumbs);

    //CONTRIBITE OVERLAY PAGE
    $(".results .add a").fancybox(settings.contributeview);


    //MAPS OVERLAY PAGE
    $(".results .gmaps a").fancybox(settings.mapview);

    //DETAILS OVERLAY PAGE
    $(".results .details a").fancybox(settings.detailsview);

    $.localScroll();

    //SHOW/HIDE IMAGE INFORMATION
    $(".results li.thumb, .results li.slideshow").mouseenter(function () {
        $(this).children("p").fadeIn();
    });
       $(".results li.thumb, .results li.slideshow").mouseleave(function () {
        $(this).children("p").fadeOut();
    });


    $(".results li.add, .results li.gmaps, .results li.details").mouseenter(function() {
        $(this).addClass("selected");
    });

    $(".results li.add, .results li.gmaps, .results li.details").mouseleave(function() {
        $(this).removeClass("selected");
    });

}

function showGlobe(path) {
    
    if ($(".globe").attr("src") != path)
        $(".globe").attr("src", path);

    
}

function showSummary() {
	$(".waypoints_summary .copy").slideDown();
}

function selectJourneyPoint(obj, p) {
    $(".waypoints_summary .load").hide();
    $(".waypoints li").removeClass("selected");
    $(obj).parent().addClass("selected");
    $(".waypoints_summary .copy").slideUp();
    $(".waypoints_summary ." + p).slideDown();
    return false;
}




window.onscroll = function() {

    var defaultTop = 165

    if (navigator.userAgent.indexOf("Firefox/2") != -1) {
    }


    else {

        var defaultTop2 = $("#columns").offset();

        
        
        //alert(defaultTop2.top);

        try {
            if (window.XMLHttpRequest) { // IE 6 doesn't implement position fixed nicely...
                if (document.documentElement.scrollTop > defaultTop2.top || self.pageYOffset > defaultTop2.top) {
                    $('.control_panel').css('position', 'fixed'); //style.position = 'fixed';
                    $('.control_panel').css('top', '17px'); //.style.top = '0';
                    //$('.control_panel').animate({ top: "10px"}, "slow", "linear"); //.style.top = '0';
                } else if (document.documentElement.scrollTop < defaultTop2.top || self.pageYOffset < defaultTop2.top) {
                    $(document).ready(function() {
                        $('.control_panel').css('position', 'absolute'); //.style.position = 'absolute';
                        $('.control_panel').css('top', '16px'); //.style.top = 'auto';
                    });
                }
            }
            else {

            }

        } catch (err) { }

    }

}


function Journey() {
    
}
Journey.prototype.showLoading = function() {
    $(".ajax-loading").slideDown();
}
Journey.prototype.showSummary = function() {
    $(".ajax-journeysummary").slideDown();
}
Journey.prototype.hideSummary = function(obj, target_id) {
    $(".ajax-journeysummary").slideUp("normal", this.showLoading);
}
Journey.prototype.loadSummary = function(obj, target_id) {

    $(".ajax-journeysummary").slideDown();
    $(".results").fadeIn();
    $(".ajax-loading").slideUp();
}
Journey.prototype.SubscribeToNewsletter = function (panelid, boxid, togglelink) {

    $.post("/webservices/Journey.asmx/SubscribeToNewsletter", { email: $('#' + boxid).val() }, function (resp) {

        if (resp.success) {

            alert(resp.html);

            $(".header .copy").toggle("blind", {}, 500);
            $("#" + togglelink).toggleClass("selected");
            $('#' + boxid).val('');
        } else
            alert(resp.error);

    }, "json");


    return false;
}

function selectPoint(liclass, index, pointid, reverse) {
    
    $(".results").fadeOut();

    try {
        hj.trackEvent("pageTracker._trackEvent('Journey point click', '" + $("." + liclass + " .analyticsjourneyname")[0].value.replace("'", "\'") + "', '" + $("." + liclass + " .analyticsimagelink")[0].innerText.replace("'", "\'") + "')");
    } catch (err) { };

    $(".jpoints li").removeClass("selected");
    $(".jpoints li").removeClass("active");
    $("." + liclass).addClass("active selected");

    $(".ajax-journeysummary").slideUp("normal",

    function () {

        $.post("/webservices/Journey.asmx/loadJourneyDetails", { JourneyPointId: pointid, reverse: reverse ? reverse : false }, function (resp) {

            if (resp.success) {
                $('#' + $(".ajax-journeysummary")[0].id).html(resp.pointssummary);
                $('#columns').html(resp.pointpanels);
                
                journey.loadSummary();
                journey.journeyPointLoaded(liclass, index);
            }

        }, "json");

    }
    );

    return false;
}

Journey.prototype.journeyPointLoaded = function(liclass, index) {
    $(".stripe").attr("class", "stripe");
    $(".stripe").addClass("stripe_" + index);

    Cufon.replace('h1, h2, .journey_summary .more');    

    InitSummaryPanel();
    InitControlPanel();
    InitResultsPanel();    
}

Journey.prototype.applyFilter = function (obj, id, resultspanelid, type) {

    $(".filter li").removeClass("selected");
    $(obj).parent().addClass("selected");

    $(".level li").attr("style", "background: url('/images/loading.gif') no-repeat 100px 100px;");
    $(".level a").hide();

    setTimeout(
    function () {

        $.post("/webservices/Journey.asmx/ApplyFilter", { JourneyPointId: id, Type: type }, function (resp) {

            if (resp.success) {

                $('#' + resultspanelid).html(resp.html);                
                $(".results").fadeIn(1000, InitResultsPanel);

            } else alert(resp.error);

        }, "json");


        /*hcAjax.call(
        "/webservices/Journey.asmx/ApplyFilter",
        "JourneyPointId=" + id + "&Type=" + type + "&ResultsPanelId=" + resultspanelid + "&onSuccess=$(\".results\").fadeIn(1000,InitResultsPanel);",
        null,
        null,
        true);*/
    }
    , 500);

    return false;

}

var journey = new Journey();


//----------------- settings -----------------


var settings = {
    thumbs: {
        'overlayShow': true,
        'showCloseButton': true,
        'width': 685,
        'height': 556,
        'enableEscapeButton': true,
        'overlayColor': '#0c191a',
        'overlayOpacity': 0.8,
        'padding': 0,
        'titleShow': false,
        'type': 'iframe'
    },
    slideshow: {
        'overlayShow': true,
        'showCloseButton': true,
        'width': 685,
        'height': 556,
        'enableEscapeButton': true,
        'overlayColor': '#0c191a',
        'overlayOpacity': 0.8,
        'padding': 0,
        'titleShow': false,
        'type': 'iframe'
    },
    detailsview: {
        'overlayShow': true,
        'showCloseButton': true,
        'width': 800,
        'height': 485,
        'enableEscapeButton': true,
        'overlayColor': '#0c191a',
        'overlayOpacity': 0.8,
        'padding': 0,
        'titleShow': false,
        'type': 'iframe'
    },
    mapview: {
        'overlayShow': true,
        'showCloseButton': true,
        'width': 800,
        'height': 555,
        'enableEscapeButton': true,
        'overlayColor': '#0c191a',
        'overlayOpacity': 0.8,
        'padding': 0,
        'titleShow': false,
        'type': 'iframe'
    },
    contributeview: {
        'overlayShow': true,
        'showCloseButton': true,
        'width': 800,
        'height': 600,
        'enableEscapeButton': true,
        'overlayColor': '#0c191a',
        'overlayOpacity': 0.8,
        'padding': 0,
        'titleShow': false,
        'type': 'iframe'
    },
    contact: {
        'overlayShow': true,
        'showCloseButton': true,
        'width': 800,
        'height': 520,
        'enableEscapeButton': true,
        'overlayColor': '#0c191a',
        'overlayOpacity': 0.8,
        'padding': 0,
        'titleShow': false,
        'type': 'iframe'
    },
    imagesotw: {
        'overlayShow': true,
        'showCloseButton': true,
        'width': 685,
        'height': 556,
        'enableEscapeButton': true,
        'overlayColor': '#0c191a',
        'overlayOpacity': 0.8,
        'padding': 0,
        'titleShow': false,
        'type': 'iframe'

    },
    regular: {
        'overlayShow': true,
        'showCloseButton': true,
        'width': 800,
        'height': 510,
        'enableEscapeButton': true,
        'overlayColor': '#0c191a',
        'overlayOpacity': 0.8,
        'padding': 0,
        'titleShow': false,
        'type': 'iframe'
    }

}

