﻿/*
 * Javascript/JQuery - Main functions
 * @author: valerie.hanesse @ readingroom.com
 * @created: 26.04.2010
 * 
 * ************************************************************/

//GMap intgration based on UK postcode
function initialize(id,lat,lng) {
    var point = new google.maps.LatLng(lat,lng);
    var mapOptions = {
        center: point,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        zoom: 15
    };
    map = new google.maps.Map(document.getElementById(id), mapOptions);
    var marker = new google.maps.Marker({
        position: point,
        map: map,
        icon: "/Design/CHAPS/images/templates/icons/marker.png"
    });
}
function usePointFromPostcode(id,postcode, callbackFunction) {
    var localSearch = new GlocalSearch();
    localSearch.setSearchCompleteCallback(null,
    function() {
        if (localSearch.results[0]) {
            var lat = localSearch.results[0].lat;
            var lng = localSearch.results[0].lng;
            callbackFunction(id, lat, lng);
        } else {
            //alert("Postcode not found!");
        }
    });
    localSearch.execute(postcode + ", UK");
}
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func;
        }
    }
}
function addUnLoadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') {
        window.onunload = func;
    } else {
        window.onunload = function() {
            oldonunload();
            func;
        }
    }
}


jQuery(document).ready(function() {
    //rollover primary navigation
    var menus = jQuery("#primaryNavigation ul:first").children("li");
    if (menus.size() > 0) {
        menus.children("a").each(function() {
            var w = jQuery(this).parent("li").width() + 45;
            jQuery(this).next("div.level2").css({ "width": w + "px" });
            jQuery('<div class="layout right"><span>layout element</span></div><div class="layout left"><span>layout element</span></div>').appendTo(jQuery(this).next("div.level2"));
        });

        menus.children("a").hover(
            function() {
                jQuery(this).next("div.level2").show(0);
            },
            function() {
                jQuery(this).next("div.level2").delay(100).hide(0);
            }
        );
        menus.children("div.level2").hover(
            function() {
                jQuery(this).stop();
                jQuery(this).show(0);
            },
            function() {
                jQuery(this).hide(0);
            }
        );
    }
    /*rollover effect on first level in primary nav*/
    menus.children("a").hover(function() { jQuery(this).parent("li").addClass("hover"); }, function() { jQuery(this).parent("li").removeClass("hover"); });

    /* closed / opened binding*/
    var bindings = jQuery(".binding");
    if (bindings.size() > 0) {
        bindings.each(function(i) {
            var elt = jQuery(this);
            var clickElt = jQuery("<a href=\"#click\" class=\"click\" title=\"open it\">&nbsp;</a>");
            var inner = elt.children("div.inner:first");
            jQuery(clickElt).click(function() {
                if (elt.hasClass("opened")) {
                    if (elt.hasClass("contact")) { elt.find(".map:first").hide(); }
                    inner.slideUp("slow");
                    elt.removeClass("opened");
                    elt.addClass("closed");
                    jQuery(this).attr("title", "open it");
                } else {
                    inner.slideDown("slow", function() { if (elt.hasClass("contact")) { elt.find(".map:first").show(); } });
                    elt.removeClass("closed");
                    elt.addClass("opened");
                    jQuery(this).attr("title", "close it");
                }
            });
            jQuery(clickElt).prependTo(elt);
            if (elt.hasClass("contact")) {
                if (!elt.hasClass("opened")) { elt.addClass("opened"); }
                clickElt.attr("title", "close it");
            } else {
                if (!elt.hasClass("closed")) { elt.addClass("closed"); }
            }
        });
    }

    /* filter - resources */
    var filters = jQuery(".filter");
    if (filters.size() > 0) {
        filters.each(function(i) {
            var elt = jQuery(this);
            var clickElt = jQuery("<a href=\"#click\" class=\"click\" title=\"close it\">&nbsp;</a>");
            var inner = elt.children("div.content:first");
            jQuery(clickElt).click(function() {
                if (elt.hasClass("opened")) {
                    inner.slideUp("slow");
                    elt.removeClass("opened");
                    elt.addClass("closed");
                    jQuery(this).attr("title", "open it");
                } else {
                    inner.slideDown("slow");
                    elt.removeClass("closed");
                    elt.addClass("opened");
                    jQuery(this).attr("title", "close it");
                }
            });
            jQuery(clickElt).prependTo(elt);
            if (!elt.hasClass("opened")) { elt.addClass("opened"); }
        });
    }
    /* draughtboard */
    var draughtboards = jQuery(".draughtboard ul.listing");
    if (draughtboards.size() > 0) {
        draughtboards.each(function() {
            var campains = jQuery(this).children("li");
            var nbCampains = campains.size();
            if (nbCampains > 0) {
                campains.each(function() {
                    var frame = jQuery(this).children(".image-frame");
                    var inner = jQuery(this).children(".inner");
                    var innerClone = inner.clone();


                    // modulo on index to define if li is the first on the line or not
                    var modulo = (jQuery(this).index()) % 4;
                    // if the modulo is different than 0, elt is not the first li on the line and therefore qe need a margin on the left
                    if (modulo > 0) {
                        jQuery(this).addClass("left-margin");
                    }
                    if ((modulo == 2) || (modulo == 3)) {
                        jQuery(this).addClass("left-bubble");
                        innerClone.prependTo(frame);
                    } else {
                        innerClone.appendTo(frame);
                    }
                    inner.remove();
                });
            }
        });
        var thumbnails = draughtboards.find(".image-frame");
        thumbnails.mouseenter(function() {
            var position = jQuery(this).position();
            var clone = jQuery(this).clone();
            clone.appendTo(jQuery(this).closest("ul.listing"));
            clone.wrap('<li class="clone" />');
            var li = clone.parent("li");
            if ((jQuery(this).parent("li")).hasClass("left-bubble")) {
                li.addClass("left-bubble");
                li.css({ "top": position.top + "px", "left": (position.left - 250) + "px" });
            } else {
                li.css({ "top": position.top + "px", "left": position.left + "px" });
            }
            li.mouseleave(function() { jQuery(this).remove(); });
        });
    }

    /*Dropdown*/
    var dropdowns = jQuery(".dropdown");
    if (dropdowns.size() > 0) {
        dropdowns.each(function() {
            var elt = jQuery(this);
            var title = elt.children("label:first").text();
            var newDropdown = '<div class="new-dropdown closed">';
            newDropdown += '<p class="title">' + title + '</p>';
            var options = elt.find("option");
            var nLinks = 0; //there is a list of links which are created from options' value
            nLinks = options.size();
            if (nLinks > 0) {
                newDropdown += '<p class="block select"><span>' + options[0].innerHTML + '</span><a class="click" href="#click">&nbsp;</a></p>';
                newDropdown += '<ul>';
                options.each(function(index) {
                    var link = jQuery(this);
                    if (index > 0) {
                        newDropdown += '<li><a href="' + link.attr("value") + '">' + link.text() + '</a></li>';
                    }
                });
                newDropdown += '</ul>';
                newDropdown += '</div>';
                elt.replaceWith(newDropdown);
            }
        });
    }
    jQuery(".new-dropdown .select").click(function() {
        jQuery(this).closest(".new-dropdown").toggleClass("closed");
    });

    /*text fields*/
    jQuery("input.text").focus(function() {
        var elt = jQuery(this);
        if ((elt.attr("value") == "Enter your postcode") || (elt.attr("value") == "Enter your email")) {
            elt.attr("value", "");
        }
    });
    jQuery("input.text").blur(function() {
        var elt = jQuery(this);
        if (elt.attr("value") == "") {
            if (elt.hasClass("postcode")) {
                elt.attr("value", "Enter your postcode");
            } else if (elt.hasClass("email")) {
                elt.attr("value", "Enter your email");
            } else {
                elt.attr("value", "Enter keywords");
            }
        }
    });

    /* date picker */
    //hook a picker on each .datepicker class elt
    
    
    jQuery('.datepicker').datepicker({maxDate: 'D', dateFormat: 'dd/mm/yy', firstDay: 1});
	//Date range (start date / end date)
	jQuery('.startdate').datepicker('option', {
		onSelect: function(dateText, inst){
			//set after init, that enddate cannot start before startdate
			
	jQuery('.enddate').datepicker("option", "minDate", dateText );
		}
	});

    /* carousel */
    var carousels = jQuery(".carousel ul.listing");
    if (carousels.size() > 0) {
        var l = 110; //l: length thumbnail
        var max = 10; //max thumbnails
        var nbShown = 6; //how many shown in the navigation
        carousels.each(function() {
            var carousel = jQuery(this);
            var slide = jQuery('<div class="slide"><div class="image-frame"><img src="" alt="alternative text here" width="420" height="300" /></div><div class="inner"></div><div class="layout top-left"><span>layout element</span></div><div class="layout top-right"><span>layout element</span></div></div>');
            carousel.before(slide);
            carousel.wrap('<div class="slider-navigation" />');
            carousel.parent().after(jQuery('<div class="layout bottom-left"><span>layout element</span></div><div class="layout bottom-right"><span>layout element</span></div>'));

            var lis = carousel.children("li");
            var nbThs = lis.size(); //nb of thumbnails

            lis.each(function() {
                var elt = jQuery(this);
                elt.css({ "left": elt.index() * l + "px" });
                if (elt.index() == 0) {
                    var mainIllustration = elt.find(".main-illustration a");
                    var slideimg = slide.find("img:first");
                    slideimg.attr({ "src": mainIllustration.attr("href"), "alt": mainIllustration.attr("title") });
                    var inner = elt.find(".inner").clone(true);
                    inner.find("h3").remove();
                    var slideinner = slide.find(".inner");
                    slideinner.replaceWith(inner);
                }
                if (elt.index() > 0) {
                    elt.find("img:first").addClass("off");
                } else {
                    elt.find("img:first").addClass("on");
                }
                if (elt.index() > (max - 1)) {//max lis
                    elt.remove();
                }
            });


            if (nbThs > nbShown) {
                var next = jQuery('<div class="next"><span>next</span></div>')
                var previous = jQuery('<div class="previous off"><span>previous</span></div>');
                carousel.parent().after(next);
                carousel.parent().before(previous);
            } else {
                carousel.parent().addClass("no-navigation");
            }

            carousel.find(".image-frame img").click(function() {
                jQuery(this).removeClass("off");
                jQuery(this).addClass("on");
                var li = jQuery(this).closest("li");
                li.siblings("li").each(function() {
                    var img = jQuery(this).find(".image-frame img");
                    if (!img.hasClass("off")) { img.addClass("off"); }
                    if (img.hasClass("on")) { img.removeClass("on"); }
                });
                var mainIllustration = li.find(".main-illustration a");
                var slideimg = slide.find("img:first");
                slideimg.attr({ "src": mainIllustration.attr("href"), "alt": mainIllustration.attr("title") });
                var inner = li.find(".inner").clone(true);
                inner.find("h3").remove();
                var slideinner = slide.find(".inner");
                slideinner.replaceWith(inner);
            });

            if (nbThs > nbShown) {
                //origin on the rule
                var origin = 0;
                var mini = (-(nbThs - nbShown) * l) + origin;
                //previous click
                previous.click(function() {
                    var elt = jQuery(this);
                    if (!carousel.is(":animated")) {
                        if (!elt.hasClass("off")) {
                            var x = carousel.position().left; //current position
                            if (x < origin) {//we can go to the next
                                x = x + l;
                                carousel.animate({ left: x + "px" }, "slow", function() { if (x == origin) { elt.addClass("off"); } });
                                if (elt.siblings(".next").hasClass("off")) { elt.siblings(".next").removeClass("off"); }
                            }
                        }
                    }
                });

                //next click
                next.click(function() {
                    var elt = jQuery(this);
                    if (!carousel.is(":animated")) {
                        if (!elt.hasClass("off")) {
                            var x = carousel.position().left; //current position
                            if (x > mini) {//we can go to the next
                                x = x - l;
                                carousel.animate({ left: x + "px" }, "slow", function() { if (x == mini) { elt.addClass("off"); } });
                                if (elt.siblings(".previous").hasClass("off")) { elt.siblings(".previous").removeClass("off"); }
                            }
                        }
                    }
                });
            }
        });
    }

    //new window links - if "new" class on the link
    var newLinks = jQuery("a.new");
    if (newLinks.size() > 0) {
        newLinks.append("<span>(open in new window)</span>");
        newLinks.attr("title", "Open in a new window");
        newLinks.click(function() { window.open(jQuery(this).attr("href")); return false; });
        newLinks.keypress(function(e) { window.open(jQuery(this).attr("href")); return false; });
    }

});
