// JavaScript Document
$(document).ready(function(){
	
	$(document).bind("contextmenu",function(e){
		//disable right click
		alert("Onze afbeeldingen zijn met copyright beschermd. Indien u een kopie wilt hebben kunt u contact met ons opnemen.");
        	return false;
    	});
	
	$('#logo').cursorHover();
	
	$('#logo').click(function(){
		window.location.href = "/"
	});
	
	//transparency update ie6
	$("#logo").pngfix();
	$("#languageBarShadow, #menuL0BG .body, #menuL0BG .tail, #contentShadow, #contentBorder1Shadow, #contentBorder2Shadow, #contentBorder3Shadow").pngfix();
	
	//language bar
	$(this).updateLanguageSwitchURL();
	
	//bind click to 'div.model img'
	$( ".model" ).bindHrefToImg();
	
	//rescale bg menu shadow
	$('#menuL0BG').updateHeight();
	
	//display overlay for pages with model
	if($("#model-thumbnails dl").length > 0 ){
		
		//show blue overlay
		$("body").displayOverlay();
		
		var myURL = parseURL(document.location.href);
		
		if( (myURL.segments[0] != 'highlights') && (myURL.segments[1] != 'highlights') ){
		  var path = $( "#model-thumbnails dl:first img").attr('longdesc').toLowerCase();
		
		  $( "#model-thumbnails dl" ).append('<div class="mask"> </div>');
		  $( this ).showLargeImg("#model-thumbnails dl .mask", path);
		
		  //add class to first thumbnail
		  $( "#model-thumbnails dl:first").addClass("active");		

		  $( "#model-thumbnails .mask" ).hover(function (e){
			//toggle gray overlay
			$( "#model-thumbnails dl").removeClass("active");
			$( this ).parent().addClass("active");	
			
			//show large image
			$( this ).showLargeImg( this, path );
		  });
		} else{
			$('#model-thumbnails .csc-textpic-imagecolumn').css('width', 650);
		}
	};
	
	//dealer list
	$("li.lvlOne").hover(function (e){
		//set
		if ( $(this).hasClass("open") ){
			//reset
			$(this).removeClass("open");
			$(this).find("span.expand").html("+");
			$(this).find("ul.foldout").css({
				"display": "none"
			});
		} else {
			//reset
			$("li.lvlOne").removeClass("open");
			$("li.lvlOne").find("span.expand").html("+");
			$("li.lvlOne").find("ul.foldout").css({
				"display": "none"
			});
			
			//set	
			$(this).find("ul.foldout").css({
				"display": "block"
			});
			$(this).find("span.expand").html("-");
			$(this).addClass("open");
		}
	});
	
	//reposition kleur stalen div
	if($('.kleurStalen img').length > 0){
		$('.kleurStalen').rebuildKleurStalen();
	}	
});
(function($) {
  $.fn.bindHrefToImg = function(){
	$(this).each(function (e){
		var el = this;
		var img = $('img', el);
		var aHref = $('a', el).attr("href");
		
		img.bind("click", function(e){
			window.location.href = "/" + aHref;
		});
	});
  };
})(jQuery);

(function($) {
  $.fn.updateHeight = function(){
	var h = $('.navigation li').height();
	h = ( $('.navigation li').length * h) + 54;
	if( h > 454){
		$('#menuL0BG .body').css('height', h);
	}
  };
})(jQuery);

(function($) {
  $.fn.rebuildKleurStalen = function(){
     el = $(this);
     $('#overlayContainer').append(el);
     
     var h = $(this).height();
     var t = 542 - h;
     h = h - 80;
 
     if($('img', el).length == 1){
     	h = 33;
     	t = 430;
     }
     el.css({'top':t, 'display': 'block' });
     $('.csc-textpic', el).css('height', h); 
  };
})(jQuery);

(function($) {
  $.fn.cursorHover = function(){
     $(this).hover(
	function () {
        	$(this).addClass('hover');
      	}, 
      	function () {
        	$(this).removeClass('hover');
      	}
     )
  };
})(jQuery);

(function($) {
  $.fn.updateLanguageSwitchURL = function(){
  
  	var myURL = parseURL(document.location.href);
	var endvalue = myURL.segments.length;
	
  	$('a', this).each(function(){
  		var el = $(this);
  		var i=0;
		
		var url = '';

		if ( el.attr('id') == 'en'){
			if (( myURL.segments[0] != 'en') && (myURL.segments[0] != '')){
    				for(i = 0; i <= endvalue - 1; i++){
       					url += "/" + myURL.segments[i] ;
    				}
   				url = '/en'+url;
   				el.attr('href', url);
   			}
   			
   			if( myURL.segments[0] == 'en' ){
   				el.addClass('selected');
   			}
		}
		if ( el.attr('id') == 'nl'){
			if( myURL.segments[0] == 'en'){
				for(i = 1; i <= endvalue - 1; i++){
       					url += "/" + myURL.segments[i] ;
    				}
   				el.attr('href', url);
			}
			if( myURL.segments[0] != 'en' ){
   				el.addClass('selected');
   			}
		}
  	});
  };
})(jQuery);

$.fn.showLargeImg = function( el, path ){

	//show large image
	var imgsrc = $( el ).parent().find("dt img").attr("src");
	
	var pos = imgsrc.lastIndexOf("/");
	var imagename = imgsrc.substr(pos + 1);
	
	var posDash = imagename.lastIndexOf("_");
	
	var lenDash = imagename.length - 7;
	
	if(posDash == lenDash){		
		//extension = imgsrc.substr(-3);
		imagename = imagename.substr(0,posDash) + ".jpg";
	} 
	
	if( $("#model-large img").length > 0){
		$("#model-large img").remove();
	}
	
	$("#model-large").append('<img src="fileadmin/agm/scooters/'+ path + imagename + '" alt="AGM Scooters"/>');

}

$.fn.closeOverlay = function(){
	var myURL = parseURL(document.location.href);

	var i=0;
	var endvalue = myURL.segments.length;
	var url = '';
	for(i = 0; i <= endvalue - 2; i++){
	   url += "/" + myURL.segments[i] ;
	}
	url += '.html';

	window.location.href = url;
}

$.fn.displayOverlay = function(){
	//load overlay
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		$("body","html").css({
			height: "100%", 
			width: "100%", 
			"display": "block"
		});
		//$("html").css("overflow","hidden");
	}
	
	if(document.getElementById("overlay") === null){
		//Afbeelding wordt geladen/Image is loading
		$( this ).append("<div id='overlay' class='loading'></div>");
		$("#overlayContainer").append("<div class='closeMe'> </div><div class='overlayCloseMe'> </div>");
		
		//set overlay to cover entire page
		$("#overlay").css({
			"height": $(document).height(),
			"width": $(document).width(),
			"display": "block"
		});		
	}
	
	//collapse container
	$("#container").css({
		"height": "0"
	});
	
	//add click event
	$("#overlay").click(function(e){		
		$("body").closeOverlay();
	});
	$(".overlayCloseMe").click(function(e){		
		$("body").closeOverlay();
	});
	//add click event
	$(".closeMe").click(function(e){		
		$("body").closeOverlay();
	});		
}

// This function creates a new anchor element and uses location
// properties (inherent) to get the desired URL data. Some String
// operations are used (to normalize results across browsers).
 
function parseURL(url) {
    var a =  document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':',''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function(){
            var ret = {},
                seg = a.search.replace(/^\?/,'').split('&'),
                len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
        hash: a.hash.replace('#',''),
        path: a.pathname.replace(/^([^\/])/,'/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
        segments: a.pathname.replace(/^\//,'').split('/')
    };
}
