// JavaScript Document
var finish_addresses_first = false;
function handleFlowResult(target, output){
	switch(target){
		case 'city_selecter':
			$(target).innerHTML 		= output;
			$(target).addEvent('domready', function() {
				if($('search_optioncity')){
					loadDropDownActions('search_optioncity','city');		
				}
				basicShow(target, true, true);
				allow_click				= true;
			});
		break;
		case 'storelocator_result':
			$(target).innerHTML 		= output;
			$(target).addEvent('domready', function() {
													
													
													
													
				basicShow(target, true, true);
				allow_click				= true;
			});
		break;
		case 'slides_container_home_info':
			$(target).innerHTML 		= output;
			$(target).addEvent('domready', function() {
				basicShow(target, true, true);
				allow_click				= true;
			});
		break;
	}
}

function initHomeVideo(){
	if($('home_video')){
	var obj = new Swiff(root_domain+'home/video.swf', {
		id: 'home_video',
		container: $('home_video'),
		width: 600,
		height: 520,
		params: {
			wmode: 'transparant',
			bgcolor: '#ffffff'
		},
		vars: {
			item_id: 0
		}
	})
	}
}


var active_slide_home			= 0;
var count_slides_home			= 0;
var slide_urls_home				= new Array();
var slide_links_home			= new Array();
var slide_video_image			= new Array();
var slide_ids					= new Array();
var fx_slide_info_home;
var proportions_original_home;
var allow_refresh_home			= true;
var allow_auto_refresh_home		= true;
var refresher_home;
var homelink					= '';

var slide_left_btn;
var slide_right_btn;
var slide_slide_bg;

function initHomeSlides(){
	active_slide_home			= -1;
	if($('home_slides')){
		//alert(1);
		if($('valuesURL_2')){
			if($('valuesURL_2').value != ''){
				var url_slides			= $('valuesURL_2').value;
				slide_urls_home			= url_slides.split('#');
				var link_slides			= $('valuesLinks_2').value;
				slide_links_home		= link_slides.split('#');
				var video_image			= $('valuesVideoImage_2').value;
				slide_video_image		= video_image.split('#');
				var slide_ids_value		= $('valuesIDs_2').value;
				slide_ids				= slide_ids_value.split('#');
				
				
				slide_left_btn 			= new Fx.Morph($('slides_container_home_btn_prev'), {duration: 450, transition: Fx.Transitions.Cubic.easeOut});
				slide_right_btn 		= new Fx.Morph($('slides_container_home_btn_next'), {duration: 450, transition: Fx.Transitions.Cubic.easeOut});
				slide_slide_bg 			= new Fx.Morph($('slides_container_home'), {duration: 450, transition: Fx.Transitions.Cubic.easeOut});
				
				
				
				count_slides_home		= $('aantal_thumbs_2').value;
				setSlideAutoHome();
				
				if($('slides_container_home_border')){
					$('slides_container_home_border').addEvent('click', function(event){ 
						if(homelink!=''){
							window.location	= homelink;	
						}
					});
				}
			}
		}
	}
}
function navSlide(direction){
	$clear(refresher_home)
	var nr 			= active_slide_home
	switch(direction){
		case 1:
			if(nr < (count_slides_home-1)){
				nr++;
			}else{
				nr			= 0;
			}
		break;
		case -1:
			if(nr > 0){
				nr--
			}else{
				nr			= count_slides_home-1;
			}
		break;
	}
	allow_refresh_home = true;
	setSlideHome(nr);
}
function setSlideManualHome(){
	$clear(refresher_home)
	var nr 			= active_slide_home
	if(nr < (count_slide_home-1)){
		nr++
	}else{
		nr			= 0;
	}
	setSlideHome(nr);
}

function setSlideAutoHome(){
	if(allow_auto_refresh_home){
		var nr 			= active_slide_home
		if(nr < (count_slides_home-1)){
			nr++
		}else{
			nr			= 0;
		}
		setSlideHome(nr);
	}else{
		$clear(refresher_home)
	}
}

function setSlideHome(nr){
	if(nr!=active_slide_home && allow_refresh_home == true){
		active_slide_home	= nr;
		allow_refresh_home	= false;
		
		homelink			= 'http://' + slide_links_home[nr];
		
		var video_image		= slide_video_image[nr];

		$('home_video_container').empty();
		
		if(video_image == 1 || video_image == ''){
			loadNewSlideHome(slide_urls_home[nr]);
		}else{
			loadNewSlideHomeVideo(slide_urls_home[nr]);
		}
		
		loadNshow(root_domain + 'lookbook/lookbook-detail.php?lookbook='+slide_ids[nr],'slides_container_home_info', 'get', false);
		
	}
}

function loadNewSlideHomeVideo(new_url){
	allow_click		= false;
	var obj = new Swiff(root_domain+'home/video.swf', {
		id: 'home_video_container',
		container: $('home_video_container'),
		width: 748,
		height: 420,
		params: {
			wmode: 'window',
			bgcolor: '#fff',
			allowFullScreen: "true"
		},
		vars: {
			url: new_url
		}
	})
	
	allow_click		= true;
	video_id		= 0;
	
	$('slide_image_home').setStyle('opacity',0);
				
	
	allow_refresh_home	= false;
	$clear(refresher_home);
//	refresher_home		= setSlideAutoHome.periodical(15000);
}

var current_img_fx_out_home;
var current_img_fx_in_home;
var this_breedte_image_home;
var this_hoogte_image_home;
				
function loadNewSlideHome(new_url){
	new Asset.images(new_url, {
		onProgress: function(i) {
			this_breedte_image_home 	= this.width;
			this_hoogte_image_home 		= this.height;
			this.setStyles({
				'opacity': 0
			});
		},
		onComplete: function() {
			
			current_img_fx_out_home = new Fx.Morph($('slide_image_home'), {duration: 450, transition: Fx.Transitions.Cubic.easeOut,onComplete: function() {
				
				current_img_fx_in_home = new Fx.Morph($('slide_image_home'), {duration: 450, transition: Fx.Transitions.Cubic.easeOut,onComplete: function() {
					allow_refresh_home	= true;
					$clear(refresher_home);
					refresher_home		= setSlideAutoHome.periodical(15000);
				}});
				
				current_img_fx_in_home.set({'opacity': 0});
				$('slide_image_home').src = new_url;
				//current_img_fx_in_home.set({'left': (600 - this_breedte_image_home)/2 });
				current_img_fx_in_home.set({'top': 0 });
				
				
				
				slide_left_btn.cancel;
				slide_left_btn.start({'left': (600 - this_breedte_image_home)/2});
				slide_right_btn.cancel;
				slide_right_btn.start({'left': 600 - (600 - this_breedte_image_home)/2 - 42});
				slide_slide_bg.cancel;
				slide_slide_bg.start({'height': this_hoogte_image_home , 'width': this_breedte_image_home, 'left': (600 - this_breedte_image_home)/2});
				
				current_img_fx_in_home.cancel();
				current_img_fx_in_home.start({'opacity': 1});
				//alert(homelink);
				
			}});
				
			current_img_fx_out_home.cancel();
			current_img_fx_out_home.start({'opacity': 0});
			
		}
	});
		
}



function initStorelocator(){
	if($('search_optioncountry')){
		loadDropDownActions('search_optioncountry','country');		
	}
	if($('search_optioncity')){
		loadDropDownActions('search_optioncity','city');		
	}
	if($('list_100')){
		initListingStores();	
	}
}


var active_slide			= 0;
var count_slides			= 0;
var slide_urls				= new Array();
var slide_ttls				= new Array();
var fx_slide_info;
var proportions_original	= 0;
var allow_refresh			= true;
var allow_auto_refresh		= true;
var refresher;

function setSlideBase(){
	active_slide		= -1;
	if($('valuesURL_x')){
		if($('valuesURL_x').value != ''){
			var url_slides		= $('valuesURL_x').value;
			slide_urls			= url_slides.split('#');
			var title_slides	= $('valuesTitles_x').value;
			slide_ttls			= title_slides.split('#');
			count_slides		= $('aantal_thumbs_x').value;
			setSlideAuto();
		}
	}
}

function setSlideManual(){
	$clear(refresher)
	var nr 			= active_slide
	if(nr < (count_slides-1)){
		nr++
	}else{
		nr			= 0;
	}
	setSlide(nr);
}

function setSlideAuto(){
	if(allow_auto_refresh){
		var nr 			= active_slide
		if(nr < (count_slides-1)){
			nr++
		}else{
			nr			= 0;
		}
		setSlide(nr);
	}else{
		$clear(refresher)
	}
}

function setSlide(nr){
	if(nr!=active_slide && allow_refresh == true){
		active_slide	= nr;
		allow_refresh	= false;
		loadNewSlide(root_domain + slide_urls[nr]);
	}
}

var current_img_fx_out;
var current_img_fx_in;
var this_breedte_image;
var this_hoogte_image;
var proportions_site;
				
function loadNewSlide(new_url){
	new Asset.images(new_url, {
		onProgress: function(i) {
			this_breedte_image 	= this.width;
			this_hoogte_image 	= this.height;
			this.setStyles({
				'opacity': 0
			});
		},
		onComplete: function() {
			
			proportions_original 	= this_hoogte_image/this_breedte_image;
			
			current_img_fx_out = new Fx.Morph($('slide_image'), {duration: 600, transition: Fx.Transitions.Cubic.easeOut,onComplete: function() {
				
				current_img_fx_in = new Fx.Morph($('slide_image'), {duration: 400, transition: Fx.Transitions.Cubic.easeOut,onComplete: function() {
					allow_refresh	= true;
					$clear(refresher);
					refresher		= setSlideAuto.periodical(55000);
				}});
				
				current_img_fx_in.set({'opacity': 0});
				$('slide_image').src = new_url;
				var new_width;
				var new_height;
				
				if(proportions_original >= proportions_site){
					new_height		= innerWidth * proportions_original
					if(new_height < innerHeight){
						new_height 		=  innerHeight;
						new_width 		=  new_height * proportions_original;
					}
					$('slide_image').setStyles({'width' : new_width, 'height' : new_height});
					$('slides_container').setStyles({'top' : 0, 'left' : 0});
					//$('slide_image').setStyle('width' ,innerWidth);
				}else{
					var new_width		= innerHeight / proportions_original
					if(new_width < innerWidth){
						new_width 		=  innerWidth;
						new_height 		=  innerWidth / proportions_original;
					}
					$('slide_image').setStyles({'width' : new_width, 'height' : innerHeight});
					$('slides_container').setStyles({'left' : 0 , 'top' : 0});
				}
				
				
				current_img_fx_in.cancel();
				current_img_fx_in.start({'opacity': 0.3});
				
				
			}});
				
			current_img_fx_out.cancel();
			current_img_fx_out.start({'opacity': 0});
			
		}
	});
		
}

var active_prov			= 0;
function initListingStores(){
	var fx_locations	= new Array;
	var loc_array 		= new Array;
	
	var barList			= $$('.listingHeaderBasic');
	barList.each(function(element) {
		var current 		= parseInt(element.getProperty('id').substr(5,3));
		loc_array[current]	= new Array;
	});	
	
	var locList			= $$('.location');
	locList.each(function(element) {
		var this_id					= element.getProperty('id');				  
		var current_prov  			= parseInt(this_id.substr(8,3));
			loc_array[current_prov].push(this_id);
		fx_locations[this_id]		= MorpheItems(element, 500, Fx.Transitions.Cubic.easeOut);
		if(active_prov==0){
		fx_locations[this_id].set({'opacity':1});
		}
	});
	
	barList.each(function(element) {
		element.addEvent('click', function(event){
			if(active_prov != 0){
		//		$('list_'+active_prov).setStyle('background-image', 'url('+root_domain+'img/'+root_short+'/assets/frame/listing-footer-list.png');
				for(i=0;i< loc_array[active_prov].length; i++){
					fx_locations[loc_array[active_prov][i]].cancel();
					fx_locations[loc_array[active_prov][i]].start({'height':0,'opacity':1});
				}
			}
			var current 		= parseInt(element.getProperty('id').substr(5,3));
		//element.setStyle('background-image', 'url('+root_domain+'img/'+root_short+'/assets/frame/listing-footer-list-open.png');
					
			active_prov			= current;
			for(i=0;i< loc_array[current].length; i++){
				fx_locations[loc_array[current][i]].cancel();
				if(active_prov != 100){
				fx_locations[loc_array[current][i]].start({'height':130,'opacity':1});
				}else{
				fx_locations[loc_array[current][i]].start({'height':55,'opacity':1});
				}
			}
		});
	});		
}

var mapActive = false;
function openMap(co1,co2, name_string){
	var position = new google.maps.LatLng(co1,co2);
	var myOptions = {
	  zoom: 15,
	  center: position,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	if(mapActive == true){
		if($('mapdiv')){
			$('mapdiv').destroy();
		}
		mapActive = false
	}
	
	var myBGDiv1 = new Element('div', {
		'opacity': 0.8,
		'id': 'bg_mapdiv1',
		'class': 'bg_mapdiv'
	});
	var myBGDiv2 = new Element('div', {
		'opacity': 1,
		'id': 'bg_mapdiv2',
		'class': 'bg_mapdiv'
	});
	var myDiv = new Element('div', {
		'opacity': 1,
		'id': 'mapdiv',
		'class': 'mapdiv'
	});
	var myCloseDiv = new Element('div', {
		'opacity': 1,
		'id': 'close_mapdiv',
		'class': 'close_mapdiv'
	});
	
	
	myCloseDiv.addEvent('click', function(event){
		myBGDiv1.destroy();
		myDiv.destroy();
		myBGDiv2.destroy();
		myCloseDiv.destroy();
	});
	
	myBGDiv1.setStyles({'width':innerWidth, 'height':innerHeight, 'background-color':'#111'});
	myBGDiv2.setStyles({'width':innerWidth, 'height':innerHeight});
	myDiv.setStyles({'left':middleX - 340, 'top':300});
	myCloseDiv.setStyles({'left':middleX - 365, 'top':275});
	myBGDiv1.inject($('main_container'));
	myBGDiv2.inject($('main_container'));
	myDiv.inject($('bg_mapdiv2'));
	myCloseDiv.inject($('main_container'));
	
	mapActive	= true;
	
	var map = new google.maps.Map(document.getElementById("mapdiv"), myOptions);
	
	//var contentString 	=  '<div id="map_content"><h1 class"common">Maaq Signing & Interior</h1><div id="bodyContent"><b>Adres</b><br>';
   	var contentString		=  name_string;
    //contentString 		=  contentString + contentString_2 + '</div></div>';
        
    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

   var marker = new google.maps.Marker({
        position: position, 
        map: map,
        title:root_short
    });
   
    infowindow.open(map,marker);	
}

function resizeBGSets(){
	if($('detailimage') && $('showing_sets')){
		if(innerWidth >= 1000){
			$('detailimage').setStyles({'width' : innerWidth});
		}
	}
	if($('product_info_set')){
		if(innerWidth >= 1000){
			$('product_info_set').setStyle('left' , middleX + middleX / 2);
		}
	}
	
}

function resizeBG(){
	if(proportions_original >= proportions_site){
		new_height		= innerWidth * proportions_original
		if(new_height < innerHeight){
			new_height 		=  innerHeight;
			new_width 		=  new_height * proportions_original;
		}
		$('slide_image').setStyles({'width' : new_width, 'height' : new_height});
		$('slides_container').setStyles({'top' : 0, 'left' : 0});
		//$('slide_image').setStyle('width' ,innerWidth);
	}else{
		var new_width		= innerHeight / proportions_original
		if(new_width < innerWidth){
			new_width 		=  innerWidth;
			new_height 		=  innerWidth / proportions_original;
		}
		$('slide_image').setStyles({'width' : new_width, 'height' : innerHeight});
		$('slides_container').setStyles({'left' : 0 , 'top' : 0});
	}
}





function updateScroll(target, scroll_in){
	var this_height 		= '';
	var container_height 	= '';
	
	if(browserIE){
		this_height 		= $(target).offsetHeight + 150;
		container_height 	= $(scroll_in).offsetHeight;
	}else{
		this_height 		= $(target).getHeight();
		container_height 	= $(scroll_in).getHeight();
	}
	
	if(container_height < this_height){
		activeScrollElement 	= $(scroll_in);
		activeScroll 			= new Fx.Scroll(activeScrollElement);
		scrollPosition			= 0;
		activeScrollHeight		= this_height - container_height;
		activeScroll.set(0, scrollPosition);
		
		$('scroll-wrapper').setStyle('display','block');
		$('scroll-wrapper').setStyle('opacity',1);
		
		mySlider = new BasicSlider('scroll-container', 'scroller', {
			range: [0,800],
			wheel: true,
			steps: 100,
			snap: true,
			mode: 'vertical',
			onChange: function(pos){
				scrollPosition	= pos/700 * activeScrollHeight;
				activeScroll.set(0, scrollPosition);
				loadImageRows(scrollPosition);
				//console.log(scrollPosition);
				setCookie('scroll'+thisScrollPage,scrollPosition,1);
			}
		});
		
		
		if($('scroll-bottom')){
			$('scroll-bottom').addEvent('click', function(event){ 
				scrollContinues(1);
				//continuesScroller = scrollContinues.periodical(1500);
			});
			
			$('scroll-top').addEvent('click', function(event){ 
				scrollContinues(-1);
			});
		}
		
	}else{
		scrollPosition		= 0;
		$('scroll-wrapper').setStyle('opacity',0.1);
	}
}

var continuesScroller;
function scrollContinues(direction){
	scrollPosition	+= direction * 324;
	activeScroll.set(0, scrollPosition);
	//alert(scrollPosition);
	switch(direction){
		case 1:
		case 0.1:
			if( (scrollPosition / activeScrollHeight * 363) <= 363){
				$('scroller').setStyle('top',scrollPosition / activeScrollHeight * 363);
			}else{
				scrollPosition	-= 324;
				$('scroller').setStyle('top',363);
			}
			loadImageRows(scrollPosition);
		break;
		case -1:
		case -0.1:
			if( (scrollPosition / activeScrollHeight * 363) > 0){
				$('scroller').setStyle('top',scrollPosition / activeScrollHeight * 363);
			}else{
				scrollPosition	= 0;
				$('scroller').setStyle('top',0);
			}
			loadImageRows(scrollPosition);
		break;
		default:
			if( (scrollPosition / activeScrollHeight * 363) <= 363){
				$('scroller').setStyle('top',scrollPosition / activeScrollHeight * 363);
			}else{
				scrollPosition	-= 324;
				$('scroller').setStyle('top',363);
			}
			loadImageRows((scrollPosition + 600));
		break;
	}
	
	
	setCookie('scroll'+thisScrollPage,scrollPosition,1);
}

function scrollByButton(target, scroll_in, direction, updownORleftright){
	var this_size 		= '';
	var container_size 	= '';
	
	switch(updownORleftright){
		case 1:
			//up down
			//direction -1 is up 1 is down;
			if(browserIE){
				this_size 			= $(target).offsetHeight + 150;
				container_size 		= $(scroll_in).offsetHeight;
			}else{
				this_size 			= $(target).getHeight();
				container_size 		= $(scroll_in).getHeight();
			}
		break;
		case 2:
			//left right
			//direction -1 is left 1 is right;
			if(browserIE){
				this_size 			= $(target).offsetWidth + 150;
				container_size	 	= $(scroll_in).offsetWidth;
			}else{
				this_size 			= $(target).getWidth();
				container_size 		= $(scroll_in).getWidth();
			}
		break;
	}
	
	if(container_size < this_size){
		if(scrollPosition + (direction * 300) < 0){
			scrollPosition 	= 0;
			direction		= 0;
		}
		
		if((scrollPosition + (direction * 300)) >= 0 && (container_size + scrollPosition + direction * 300) <= (this_size + 300)){
			activeScrollElement 	= $(scroll_in);
			activeScroll 			= new Fx.Scroll(activeScrollElement);
			scrollPosition			+= direction * 300;
			setCookie('scroll'+thisScrollPage,scrollPosition,1);
			switch(updownORleftright){
				case 1:
				activeScroll.start(0, scrollPosition);
				break;
				case 2:
				activeScroll.start(scrollPosition,0);
				break;
			}
		}
	}
}


function scrollToPosition(target, scroll_in, toPosition, updownORleftright){
	var this_size 		= '';
	var container_size 	= '';
	
	scrollPosition		= toPosition;
			
	
	switch(updownORleftright){
		case 1:
			//up down
			//direction -1 is up 1 is down;
			if(browserIE){
				this_size 			= $(target).offsetHeight + 150;
				container_size 		= $(scroll_in).offsetHeight;
			}else{
				this_size 			= $(target).getHeight();
				container_size 		= $(scroll_in).getHeight();
			}
		break;
		case 2:
			//left right
			//direction -1 is left 1 is right;
			if(browserIE){
				this_size 			= $(target).offsetWidth + 150;
				container_size	 	= $(scroll_in).offsetWidth;
			}else{
				this_size 			= $(target).getWidth();
				container_size 		= $(scroll_in).getWidth();
			}
		break;
	}
	
	if(container_size < this_size){
		activeScrollElement 	= $(scroll_in);
		activeScroll 			= new Fx.Scroll(activeScrollElement);
		switch(updownORleftright){
			case 1:
			activeScroll.start(0, scrollPosition);
			setCookie('scroll'+thisScrollPage,scrollPosition,1);
			break;
			case 2:
			activeScroll.start(scrollPosition,0);
			break;
		}
	}
}

















function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name){
			return unescape(y);
		}
	}
}



var rolled = 0;
	
function MouseScroll (event) {
	if ('wheelDelta' in event) {
		rolled = event.wheelDelta;
	}
	else {  // Firefox
			// The measurement units of the detail and wheelDelta properties are different.
		rolled = -40 * event.detail;
	}
	
	if(rolled > 0){
		scrollContinues(-0.1);
	}else{
		scrollContinues(0.1);
	}
}

function InitScroll() {
		// for mouse scrolling in Firefox
	var elem = document.getElementById ("main_container");
	if (elem.addEventListener) {
		if(browserFF){
			elem.addEventListener ("DOMMouseScroll", MouseScroll, false);
		}else{
			elem.addEventListener ("mousewheel", MouseScroll, false);
		}
	}
	else {
		if (elem.attachEvent) { // IE before version 9
			elem.attachEvent ("onmousewheel", MouseScroll);
		}
	}
}





