/*------------------------------------------------------------------------------
    JS Document (https://developer.mozilla.org/en/JavaScript)

    project:    Victorinox V2
    created:    2009-09-28
    author:     Guillaume KEMPFER

    summary:		
----------------------------------------------------------------------------- */

/*  =CONSTANTES
----------------------------------------------------------------------------- */
var d = document;
var w = window;
var debug = null;
window.config = {
    debug : false
}

/*  =UTILITIES
----------------------------------------------------------------------------- */
/* getElementsByClassName when not supported */
var matchAll = function() {
    var node = arguments[1] || document;
    var elms = node.getElementsByTagName('*');
    var className = arguments[0];
    if (d.getElementsByClassName) {
        return node.getElementsByClassName(className);
    }
    else {
        var regExp = new RegExp('\\b'+className+'\\b');
        var array = [];
        for (var i = 0; i < elms.length; i++) {
            var current = elms[i];
            if (current.className.match(regExp)) {
                array.push(current);
            }
        }
        return array;
    }
};

/* logs into a textarea for IE and in the console for others */
var debuger = function() {
    if (window.config.debug && !debug && typeof console == 'undefined') {
        var parent = d.getElementsByTagName('div')[0];
        var body   = d.getElementsByTagName('body')[0];
        var debug  = d.createElement('textarea');
        debug.setAttribute('rows', 10);
        debug.setAttribute('cols', 80);
        debug.setAttribute('name', 'debug');
        body.insertBefore(debug, parent);
        window.debug = debug;
    }
    else if (window.config.debug && console) {
        log('Debug mode : on');        
    }
};
var log = function(x) {
    if (typeof console != 'undefined')
        console.log(x);
    else if (debug) {
        debug.value += x + '\n';
        debug.scrollTop = debug.scrollHeight;
    }
};

jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    return this.each(function() {
      var targetOffset = $(this).offset().top;
      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});


/*  =FUNCTIONS
----------------------------------------------------------------------------- */

var removeValue = function( item, value ) {

	if (item.value == value ) { item.value='' };
	
}

var setDefaultLanguage = function () {
	
	var lang = "greatbritain";
	
	switch( navigator.language.split('-')[0]){
	
		case 'en' : lang = "greatbritain"; break;
		case 'fr' : lang = "france"; break;
		case 'es' : lang = "spain"; break;
		case 'de' : lang = "germany"; break;
		default   : lang = "greatbritain"; break;
	}
	
	return lang;
	
}

var showMenu = function ( element ) {
	
	$( element+" ul" ).hide();
	$( element  ).hover(
		function () {
			if ( $(this).find("ul:animated").length == 0 ) {
      	$(this).find("ul").fadeIn(300);
      }
    }, 
    function () {
      $(this).find("ul").fadeOut(100);
    }
	);
}



var showDelivery = function() {
	
	$( '#deliveryDifferent:checked' ).val() == 'on' ? $( '#my-delivery-infos' ).show() : $( '#my-delivery-infos' ).hide();
	
	$( '#deliveryDifferent' ).click(
		function() {
			if ( $( '#deliveryDifferent:checked' ).val() == 'on' ) {
				$( '#my-delivery-infos' ).slideDown(300);
				$( '#my-delivery-infos' ).scrollTo( 800 );
			}
			else{
				$( '#my-delivery-infos' ).slideUp(300);
			}
		}
	);
}

var showGiftMessage = function() {
	
	$( '#isagift:checked' ).val() == 'on' ? $( '#giftMessage' ).show() : $( '#giftMessage' ).hide();
	
	$( '#isagift' ).click(
		function() {
			$( '#giftMessage' ).toggle();
	})
	
	$( '#giftMessage' ).focus( 
		function() {
			$( this ).empty();
	})

	
}

var textLimit = function( zone, max ) {
	if( zone.value.length >= max ){
		zone.value = zone.value.substring( 0, max );
	}
}

// IE6 only
var gimmeMyCSS2 = function(){
	
	jQuery('input[type=text], input[type=password]').addClass('text-input');
	
}

var horizontalScroll = function(){
  //Get our elements for faster access and set overlay width
  var div = $('div.items'),
      items = $('.inner-items'),
      // unordered list's left margin
      itemPadding = 16;

  //Get menu width
  var divWidth = div.width();

  //Remove scrollbars
  div.css({overflow: 'hidden'});

  //Find last image container
  var lastItem = items.find('.product:last-child');

  //When user move mouse over menu
  div.mousemove(function(e){

    //As images are loaded ul width increases,
    //so we recalculate it each time
    var itemsWidth = lastItem[0].offsetLeft + lastItem.outerWidth() + itemPadding;

    var left = (e.pageX - div.offset().left) * (itemsWidth-divWidth) / divWidth;
    div.scrollLeft(left);
  });
  
  $( '.product' ).hover(
  	function() {
  		$(this).css( 'background-color', '#eee' );
  	},
  	function() {
  		$(this).css( 'background-color', 'transparent' );
  	}
  )
}

var checkOthersCountry = function(){
	$( '#registerForm' ).submit(function(){
		// On submit disable its submit button
		$( '#registerButton' ).css("visibility","hidden");
	});
	$( '#countryList' ).change( function() {
		if ( ( $( '#countryList' ).val() == 'latam' ) ||  ( $( '#countryList' ).val() == 'usa' ) || ( $( '#countryList' ).val() == 'canada' ) ) {
		
			if ( $( '#countryList' ).val() == 'latam' ){ 
				popupcentree( $( '#popupLatamUrl' ).val(), '', '650', '500', '');
			}
			else if ( $( '#countryList' ).val() == 'usa' ) {
				window.location.href = $( '#popupUsaUrl' ).val();
			}
			else if ( $( '#countryList' ).val() == 'canada' ) {
				popupcentree( $( '#popupCanadaUrl' ).val(), '', '650', '250', '');
			}
			
			$( '#registerButton' ).css("visibility","hidden");			
		}
		else {
			$( '#registerButton' ).css("visibility","visible");
		}
	} )
}


/*  =WINDOW.ONLOAD
----------------------------------------------------------------------------- */
jQuery(document).ready(function(){

	horizontalScroll();
  showMenu( ".lang-list" );
  showMenu( "#other-sites" );
  showDelivery();
  showGiftMessage();
  checkOthersCountry();
  
  // Classes
  if (jQuery('html').hasClass('msie6')){
	
      // IE 6 FUNCTIONS ONLY
			gimmeMyCSS2();
	
  }

});


function resizeFlashContainer( newHeight, newWidth ) {
	var idFlashContainer="#flash-block";
	$(idFlashContainer).css('height', newHeight+"px" ); 
	$(idFlashContainer).css('width', newWidth +"px" ); 
};