$(document).ready(function(){
	
	// Hide Page if it excists
	function resetpage() {
				
	}
	
	var base_url = 'http://www.bellerose.be/'
	
	function closeLoad() {
	
		$('#loadr, #load').fadeTo(300, 1).fadeTo(300, 0, function(){ $('#loadr, #load').fadeTo(0,1).hide(); });
	
	}
	
	function setDisch() {
		$('.menu_active').each(function(){
			if ($(this).hasClass('menu_disch')) {
				var currentSrc = $(this).find('img').attr('src').replace('.png', '');
				$(this).find('img').attr('src', ''+currentSrc+'_hover.png');
			}
		});
	}
	
	setDisch();
	
	$('.menu_active').hover(function(){
	
		if ($(this).hasClass('menu_disch')) {
		
		} else {

			var currentSrc = $(this).find('img').attr('src').replace('.png', '');
			$(this).find('img').attr('src', ''+currentSrc+'_hover.png');
	
		}
	
	}, function(){
	
		if ($(this).hasClass('menu_disch')) {
		
		} else {
	
			var currentSrc = $(this).find('img').attr('src').replace('_hover', '');
			$(this).find('img').attr('src', ''+currentSrc+'');	
	
		}
		
	});
	
	var belleroseMenu = false;
	
	function showMenu() {
		$('#bellerose_menu, .menu_part').show();
		belleroseMenu = true;
	}
	
	function hideMenu() {
		$('#bellerose_menu, .menu_part').hide();
		belleroseMenu = false;
	}
	
	hideMenu();
	
	$('#bellerose_logo').click(function(){
	
		if (belleroseMenu == false) {
			showMenu();
			bellroseAreaClose();
			bellrosePageClose();
		} else {
			hideMenu();
		}
		
	});
	
	$('#menu_closeme').click(function(){
		hideMenu();
	});
	
	
	var bellerose_ease = 'linear';
	
		
		$("#menu_about").live('click', function(){
		
				var permfix = 2;
				
				//$(this).addClass('menu_disch');
				//var currentSrc = $(this).find('img').attr('src').replace('.png', '');
				//$(this).find('img').attr('src', ''+currentSrc+'_hover.png');
				
				bellroseAreaClose();
				
				$("#bellerose_page_inner").load(""+base_url+"?page_id=" + permfix + ' #page_holder', {}, function(){
					placeAbout();
					$("#bellerose_page").show();
					hideMenu();
						
   				});
			
		});	
		
		$("#menu_contact").live('click', function(){
		
				var permfix = 16;
				
				bellroseAreaClose();
				
				$("#bellerose_page_inner").load(""+base_url+"?page_id=" + permfix + ' #page_holder', {}, function(){
					placeAbout();
					$("#bellerose_page").show();
					hideMenu();
						
   				});
			
		});	
		
		$("#menu_subscribe").live('click', function(){
		
				var permfix = 18;
				
				bellroseAreaClose();
				
				$("#bellerose_page_inner").load(""+base_url+"?page_id=" + permfix + ' #page_holder', {}, function(){
					placeAbout();
					$("#bellerose_page").show();
					hideMenu();
						
   				});
			
		});	
		
	
		var category = $.cookie('Category');
		var home = $.cookie('Home');
		
		if (category == 6) {
			$('.backToStoresfull:eq(0)').hide();
		}
	
		if (home == 'Yes') {
			
			$('#bellerose_logo').trigger('click');
   				
		}
		
		var area = true;
		var imagesLoading = 0;
		
		function setDescriptionStatus() {
		
			if (area == true) {
			
				if (category == 3 || category == 4 || category == 5) {
					//$('#text_trigger').hide();
					//bellroseAreaClose();
					//bellroseAreaOpen();
					
				} else if (category == 6 || category == 7 || category == 8) {
					bellroseAreaOpen();
				}
			
			} else {

			
			}		
		
		}
		
		setDescriptionStatus();
		
		if (home == 'Yes') {
			showMenu();
		}
		
		
		$('#ContainerWrapper img').attr('title','');
	
	
		/*** Set images width & height ***/
    			
    	var imageWidth = 1600;
		var imageHeight = 1000;
		var imageRatio = 0;
		
    	var videoWidth = 1280;
		var videoHeight = 720;
		var videoRatio = 0;
		
		// Variables
		var viewportWidth = $(window).width();
		var viewportHeight = $(window).height();
		var subnavpos = viewportHeight -60;
		var totalimgcount = $('.key').size();
		var slidecunt = 0;
		
		//$('#Container').width(viewportWidth).height(viewportHeight);
		
		/*** Delete all image Title info ***/
		$('img, .dropr a, #info a').attr("title", '');
		
    	/*** Function that does the magic ***/
		function resizeImage() {
			
			$('#ContainerWrapper').width((viewportWidth*totalimgcount)+100);
			
			// Loop Images Positions
			$(".media").each(
			 
				function(intIndex){
				
					if ($(this).hasClass('image')) {
					
						var navWidth = $(window).width();
						var navHeight = $(window).height();
						
						var navRatio = navWidth / navHeight;
						imageRatio = imageWidth / imageHeight;
						if (navRatio > imageRatio) {
							var newHeight = (navWidth / imageWidth) * imageHeight;
							var newWidth = navWidth;
						} else {
							var newHeight = navHeight;
							var newWidth = (navHeight / imageHeight) * imageWidth;
						}
						
						
						newTop = 0 - ((newHeight - navHeight) / 2);
						newLeft =  0 - ((newWidth - navWidth) / 2);
						
			 			var selector = '.image:eq('+intIndex+') img';
			 			var selectorHolder = '.image:eq('+intIndex+')';
			 			var selectorDesc = '.description:eq('+intIndex+')';
			 			
			 			
			 			$(selectorHolder).css({'height': ''+navHeight+'px', 'width': ''+navWidth+'px', 'left': ''+navWidth*intIndex+'px'});
						$(selector).css({'height': ''+newHeight+'px', 'width': ''+newWidth+'px'});
						$(selectorHolder).scrollTop((newHeight - viewportHeight)/2);
						$(selectorHolder).scrollLeft((newWidth - viewportWidth)/2);
						
						
						var getDescHeight = $(selectorDesc).height();
						var myDescHeightFormula = - ((getDescHeight/2) - ((newHeight - viewportHeight)/2));
						
						
						var getDescWidth = 512;
						var myDescWidthFormula = - ((getDescWidth/2) - ((newWidth - viewportWidth)/2));
						
						
						$(selectorDesc).css({'margin-top':''+myDescHeightFormula+'px', 'margin-left':''+myDescWidthFormula+'px'});
						
						
					} else if ($(this).hasClass('video')) {
					
						var navWidth = $(window).width();
						var navHeight = $(window).height();
						
						var navRatio = navWidth / navHeight;
						videoRatio = videoWidth / videoHeight;
						if (navRatio > videoRatio) {
							var newHeight = (navWidth / videoWidth) * videoHeight;
							var newWidth = navWidth;
						} else {
							var newHeight = navHeight;
							var newWidth = (navHeight / videoHeight) * videoWidth;
						}	
						newTop = 0 - ((newHeight - navHeight) / 2);
						newLeft =  0 - ((newWidth - navWidth) / 2);
						
							
			 			
			 			//var selector = '.image:eq('+intIndex+') img';
			 			var selectorVideo = '.media:eq('+intIndex+') video';
			 			var selectorHolder = '.video:eq('+intIndex+')';
			 			$(selectorHolder).css({'height': ''+navHeight+'px', 'width': ''+navWidth+'px', 'left': ''+navWidth*intIndex+'px'});
						
						//$(selector).css({height: newHeight, width: newWidth});
						$(selectorVideo).css({'height': ''+(newHeight)+'px', 'width': ''+(newWidth)+'px'});
						
						$(selectorHolder).scrollTop((newHeight - viewportHeight)/2);
						$(selectorHolder).scrollLeft((newWidth - viewportWidth)/2);
					
					}
					

    	        
				}
			 
			);
			

			$('#Container').scrollTo('.key:eq('+slidecunt+')', 0, {easing: bellerose_ease, offset: 0, axis: 'x', onAfter:function(){ 
				// After the scroll	
				//alert(slidecunt);						
				}			
			});	
						
		}
		
		var speed = 400;
		//var totalimgcount = $('#Container .media').length;
		
		function moveTextTriggerRight() {
		
				$('#text_trigger').animate({
				
					right: '0px'
				
				}, 0, function() {
				
    				// Animation complete.
    				
  				});
		
		}
		
		function moveTextTriggerLeft() {
		
				$('#text_trigger').animate({
				
					right: '34px'
				
				}, 0, function() {
				
    				// Animation complete.
    				
  				});
		
		}
		
		function checkAvail() {
		
			var testo1 = $('#countme').html();
			var testo2 = $('#totalme').html();
		
			if (slidecunt == 0) {
			
				$('.prev_slidr').hide();
				$('.next_slidr').show();
				moveTextTriggerLeft();
			
			} else if (testo1 == testo2) {
			
				if (category == 3 || category == 4 || category == 5 || category == 6) {
				
					$('.next_slidr, .prev_slidr').show();
				
				} else {
				
					$('.next_slidr').hide();
					$('.prev_slidr').show();
				
					moveTextTriggerRight();
				
				}
			
			} else {
				$('.next_slidr, .prev_slidr').show();
				moveTextTriggerLeft();
			}
		
		}
		
		checkAvail();


		function moveFirst() {
		
			if (slidecunt > 0) {
			
				ifMoveRight = 1;
			
				//$('#ContainerWrapper .wearevideo:eq('+slidecunt+')').trigger('pause');
				slidecunt = 0;
				
				placeAbout();
				//bellrosePageClose();
				
				$('#Container').scrollTo('.key:eq('+slidecunt+')', speed, {easing: bellerose_ease, offset: 0, axis: 'x', onAfter:function(){ 
					// After the scroll	
					//alert(slidecunt);	
					
					setCounter();
					ifMoveRight = 0;
					checkAvail();
					//$('#ContainerWrapper .wearevideo:eq('+slidecunt+')').trigger('play');
										
					}			
				});	
			
			} else {
			
				//alert('start');
			
			}
		
		}

		
		function moveLeft() {
		
			if (slidecunt > 0) {
			
				$('#ContainerWrapper .wearevideo:eq('+slidecunt+')').trigger('pause');
				slidecunt--;
				
				placeAbout();
				//bellrosePageClose();
				
				$('#Container').scrollTo('.key:eq('+slidecunt+')', speed, {easing: bellerose_ease, offset: 0, axis: 'x', onAfter:function(){ 
					// After the scroll	
					//alert(slidecunt);	
					
					setCounter();
					
					checkAvail();
					//$('#ContainerWrapper .wearevideo:eq('+slidecunt+')').trigger('play');
										
					}			
				});	
			
			} else {
			
				//alert('start');
			
			}
		
		}
		
		//alert(totalimgcount);
		
		var ifMoveRight = 0;
		
		function moveRight() {
		
		if (ifMoveRight == 0) {

		
			if (slidecunt > -1 && slidecunt < (totalimgcount-1) ) {
			
				ifMoveRight = 1;
			
				$('#ContainerWrapper .wearevideo:eq('+slidecunt+')').trigger('pause');
				slidecunt++;
				setDescriptionStatus();
				placeAbout();
				//bellrosePageClose();
				
				$('#Container').scrollTo('.key:eq('+slidecunt+')', speed, {easing: bellerose_ease, offset: 0, axis: 'x', onAfter:function(){ 
					// After the scroll	
					//alert(slidecunt);	
					
					setCounter();
					checkAvail();
					//$('#ContainerWrapper .wearevideo:eq('+slidecunt+')').trigger('play');
					
					ifMoveRight = 0;
										
					}			
				});	
			
			} else {
				
					var nextprev = $('#bellerose_nextprev a').length;
					var content = $('#bellerose_nextprev a').html();
					
					if (content == 'Back') {
					
						ifMoveRight = 1;
					
						var nextHref = $('#bellerose_nextprev a').attr('href');
						
						$('#load').show();

						$.ajax({
						  url: ''+nextHref+'',
						  success: function(data) {
						  
						  	var $response = $(data);
						  	var oneval = $response.find('#ContainerWrapper').html();
						  	var twoval = $response.find('#bellerose_area_inner').html();
						  	
						  	$("#bellerose_area_inner").empty().append(twoval);
						  	$('#bellerose_nextprev').remove();
						  	
							
        					$(data).find('#ContainerWrapper img').each(function(intIndex) {
        					
        						var imgPath = $(this).attr('src');
        					
        						$('<img/>')[intIndex].src = ''+imgPath+'';
							
						    	$("#ContainerWrapper").append(oneval);
						    	$('#load').hide();
						    	updateTotalImgCount();
						    	resizeImage();
						    	ifMoveRight = 0;
						    	moveRight();
						   
						   		
						    	
						    	preload();
						    	
        					});
							          
						    
						  }
						});
					
					} else {
					
						if (category == 3 || category == 4 || category == 5 || category == 6 || category == 7 || home == 'Yes') {
						
							moveFirst();
						
						}
					
					}
				

				
				
			}
			
		} // end if moveRight
		
		}
		
		
		$('.backToStores').click(function(){
			moveFirst();
		});
		
		$('.storeMove').click(function(){
			var whatStore = $(this).attr('id');
			slidecunt = (whatStore-1);
			placeAll();
			moveRight();
		});
		
		//$("#ContainerWrapper .wearevideo").trigger('play');
		
		function updateTotalImgCount() {
		
			totalimgcount = $('.key').size();
			setCounter();
		
		}
		
		function setCounter() {
			
			if (category == 7 || category == 8) {
			
				//$('#countme').html(''+(slidecunt+1)+'');
				//Cufon.replace('#menu_counter');
			
			} else {
			
				$('#countme').html(''+(slidecunt+1)+'');
				Cufon.replace('#menu_counter');
			
			}
			
		}
		
		var totalme = 0;
		var thistotalme = 0;
		
		$('#totalme span.testcount').each(function(){
			thistotalme = parseInt($(this).attr('id'));
			totalme = totalme + thistotalme;
			
			$('#totalme').html(''+totalme+'');
			Cufon.replace('#menu_counter');
		});
		
		$('.next_slidr').click(function(){
			moveRight();
		});
		
		$('.prev_slidr').click(function(){
			moveLeft();
		});
		
		
		shortcut.add("Left",function() {
		
			moveLeft();
		});

		shortcut.add("Right",function() {
			if (ifMoveRight == 0) {
				moveRight();
			}
		});
		


		
    	/*** Resize Instant ***/
		resizeImage();
		
		
		function bellrosePageClose() {
			$('#bellerose_page').hide();
			//parent.location.hash = '';
		}
		
		$('#bellerose_page_close').click(function(){
			bellrosePageClose();
		});
		
		
		function bellroseAreaClose() {
			$('.description').hide();
			$('#text_trigger').show();
			area = false;
		}
		
		function bellroseAreaOpen() {
			$('.description').show();
			$('#text_trigger').hide();
			hideMenu();
			bellrosePageClose();
			area = true;
		}
		
		$('#bellerose_area_close').click(function(){
			bellroseAreaClose();
		});
		
		$('.description_close').live('click', function(){
			bellroseAreaClose();
		});
		
		$('#text_trigger').click(function(){
		
			if (area == true) {
				bellroseAreaClose();
			} else {
				bellroseAreaOpen();
			}
		
		});
		
		
		function placeAbout() {
		
			
			var pageHeight = $('#bellerose_page').height();
			$('#bellerose_page').css({'margin-top':'-'+pageHeight/2+'px'});
			var pageHeight = $('#bellerose_area').height();
			$('#bellerose_area').css({'margin-top':'-'+pageHeight/2+'px'});
			
			/*
			var descHeight = $('.description:eq('+slidecunt+')').height();
			$('.description:eq('+slidecunt+')').css({'margin-top':'-'+descHeight/2+'px'});
			*/
			
		}
		
		placeAbout();
		
		var descHeightIsh = 0;
		
		function placeAll() {
			
			/*
			$('.description').each(function(){
				descHeightIsh = $(this).height();
				$(this).css({'margin-top':'-'+((descHeightIsh/2))+'px'});
			});
			*/
		}
		
		function chechBoxHeight() {
		
			/*
			var pageHeight = $('#bellerose_page').height();
			viewportHeight = $(window).height();
			
			if ((viewportHeight-60) < pageHeight) {
			
				$('#bellerose_page').height((viewportHeight-60));
			
			}
			*/
			
		
		}
		
		chechBoxHeight();
		
		$(window).resize(function(){
		
			viewportWidth = $(window).width();
			viewportHeight = $(window).height();
		
			resizeImage();
			placeAbout();
			
		});
		
		/*
		$(".media").touchwipe({
		     wipeLeft: function() { moveRight(); },
		     wipeRight: function() { moveLeft(); },
		     min_move_x: 20,
		     preventDefaultEvents: true
		});
				
		$(".media").unbind();
		*/
		
		/*
		function touch() {
		
    		$(".media").bind("touchend", function (event2) {
    		    var e2 = event2.originalEvent;
    		    console.log(e2.targetTouches[0].pageX);
    		    alert(e2.targetTouches[0].pageX);
    		});
    				
    		$(".media").bind("touchstart", function (event) {
    		    var e = event.originalEvent;
    		    console.log(e.targetTouches[0].pageX);
    		    alert(e.targetTouches[0].pageX);
    		});
		
		}
		
		touch();
		
		function unbindTouch() {
			$(".media").unbind("touchmove");
		}
		*/
				
		function preload() {
			if (home == 'Yes') {
			
			} else {
				preloadNext();
			}
		}
		
		function preloadNext() {
			
			$('#preload').fadeTo(1000, 0, function(){
		
			var locateMe = $('#bellerose_nextprev a').attr('href');
			$('#preload').load(locateMe+' #ContainerWrapper img', function() { 
				preloadPrev(); 
			});

			
			});
				
		}
		
		function preloadPrev() {
		
			//var locateMe = $('#left_arrow_big a').attr('href');
			//$('#preload').load(locateMe+' #ContainerWrapper img', function() { 
				// Finished Preloading
			//});
		
		}
		
		$(window).load(function(){

				closeLoad();				
    			preload();

		});
	
	
});
