/**
 * @project: Revor
 * @location: Wax Interactive - www.wax.be
 * @date: 16.09.2010
 * @author: Jan De Wilde
 * @description: Core logic
 */ 

if(typeof $.REVOR == "undefined") {
	$.REVOR = Object;
}

/**
 * Build scriptroot
 */
$.REVOR.setup = {
	scriptroot		:		null,
	
	construct		:		function(){
		$.REVOR.setup.buildscriptroot();
		$.REVOR.setup.externalLinks();
	},
	
	buildscriptroot	:		function(){
		$("script").each(function(){
			if(this.src.toString().match(/revor.js$/)){ ///revor.*?js$/
				$.REVOR.setup.scriptroot = this.src.toString().replace(/javascript\/revor.js$/,"");
				return false;
			}
		});
	},
	
	externalLinks	:		function(){
		$('a.external').bind('click',function(){
			window.open(this.href);
			return false;
		});
	}
}

/**
 * Navigation
 */
$.REVOR.nav = {
	construct		:		function(){
		$("#navBar ul").superfish({
			autoArrows: false,
			dropShadows: false,
			delay: 0,
			speed: 0,
			onInit: function(){
				$('#navBar ul li').each(function(){
					$(this).has('ul').addClass('sub');
				});
			}
		});
		if($.browser.mozilla || $.browser.safari){
			$('#mainmenu ul ul').css('background-position','left 5px');
		}
		if($.browser.msie && $.browser.version < 7){
			$('#mainmenu ul ul').css('background-position','left 6px');
		}
		
		$('div.album a,#dealerOverview a.lightbox').lightBox({
			overlayBgColor: '#000',
			overlayOpacity: 0.8,
			imageLoading: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-ico-loading.gif',
			imageBtnClose: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-btn-close.gif',
			imageBtnPrev: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-btn-next.gif',
			containerResizeSpeed: 350,
			txtImage: 'Afbeelding',
			txtOf: 'van'
		});
	}
}

/**
 * Cycle management
 */
$.REVOR.cycle = {
	construct		:		function(){
		$.REVOR.cycle.head();
		$.REVOR.cycle.promo();
		$.REVOR.cycle.store();
		$.REVOR.cycle.product();
	},
	
	head			:		function(){
		$('#navCycle').cycle({
			fx: 'fade',
			timeout: 5000,
			prev: '#hPrev',
			next: '#hNext'
		});
		$('#navUnit').hover(
			function() { $('#controls').css('display','block'); },
			function() { $('#controls').css('display','none'); }
		);
	},
	
	promo			:		function(){
		var pItemMaxH = 0;
		$('#promoCycle .pItem').each(function(){
			if($(this).height() > pItemMaxH) {
				pItemMaxH = $(this).height();
			}
		}).css('height',pItemMaxH);
		
		var left = $('#content div.left:first').outerHeight();
		var right = pItemMaxH + $('#promoCycle').prev().outerHeight();
		
		if(left > right){
			$('#promoCycle .pItem').css('height',left-$('#promoCycle').prev().outerHeight());
		}
		
		$('#promoCycle').cycle({
			fx: 'fade',
			timeout: 5000,
			pager: '#promoNav'
		});
	},
	
	store			:		function(){

		$('#afbeeldingenCycle').cycle({
			fx: 'fade',
			timeout: 5000,
			pager: '#afbeeldingenNav'
		});
		$('#afbeeldingenCycle a').lightBox({
			overlayBgColor: '#000',
			overlayOpacity: 0.8,
			imageLoading: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-ico-loading.gif',
			imageBtnClose: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-btn-close.gif',
			imageBtnPrev: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-btn-next.gif',
			containerResizeSpeed: 350,
			txtImage: 'Afbeelding',
			txtOf: 'van'
		});
	},
	
	scaleProductImg	:		function(){
		$('#productCycle a img').each(function(){
			var width = $(this).width();
			var height = $(this).height();
			
			var spaceRatio = 480/300;
			var imgRatio = width/height;
			if(!isNaN(imgRatio)){
				if(imgRatio <= spaceRatio){
					$(this).height(300);
				} else {
					$(this).width(480);
				}
			}
			$(this).css('visibility','visible');
		});
	},
	
	product			:		function(){
		$.REVOR.cycle.scaleProductImg();
		$('#productCycle').cycle({
			fx: 'fade',
			timeout: 5000,
			prev: '#pPrev',
			next: '#pNext',
			pager:  '#productNav', 
			pagerAnchorBuilder: function(idx, slide) {
				return '<a href="#"><img src="' + slide.rel + '" /></a>'; 
			},
			after: function(){
				$.REVOR.cycle.scaleProductImg();
			},
			before: function(){
				$.REVOR.cycle.scaleProductImg();
			}
		});
		$('#productCycleWrapper').hover(
			function() { $('#controls').css('display','block'); },
			function() { $('#controls').css('display','none'); }
		);
		$('#productCycle a').lightBox({
			overlayBgColor: '#000',
			overlayOpacity: 0.8,
			imageLoading: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-ico-loading.gif',
			imageBtnClose: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-btn-close.gif',
			imageBtnPrev: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: $.REVOR.setup.scriptroot+'images/lightbox/lightbox-btn-next.gif',
			containerResizeSpeed: 350,
			txtImage: 'Afbeelding',
			txtOf: 'van'
		});
		$('#pEnlarge').bind('click',function(){
			$('#productCycle a:visible').click();
		});
	}
}

$.REVOR.searchEngine = {
	construct		:		function(){
		var searchInput = $('#searchInput');
		var origVal = searchInput.val();
		searchInput.bind({
			focus:	function(){
				if(searchInput.val() == origVal) {
					searchInput.val('');
				}
			},
			blur:	function(){
				if($.trim(searchInput.val()) == '') {
					searchInput.val(origVal);
				}
			}
		});
	}
}

$.REVOR.merken = {
	construct		:		function(){
		$.REVOR.merken.tooltip();
	},
	
	tooltip			:		function(){
		$('#merken a').bind({
			mouseenter: function(){
				var hrefCoords = $(this).offset();
				
				$('body')
				.append('<div id="tooltip"><div id="tcontent">'+$(this).attr('rel')+'</div><div id="tbeak"></div></div>')
				$('#tooltip').css({
					left: hrefCoords.left - (($('#tooltip').outerWidth()-$(this).outerWidth()) / 2),
					top: hrefCoords.top - $('#tooltip').outerHeight() - 5
				});

				$('img.color',this).css('z-index','10');
			},
			mouseleave: function(){
				$('img.color',this).css('z-index','1');
				$('#tooltip').remove();
			}
		});
	}
}

$.REVOR.dealers = {
	construct : function(){
		var _this = this;
		_this.visibleState();
		
		$('#showFilter').bind('click',function(){
			$('#checkboxContainer').toggle();
		});

		$('#checkboxContainer').bind('mouseleave',function(){
			$('#checkboxContainer').toggle();
			_this.visibleState();
		});
		
		$('#checkboxContainer div.checkbox').bind('click',function(){
			if($(this).hasClass('active')){
				$(this).removeClass('active');
			} else {
				$(this).addClass('active');
			}
			return false;
		});
		
		var visibleTimeout = null;
		$('#postcode').bind('keyup',function(){
			if(visibleTimeout != null){
				clearTimeout(visibleTimeout);
			}
			visibleTimeout = setTimeout(function(){ _this.visibleState(); },500);
		});
		
		$('#listFilter').bind("submit",function(){
			return false;
		});
	},

	filterPostalCode : function(){
		var postalCode = $('#postcode').val();
		if(parseInt(postalCode)){
			if(postalCode.length >= 2){
				$('div.dealerPart:visible').each(function(){
					var currDealer = this;
					var postCodeDealer = $('.pPostalCode',this).text();
					if(postCodeDealer.substr(0,2) !== postalCode.substr(0,2)){
						$(currDealer).hide();
					}
				});
			}
		}
	},

	visibleState : function() {
		var _this = this;
		
		var arrBrands = new Array();
		if($('#checkboxContainer').length){
			$('#checkboxContainer div.checkbox').each(function(){
				if($(this).hasClass('active')){
					arrBrands.push($('label',this).text());
				}
			});
			$("#sMerken").text(arrBrands.join(', '));
		}

		$("div.dealerPart").show();
		if(arrBrands.length > 0){
			$('div.dealerPart').each(function(){
				var dealerPart = this;
				var dealerPartBrands = $('span.dealerPartMerken',dealerPart).text().split(', ');
				var hideFlag = false;
				$(arrBrands).each(function(bIndex,bValue){
					if($.inArray(bValue,dealerPartBrands) == -1){
						hideFlag = true;
					}
				});
				if($(dealerPart).hasClass('store')) {
					hideFlag = false;
				}
				if(hideFlag){
					$(dealerPart).hide();
				}
			});
			
		}

		_this.filterPostalCode();

		$('.scroll-pane').jScrollPane({
			showArrows: true
		});
	}
}

$(document).ready(function(){
	$.REVOR.setup.construct();
	$.REVOR.nav.construct();
	$.REVOR.cycle.construct();
	$.REVOR.searchEngine.construct();
	$.REVOR.merken.construct();
	$.REVOR.dealers.construct();
});
