﻿/*
Title:      Main Javascript
Author:     Martin Cayouette, martin.cayouette@nurun.com
Updated:    May 12 2011

Content:    
            Culture Selection
            Init for all pages
                Title Animation

*/

"use strict";
// ------[ Culture Selection ]------------------------------------------------- /

/*
    if (event.preventDefault) {
        event.preventDefault(); // Firefox, Safari, Opera
    } else {
        event.returnValue = false; // IE
    }
*/

CANAMOFFROAD.pages.cultureSelection = {
    //http://api.jquery.com/animate/
    culture: null,
    selectBox: null,
    eventSelector: null,

    Init: function(eventSelectorID, selectBoxID, cultureID) {
        var inst = this;
        var culture = cultureID;
        var eventSelector = $("#" + eventSelectorID);
        var selectBox = $("#" + selectBoxID);
        if (eventSelector && selectBox) {
            //console.log(selectBoxID);
            selectBox.css({ visibility: 'visible' });

            var Height = selectBox.height();
            selectBox.addClass("closed");

            /*
            *   Added Open/Close BOX Click Event with the Select button
            *   method <Fonction> replaceClass ( el , oldClassName , newClassName )
            */
            var duration = 800;
            if ($.browser.msie) {
                var attributesIn = {
                    height: Height
                };
                var attributesOut = {
                    height: 0
                };
            } else {
                var attributesIn = {
                    opacity: 1,
                    height: Height
                };
                var attributesOut = {
                    opacity: 0,
                    height: 0
                };
            }

            eventSelector.click(function(event) {
                event.preventDefault();
                if (!selectBox.hasClass("opened")) {
                    //Removed the starting flicker... (onStart)
                    $(this).addClass("selected");
                    //setStyle
                    selectBox.css({ height: '0px' })
                        .removeClass("closed").addClass("opened")
                        .animate(attributesIn, duration, function() {
                            //(opacity) Need to remove all the element filter with his style value for IE so i can't do : selectBox.css("filter", "");
                            /*
                            if ($.browser.msie) {
                            selectBox.removeAttr("style")
                            .css({ zoom: "1", height: Height });
                            } */
                        });
                } else {
                    $(this).removeClass("selected"); //onStart
                    selectBox.animate(attributesOut, duration, function() {
                        // Animation complete.
                        selectBox.removeClass("opened").addClass("closed");
                        return;
                    });
                }
            });

            /*
            *   Added Analytics Event in link
            */
            var a = selectBox.find("a");
            for (var i = 0; i < a.length; i++) {
                $(a[i]).click(function(event) {
                    CANAMOFFROAD.pages.cultureSelection.TrackEvent(this);
                    return false;
                });
            }

        } //IF

    },

    TrackEvent: function(element) {
        /*
        *   These parameters are set in the back-end web page (.aspx)
        */
        var aParams = element.getAttribute('trackEvent');
        aParams = aParams.split(";");
        //Exemple : 'Redirections', 'Ski-Doo', 'en-us'
        try {
            trackPageEvent(aParams[0], aParams[1], aParams[2]);
        } catch (err) { }
    }

};

// ------[ Init for all pages ]------------------------------------------------- //
$(document).ready(function() {
   
    
});
