﻿/** store locator query
==============================================
*/
/* Insert your key */
VMAPI.registerKey("JSBS20090508113223435189279162");
/* From address to geographical point */
    var totLI=0;
    /*ZIP code HTTP GET ELABORATION 17/02/2010*/
    var currentCountryVMCode = '1145799'; //usa 
    var currentPoiDefinition = '144716'; //usa
    var nation = getNation();
    function getNation(){
    var domain = window.location.href.replace('http://', '');
    nation = domain.split('/')[1];
    //alert(nation);
    switch(nation)
        {
        case 'usa':
          currentCountryVMCode = '1145799';
          currentPoiDefinition = '153962';
          break;
           case 'canada':
          currentCountryVMCode = '1145795';
          currentPoiDefinition = '153962';
          break;
        case 'france':
         currentCountryVMCode = '1424';
         currentPoiDefinition = '153963';
          break;
          case 'spain':
         currentCountryVMCode = '844';
         currentPoiDefinition = '167148';
          break;
          case 'australia':
         currentCountryVMCode = '1634125';
         currentPoiDefinition = '153961';
          break;
          case 'uk':
         currentCountryVMCode = '1138';
         currentPoiDefinition = '153960';
          break;
          case 'scotland':
         currentCountryVMCode = '1138';
         currentPoiDefinition = '153960';
          break;
        default:
          currentCountryVMCode = '1145799';
          currentPoiDefinition = '153962';
  }
  var zipCode = getQuerystring('zip');
      //   alert(currentCountryVMCode+"-"+currentPoiDefinition);
    }
function getQuerystring(key, default_) {
    if (default_ == null) {
        default_ = "";
    }
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null) {
        return default_;
    } else {
        var zip = qs[1];
        geosearch = new VMGeocoder();
        myaddress = new VMAddress();
        myaddress.zipCode = qs[1];
        myaddress.city = "";
        myaddress.countryVMCode = currentCountryVMCode;
        geosearch.addEventHandler("onCallBack", poi_search);
        geosearch.search(myaddress);
        return zip;
    }
}
/*END ZIP code HTTP GET ELABORATION 17/02/2010*/
function geocoder_search() {
    geosearch = new VMGeocoder();
    myaddress = new VMAddress();
    //var elements_formulaire = document.forms['geocoder_form'].elements;
    var elements_formulaire = document.forms[0].elements;
    //myaddress.zipCode = elements_formulaire['form_CP'].value;
    myaddress.zipCode = elements_formulaire['ctl00$mainContent$form_CP'].value;
    //myaddress.city = elements_formulaire['form_city'].value;
    myaddress.city = elements_formulaire['ctl00$mainContent$form_city'].value;
    myaddress.countryVMCode = currentCountryVMCode;
    geosearch.addEventHandler("onCallBack", poi_search);
    geosearch.search(myaddress);

}

/* With the location, and the POI type, we can start the search */
function poi_search() {
    poiDefinition = new VMPOIDefinition();
    poiDefinition.getDefiniton(currentPoiDefinition); /* */
    myPOIsearch = new VMPOISearch(poiDefinition);
    myPOIsearch.addEventHandler("onCallBack", poi_result);
    myPOIsearch.search(geosearch.result);
}

/* Affichage des responses fournies par le serveur */
function poi_result() {
    $("div.map").css("display", "block");
    $("div.maps").css("display", "block");
    $("#yourmapdiv").css("display", "block");
    $("#yourhtmldiv").css("display", "block");
    $("#store-none").css("display", "none");
    //$("#footer").css("position", "static");
    if (myPOIsearch.result.VMPOIs.length > 0) {
        myPOIlist = myPOIsearch.result;
        document.getElementById("yourmapdiv").innerHTML = "";
        map = new VMMap(document.getElementById("yourmapdiv"));
        var myicon = new VMIcon("/_repository/_gui/2010/logo/rayban_logo.png", -12, -12);
        
        for (i = 0; i < myPOIsearch.result.VMPOIs.length; i++) {
            var myPOIlayer;
            //if (i == 101) iconAddress = '/images/100.png';
            //else 
            iconAddress = 'http://www.viamichelin.com/b2b2c/maf/images/poi/num/0' + (i + 1) + '.png';
            poiHTML = "<div style='width:200px'>";
            poiHTML += '<p><img align="top" style="border-right:5px solid transparent;" src="/_repository/_gui/2010/logo/rayban_logo.png"/><img src="' + iconAddress + '" alt="' + (i + 1) + ')" />&nbsp;' + "<strong>" + myPOIsearch.result.VMPOIs[i].name + "</strong>";
            poiHTML += "<br>" + myPOIsearch.result.VMPOIs[i].displayAddress + "</p>";
            j = 0;
            iconsHTML = "";
            while (j < myPOIsearch.result.VMPOIs[i].metaNums.length) {
                if ((myPOIsearch.result.VMPOIs[i].definition.getCriteriaByNum(j + 1)) && (myPOIsearch.result.VMPOIs[i].definition.getCriteriaByNum(j + 1).isDisplay) && (myPOIsearch.result.VMPOIs[i].definition.getCriteriaByNum(j + 1).type == 0) && (myPOIsearch.result.VMPOIs[i].metaNums[j] != 0)) {
                    iconsHTML += "<img src='/_repository/_gui/2010/logo/rayban_logo.png' align='absmiddle' title='" + myPOIsearch.result.VMPOIs[i].definition.getCriteriaByNum(j + 1).getLibValue(myPOIsearch.result.VMPOIs[i].metaNums[j]) + "'/>&nbsp;";
                }
                j++;
            }
            poiHTML += "<br>" + iconsHTML;
            poiHTML += "</div>";
            var myiconnumber = new VMIcon(iconAddress, 12, 6);
            myiconlayer = myPOIsearch.result.VMPOIs[i].setIconLayer(myicon);
            myiconnumlayer = new VMIconLayer(myPOIsearch.result.VMPOIs[i].coords, myiconnumber);
            map.addLayer(myiconlayer);
            myPOIlayer = myPOIsearch.result.VMPOIs[i].getLayer();
            myPOIlayer.setExpandLayer(poiHTML);
            map.addLayer(myPOIlayer);
            map.addLayer(myiconnumlayer);
        }
        map.drawMapFromLayers();
        map.showMapTools(0);
        text = myPOIlist.getHTML();
        arPOI = text.split('<div');
        textHTML = ''; totLI = 1;
        
        if (arPOI.length == 1) $("#store-total").css("display", "none");
        else {
            $("#screen div.store-prev").css("display", "block");
            $("#screen div.store-next").css("display", "block");
            //$("#store-total").html('&raquo ' + (arPOI.length - 1) + ' Ray-Ban Official Resellers have been found near <span>' + document.forms[0].elements['form_city'].value + '</span>');
            $("#store-total").html('&raquo ' + (arPOI.length - 1) + ' Ray-Ban Official Resellers have been found near <span>' + document.forms[0].elements['ctl00$mainContent$form_city'].value + '</span>');
            $("#store-total").css("display", "block");
        }
        for (i = 1; i < arPOI.length; i++) {
            posLI = (i - 1) % 10;
            if (posLI == 0 && i != 1 && i != arPOI.length) {
                $("#sections ul li." + totLI).html(textHTML);
                $("#sections ul li." + totLI).css("display", "block");
                totLI++; textHTML = '';
            }
            arPOIdati = arPOI[i].split('<br/>');
            arPOInome = arPOIdati[0].split('>');
            arPOInome = arPOInome[2].split('</spa');
            //if (i == 100) iconAddress = '/images/100';
            //else 
            iconAddress = 'http://www.viamichelin.com/b2b2c/maf/images/poi/num/0' + i;
            textHTML += '<div class="store"><div class="store-wrap"><div class="store-name"><img src="' + iconAddress + '.png" alt="' + i + ')" />&nbsp;' + arPOInome[0] + '</div>';
            if (arPOIdati[1] != "") textHTML += '<div class="store-adress">' + arPOIdati[1] + '</div>';
            if (arPOIdati[4] != "") textHTML += '<div class="store-phone">' + arPOIdati[4] + '</div>';
            textHTML += '</div></div>';
        }
        if (textHTML != "") { $("#sections ul li." + totLI).html(textHTML); $("#sections ul li." + totLI).css("display", "block"); }
        if (totLI == 1) $("#navigation ul li.1").css("display", "none");
        else {
            for (i = 1; i <= totLI; i++) { $("#navigation ul li." + i).css("display", "block"); $("#navigation ul li." + i + " span").removeClass("last"); }
            $("#navigation ul li." + (i - 1) + " span").addClass("last");
        }
        for (i = totLI + 1; i < 11; i++) { $("#sections ul li." + i).css("display", "none"); $("#navigation ul li." + i).css("display", "none"); }
        $('#sections').trigger('goto', [0]);
        $("#navigation ul li.1 a").css("color", "#d70000");
        $("#navigation ul li.1 a").css("cursor", "default");
        $("#navigation ul li.1 a.hoover").css("color", "#d70000");
        $("#search-new").css("display", "block");
    } else {
        $("#store-none").html("&raquo It's impossible to find the Ray-Ban Official Resellers with these data!");
        $("#store-none").css("display", "block");
        $("#yourmapdiv").css("display", "none");
        $("#yourhtmldiv").css("display", "none");
        $("div.maps").css("display", "none");
    }

    /*coreMetrics Modified 11/03/2010*/
    //cmCreateConversionEventTag('Store Locator','2','Lead Out','50');
    /*coreMetrics Modified 11/03/2010*/
}
// http://demos.flesler.com/jquery/serialScroll/js/init.js
// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
;(function($){var a='serialScroll',b='.'+a,c='bind',C=$[a]=function(b){$.scrollTo.window()[a](b)};C.defaults={duration:1e3,axis:'x',event:'click',start:0,step:1,lock:1,cycle:1,constant:1};$.fn[a]=function(y){y=$.extend({},C.defaults,y);var z=y.event,A=y.step,B=y.lazy;return this.each(function(){var j=y.target?this:document,k=$(y.target||this,j),l=k[0],m=y.items,o=y.start,p=y.interval,q=y.navigation,r;if(!B)m=w();if(y.force)t({},o);$(y.prev||[],j)[c](z,-A,s);$(y.next||[],j)[c](z,A,s);if(!l.ssbound)k[c]('prev'+b,-A,s)[c]('next'+b,A,s)[c]('goto'+b,t);if(p)k[c]('start'+b,function(e){if(!p){v();p=1;u()}})[c]('stop'+b,function(){v();p=0});k[c]('notify'+b,function(e,a){var i=x(a);if(i>-1)o=i});l.ssbound=1;if(y.jump)(B?k:w())[c](z,function(e){t(e,x(e.target))});if(q)q=$(q,j)[c](z,function(e){e.data=Math.round(w().length/q.length)*q.index(this);t(e,this)});function s(e){e.data+=o;t(e,this)};function t(e,a){if(!isNaN(a)){e.data=a;a=l}var c=e.data,n,d=e.type,f=y.exclude?w().slice(0,-y.exclude):w(),g=f.length,h=f[c],i=y.duration;if(d)e.preventDefault();if(p){v();r=setTimeout(u,y.interval)}if(!h){n=c<0?0:n=g-1;if(o!=n)c=n;else if(!y.cycle)return;else c=g-n-1;h=f[c]}if(!h||d&&o==c||y.lock&&k.is(':animated')||d&&y.onBefore&&y.onBefore.call(a,e,h,k,w(),c)===!1)return;if(y.stop)k.queue('fx',[]).stop();if(y.constant)i=Math.abs(i/A*(o-c));k.scrollTo(h,i,y).trigger('notify'+b,[c])};function u(){k.trigger('next'+b)};function v(){clearTimeout(r)};function w(){return $(m,l)};function x(a){if(!isNaN(a))return a;var b=w(),i;while((i=b.index(a))==-1&&a!=l)a=a.parentNode;return i}})}})(jQuery);
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};


jQuery(function($) {
    /**
    * Most jQuery.serialScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
    * @see http://flesler.demos.com/jquery/scrollTo/
    * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.serialScroll.
    */

    /**
    * The plugin binds 6 events to the container to allow external manipulation.
    * prev, next, goto, start, stop and notify
    * You use them like this: $(your_container).trigger('next'), $(your_container).trigger('goto', [5]) (0-based index).
    * If for some odd reason, the element already has any of these events bound, trigger it with the namespace.
    */

    /**
    * IMPORTANT: this call to the plugin specifies ALL the settings (plus some of jQuery.ScrollTo)
    * This is done so you can see them. You DON'T need to specify the commented ones.
    * A 'target' is specified, that means that #screen is the context for target, prev, next and navigation.
    */
    var $prev = $('div.store-prev'), //prev button
		$next = $('div.store-next'); //next button
    $('#screen').serialScroll({
        target: '#sections',
        items: 'li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
        prev: 'div.store-prev', // Selector to the 'prev' button (absolute!, meaning it's relative to the document)
        next: 'div.store-next', // Selector to the 'next' button (absolute too)
        axis: 'xy', // The default is 'y' scroll on both ways
        navigation: '#navigation li a',
        duration: 0, // Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
        force: true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
        //queue:false,// We scroll on both axes, scroll both at the same time.
        //event:'click',// On which event to react (click is the default, you probably won't need to specify it)
        //stop:false,// Each click will stop any previous animations of the target. (false by default)
        //lock:true, // Ignore events if already animating (true by default)		
        //start: 0, // On which element (index) to begin ( 0 is the default, redundant in this case )		
        cycle: false, // Cycle endlessly ( constant velocity, true is the default )
        //step:1, // How many items to scroll each time ( 1 is the default, no need to specify )
        //jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
        //lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
        //interval:1000, // It's the number of milliseconds to automatically go to the next
        //constant:true, // constant speed
        onBefore: function(e, elem, $pane, $items, pos) {
            $("#navigation ul li a").css("color", "#fff");
            $("#navigation ul li a").css("cursor", "pointer");
            $("#navigation ul li a.hover").css("color", "d70000");
            $("#screen div.store-prev").css("cursor", "pointer");
            $("#screen div.store-next").css("cursor", "pointer");
            $("#navigation ul li." + (pos + 1) + " a").css("color", "#d70000");
            $("#navigation ul li." + (pos + 1) + " a").css("cursor", "default");
            $("#navigation ul li." + (pos + 1) + " a.hover").css("color", "#d70000");
            if (pos == 0) $("#screen div.store-prev").css("cursor", "default");
            if (pos == (totLI - 1)) $("#screen div.store-next").css("cursor", "default");
        }

    });
});

//$(document).ready(function() {
//    $("form input").keypress(function(e) {
//        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
//            $('button[type=submit] .bttSubmit').click();
//            return false;
//        } else {
//            return true;
//        }
//    });
//});

jQuery(document).ready(function() {
$("form input").live("keypress", function (e) {
    if ($(this).parents("form").find("button[type=submit].bttSubmit, input[type=submit].bttSubmit").length <= 0)
    return true;
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        $(this).parents("form").find("button[type=submit].bttSubmit, input[type=submit].bttSubmit").click();
        return false;
    } else {
        return true;
    }
    });
});
