﻿/*===================================	
    main.js
    www.tigerspike.co.uk    
    by Julien Decaudin http://www.juliendecaudin.com
    27/10/2011
===================================*/

$(document).ready(function () {
    //--------------------------------------------- SETTINGS */
    var AppSettings = {
        //GLOBAL
        debug: false,
        jsClass: "js",
        ieClass: "ie",
        isIE6: ($.browser.msie && $.browser.version.substr(0, 1) < 7) ? true : false,

        //MEDIA QUERIES INFO
        mqMaxWidthHandled: 480,

        //CAROUSEL
        carouselSpeed: 700,
        carouselTimeout: 5000
    };

    //--------------------------------------------- SETUP */
    var Main = {
        run: function () {
            $('html').addClass(AppSettings.jsClass);
            if ($.browser.msie && $.browser.version.substr(0, 1) < 9) $('html').addClass(AppSettings.ieClass);

            Carousel.setup();
            Forms.setup();

            if (!navigator.userAgent.match(/Android/i) &&
                !navigator.userAgent.match(/webOS/i) &&
                !navigator.userAgent.match(/iPhone/i) &&
                !navigator.userAgent.match(/iPad/i) &&
                !navigator.userAgent.match(/iPod/i)) {
                Gmap.setup();
            }

            Misc.setup();
        }
    };

    //--------------------------------------------- CAROUSEL */
    var Carousel = {
        setup: function () {
            //INTRO CAROUSEL (NO NAV, FADE)
            $('.carousel', '#intro').each(function () {
                var items = $(this).find('ul');

                //Apply carousel
                items.cycle({
                    fx: 'fade',
                    timeout: AppSettings.carouselTimeout,
                    speed: AppSettings.carouselSpeed,
                    next: $(this),
                    manualTrump: false
                });
            });

            //CONTENT CAROUSEL (NAV, SWIPE)
            $('.carousel', '#content').each(function () {
                var items = $(this).find('ul');
                var controls = $(this).find('.carousel-controls');

                //Apply carousel
                items.cycle({
                    fx: 'scrollBothWays',
                    timeout: AppSettings.carouselTimeout,
                    speed: AppSettings.carouselSpeed,
                    easing: 'easeOutQuart',
                    prev: controls.find('a.previous'),
                    next: controls.find('a.next'),
                    pager: controls.find('nav'),
                    manualTrump: false,
                    before: function () {
                        controls.find('p').html($(this).find('p').html());
                    }
                });

                //Click
                items.click(function () {
                    $(this).cycle('next');
                });

                //Swipe
                items.swipe({
                    swipeRight: swipeRight,
                    swipeLeft: swipeLeft
                });

                function swipeRight(event) {
                    $(this).cycle('prev');
                }

                function swipeLeft(event) {
                    $(this).cycle('next');
                }
            });
        }
    };

    //--------------------------------------------- FORMS */
    var Forms = {
        setup: function () {
            //Clearable text boxes
            $('.clearable').each(function (e) {
                var field = $(this);
                var label = field.parents('form').find('label[for=' + field.attr('id') + ']');
                var defaultValue = $.trim(label.text());

                //$(this).attr('autocomplete', 'off');

                //init
                if (field.attr('value') != '') {
                    label.hide();
                }

                //focus
                field.focus(function () {
                    label.attr('class', 'edit');
                });

                //blur
                field.blur(function () {
                    if (field.attr('value') == '') {
                        label.show();
                    }
                    label.attr('class', '');
                });

                //key stroke
                field.keydown(function (e) {
                    if (e.keyCode != 9 && //tab
                        e.keyCode != 16 && //shift
                        e.keyCode != 17 && //ctrl
                        e.keyCode != 18 && //alt
                        field.attr('value') == '') {
                        label.hide();
                        label.attr('class', '');
                    }
                });

                //change
                field.change(function (e) {
                    label.hide();
                    label.attr('class', '');
                });
            })

            //Dropdown replacement
            $('select').selectBox();

            //Checkbox replacement
            $('input[type=checkbox]').checkBox();
        }
    }

    //--------------------------------------------- GOOGLE MAP */
    var Gmap = {
        setup: function () {

            if ($('.gmap-generated').length > 0) {

                $('.location a.link-gmap').each(function (n) {
                    var link = $(this);
                    var gmapId = link.parents('.location').find('.gmap-generated').attr('id');

                    //Generate Gmap object on click only
                    link.click(function () {
                        var addressParam = Utils.getURLParam(link.attr('href'), 'q');
                        var geocoder = new google.maps.Geocoder();

                        if (geocoder) {
                            //Get Geocode and generate Google Map object
                            geocoder.geocode({ 'address': addressParam }, function (results, status) {
                                if (status == google.maps.GeocoderStatus.OK) {
                                    //console.log($(results[0].geometry.location));
                                    var lat = results[0].geometry.location.lat();
                                    var lng = results[0].geometry.location.lng();

                                    var latlng = new google.maps.LatLng(lat, lng);
                                    var options = { zoom: 15, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP };
                                    var map = new google.maps.Map(document.getElementById(gmapId), options);
                                    var marker = new google.maps.Marker({
                                        position: latlng,
                                        title: link.attr('title')
                                    });
                                    marker.setMap(map);
                                }
                            });
                        }
                    });

                    //Apply link colorbox overlay
                    link.colorbox({
                        inline: true,
                        transition: 'none',
                        opacity: 0.7,
                        width: 600,
                        height: 452,
                        href: '#' + gmapId
                    });
                });
            }
        }
    }

    //--------------------------------------------- MISC */
    var Misc = {
        setup: function () {
            //Mobile header shadow click handler
            $('#header-shadow').click(function () {
                document.location = $('#logo').attr('href');
            });

            //Mobile main nav bleed fix
            if ($('header nav ul a:last').hasClass('current')) {
                $('header nav ul').addClass('bleedfix');
            }

            //Side boxes height matching
            Misc.initBoxHeight($('.box .inner', '#boxes'));
            Misc.initBoxHeight($('.box-product .inner', 'footer'));

            $('.location-row', '#content').each(function () {
                Misc.initBoxHeight($('.location', $(this)));
            });
        },

        initBoxHeight: function (el) {
            Misc.setBoxHeight(el);

            $(window).resize(function () {
                el.css('height', 'auto');
                Misc.setBoxHeight(el);
            });
        },

        setBoxHeight: function (el) {
            if ($(window).width() > AppSettings.mqMaxWidthHandled) {
                var boxMaxHeight = 0;
                //get tallest box height
                el.each(function (n) {
                    if ($(this).height() > boxMaxHeight) {
                        boxMaxHeight = $(this).height();
                    }
                });

                //apply tallest box height to smaller boxes
                el.each(function (n) {
                    $(this).height(boxMaxHeight);
                });
            }
        }
    }

    //--------------------------------------------- UTILS */
    var Utils = {
        getURLParam: function (strHref, strParamName) {
            var strReturn = "";

            if (strHref.indexOf("?") > -1) {
                var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
                var aQueryString = strQueryString.split("&");
                for (var iParam = 0; iParam < aQueryString.length; iParam++) {
                    if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
                        var aParam = aQueryString[iParam].split("=");
                        strReturn = aParam[1];
                        break;
                    }
                }
            }
            return strReturn;
        }
    }

    //--------------------------------------------- MAIN */
    Main.run(); //invoke app   
})
