// main

speed=500;

$(document).ready(function() {
	ReSize();
	init();
});

$(window).load(function() {
	ReSize();
	reInit();
	
	if ($('#dialog')) {
		var h=$('.footer').position();
		h=h.top-350;
		$('#dialog').css({top:h}).fadeIn(speed*2);
		if ($('#dialog').hasClass('temp')) {
			$('#dialog').fadeOut(speed*8,function(){
				$('#dialog').remove();
			});
		}
	}
	
});
$(window).resize(function() {ReSize();});

function init() {
	$('.text').css({overflow:'hidden'});
	shiftRightMenus();
	showControls();
	fotosClick();
	actueelClick();
}

function ReSize() {
	// size of big foto
	var iw=$('.container').width();
	if (iw<960) iw=960;
	iw-=625;
	var ih=$('.container').height();
	if (ih<500) ih=500;
	ih-=160;
	if (iw>ih) {
		var im=70+(iw-ih)/2;
		$('.photo').css({width:ih, height:ih, 'margin-left':im});
	}
	else {
		$('.photo').css({width:iw, height:iw});
	}

	// text height
	var h=$('.footer').position();
	h=h.top;
	$('.content .text').css({height:(h-255)});
	$('.content .photos').css({height:(h-80)});
	// scrollbars
	var version=String($.browser.version);
	version=version.substring(0,1);
	version=Number(version);
	if (! ($.browser.msie && version<=7)) {
		$('.content .text').jScrollPane({verticalGutter:20});
		$('.content .photos').jScrollPane({verticalGutter:20});
		$('#beurs_form').jScrollPane({verticalGutter:20});
	}
	else {
		$('.content .text').css({overflow:'auto'});
		$('.content .photos').css({overflow:'auto'});
	}
}

function reInit() {
	if (!($.browser.msie && $.browser.version<=6)) setAnimaties();
}


// Plugins & functions

// reverse
(function($) {
	$.fn.reverse = function() {
		return this.each(function() {
			$(this).prependTo( $(this).parent() );
		});
	};
})(jQuery);


function shiftRightMenus() {
	var li_right=$('.menu li.rechts').reverse();
	$(li_right).css({float:'right'}).removeClass('rechts');
	var li_sub=$('.submenu ul.lev3>li').reverse();
	$(li_sub).css({float:'right','margin-top':'-3px'});
}

function showControls() {
	$('.container').each(function(){
		if (!$(this).hasClass('contact')  && !$(this).hasClass('woonbeurs')) {
			$(this).find('.controls:first').show();
			$(this).find('.tellfriendCtrl:first').unbind().click(function(){tellFriendDialog($(this));return false;});
			$(this).find('.newsletterCtrl:first').unbind().click(function(){newsLetterDialog($(this));return false;});
		}
		if ($(this).hasClass('kerstboom')) {
			var link=$('.content a:[href*=contact]');
			$(link).attr('href','#');
			$(this).find('div.photo img:first').add(link).unbind().click(function(){kerstboomDialog($(this));return false;}).css({cursor:'pointer'});
		}
	});
}

function Dialog(obj,title,form_fields) {
	var h=$('.footer').position();
	h=h.top-350;

	var url=form_fields.action;
	var html='<form action="'+url+'" method="post">';
	$.each(form_fields.fields, function(name,field){
		var pre=name.substring(0,name.indexOf('_'));
		if (typeof(field.value) == 'undefined') field.value='';
		switch (field.type) {
			case 'hidden':
				html+='<input id="dialogForm'+name+'" type="hidden" name="'+name+'" value="'+field.value+'" />';
				break;
			case 'textarea':
				html+='<label>'+field.label+'</label><textarea id="dialogForm'+name+'" name="'+name+'"></textarea>';
				break;
			case 'dropdown':
				html+='<label>'+field.label+'</label><select id="dialogForm'+name+'" name="'+name+'">';
				$.each(field.options, function(id,value){
					html+='<option value="'+value+'">'+value+'</option>';
				});
				html+='</select>';
				break;
			case 'checkbox':
				html+='<input type="checkbox" id="dialogForm'+name+'" name="'+name+'" class="checkbox" checked="checked" /><label>'+field.label+'</label>';
				break;
			default:
				html+='<label>'+field.label+'</label><input id="dialogForm'+name+'" name="'+name+'" value="'+field.value+'" class="'+pre+'"/>';
			break;
		}
	});
	html+='<input type="submit" class="button submit" value="Verstuur" /></form>';
	
	$('body').append('<div id="mask"></div>');
	$('body').append('<div id="dialog"><div id="dialogTop"><h1>'+title+'</h1><div id="dialogClose"></div></div><div id="dialogContent">'+html+'</div></div>')
	// validation
	$('#dialog form').validate();
	$.each(form_fields.fields, function(name,field){
		if (typeof(field.validation)=='undefined') field.validation=true;
		var pre=name.substring(0,name.indexOf('_'));
		if (pre!='b' && field.validation) $('#dialogForm'+name).rules('add',{required:true});
		if (pre=='email') $('#dialogForm'+name).rules('add',{email:true});
	});
	
	$('#mask').fadeTo('fast',.3);
	$('#dialog').css({top:h}).fadeIn('fast');
	$('#dialogClose').click(function(){
		$('#dialog').fadeOut('fast',function(){
			$(this).remove();
			$('#mask').remove();
		});
	});
}

function tellFriendDialog(obj) {
	if (typeof(url)=='undefined') url=location.href;
	Dialog(obj,site.str_vriend_titel,{
		action:url,
		fields:{
			str_action:{type:'hidden',value:'tellfriend'},
			str_page:{type:'hidden',value:url},
			str_ownname:{label:site.str_vriend_eigen_naam},
			email_own:{label:site.str_vriend_eigen_email},
			str_friendname:{label:site.str_vriend_vriend_naam},
			email_friend:{label:site.str_vriend_vriend_email},
			txt_tekst:{label:site.str_vriend_tekst,type:'textarea'}
		}
	});
}
function newsLetterDialog(obj) {
	if (typeof(url)=='undefined') url=location.href;
	Dialog(obj,site.str_nieuws_titel,{
		action:url,
		fields:{
			str_action:{type:'hidden',value:'newsletter'},
			str_page:{type:'hidden',value:url},
			str_ownname:{label:site.str_contact_naam},
			email_own:{label:site.str_contact_email}
		}
	});
}
function kerstboomDialog(obj) {
	if (typeof(url)=='undefined') url=location.href;
	Dialog(obj,'Bestel hier de unieke kerstboom!',{
		action:url,
		fields:{
			str_action:{type:'hidden',value:'kerstboom'},
			str_page:{type:'hidden',value:url},
			str_name:{label:'Naam'},
			email_email:{label:'Email'},
			int_aantal:{label:'Aantal',value:'1'},
			str_verzenden:{type:'dropdown',label:'Verzendwijze',options:['Afhalen op werkplaats (&euro; 75,- per stuk)','Per post (&euro; 85,- incl. &euro; 10,- verzendkosten per stuk)']},
			str_adres:{label:'Afleveradres',validation:false},
			str_postcode_en_woonplaats:{label:'Postcode + Woonplaats',validation:false},
			b_newsletter:{type:'checkbox',label:'Aanmelden voor de nieuwsbrief'}
		}
	});
}



function fotosClick() {
	$('.photos .item').css({cursor:'pointer'}).click(function(){
		var i=$(this).children('img:first').attr('src');
		i=i.replace(/_thumb_/,'');
		$('.photo img:first').attr('src',i);
	});
}

function actueelClick() {
	$('.actueel_item').css({cursor:'pointer'}).unbind().click(function(){
		if ( ! $(this).hasClass('show')) {
			var i=$(this).children('img:first').attr('src');
			var html=$(this).children('.itemText:first').html();
			$('.photo').fadeOut('slow',function(){
				$('.photo img').attr('src',i);
				$('.photo').fadeIn('slow');
			});
			$('.actueeltext').fadeOut('slow',function(){
				$('.actueeltext .jspPane').html(html);
				$('.actueeltext').fadeIn('slow');
			});

			$('.actueel_item').removeClass('show');
			$(this).addClass('show');
		}
	});
}


/////////////////////////////////////


function setAnimaties() {
	$('.menu li').unbind().css({cursor:'pointer'}).click(function(){
		startOvergang($(this),'menu');
		return false;
	});
	$('.submenu li').unbind().css({cursor:'pointer'}).click(function(){
		if ($('body').hasClass('meubels'))
			startOvergang($(this),'meubels');
		else
			startOvergang($(this),'submenu');
		return false;
	});
	if ($('body').hasClass('meubels')) {
		$('.overzicht .item').unbind().click(function(){
			startOvergang($(this),'meubel');
			return false;
		});
		$('.next').unbind().click(function(){
			startOvergang($(this),'next');
			return false;
		});
		$('.prev').unbind().click(function(){
			startOvergang($(this),'prev');
			return false;
		});
	}

	// language switch
	$('.menu li.en').add('.menu li.nl').unbind().click(function(){
		if (typeof(url)=='undefined') url=location.href;
		if ($(this).hasClass('en'))
			url=url.replace('/nl/','/en/');
		else
			url=url.replace('/en/','/nl/');
		location.href=url;
		return false;
	});

}

function setOvergangType(menuItem,menuType) {
	var body=$('body');
	var from='';
	var oldPos=-1;
	var newPos=-1;
	if (body.hasClass('__home') || body.hasClass('home')) from='home';
	if ($('.photos').length>0) from='meubel';
	
	if (menuType!='meubel' && menuType!='next' && menuType!='prev') {
		var to=$(menuItem).attr('id');
		var lev=to;
		to=to.replace('menu_','');
		to=to.replace(/_pos\d_lev\d/,'');
		oldPos=getPos($(menuItem).parent('ul').children('li.active:first').attr('class'));
		newPos=getPos($(menuItem).attr('class'));
	}
	return {from:from, to:to, menu:menuType, oldPos:oldPos, newPos:newPos};
}

function getPos(c) {
	if (typeof(c)!='undefined') {
		c=c.replace(/.*pos/,'');
		c=c.replace(/ .*/,'');
		return Number(c);
	}
	return -1;
}


function startOvergang(menuItem,menuType) {
	$('body').css({overflow:'hidden'});
	// slide away subsub menu if any
	var subsub=$('.submenu li.active>ul.lev3');
	if (subsub.length>0) {$(subsub).animate({left:'100%'},speed/2);}
	
	// start transition
	var type=setOvergangType(menuItem,menuType);
	if (type.from!=type.to) {
		url=$(menuItem).attr('href');
		if (typeof(url)=='undefined') url=$(menuItem).children('a:first').attr('href');
		
		// load page
		$.get(url,{ajax:'true'},function(html) {
			oldcontainer=$('#old');
			ch=oldcontainer.height();
			cw=oldcontainer.width();
			oldcontainer.after('<div id="new"></div>');
			newcontainer=$('#new');
			newcontainer.html(html).attr('class','container '+newcontainer.children('#bodyclass:first').attr('class')).css({height:ch,width:cw});
			// reInit new page
			init();
			reInit();
			ReSize();
			// start animatie
			animOvergang(type,removeOldContainer);
		});
	}
}


function animOvergang(type,callback) {
	var menuTop=32;
	var submenuTop=64;
	var oldTo=0;
	
	switch (type.from) {
		case	'home':
			// ^
			newcontainer.css({top:ch});
			newcontainer.children('.menu:first').hide();
			newcontainer.children('.footer:first').css({bottom:menuTop});
			newcontainer.show().animate({top:menuTop},speed,callback);
			break;
		case 'meubel':
			switch (type.menu) {
				case 'next':
				case 'prev':
					if (type.menu=='next') {
						// sub <
						// newcontainer.show().css({top:submenuTop,left:cw});
						newcontainer.show().css({left:cw});
						oldTo=-cw;
					}
					else {
						// sub >
						// newcontainer.show().css({top:submenuTop,left:-cw});
						newcontainer.show().css({left:-cw});
						oldTo=cw;
					}
					// newcontainer.children('.menu:first').hide();
					// newcontainer.children('.submenu:first').hide();
					// newcontainer.children('.footer:first').css({bottom:submenuTop});
					oldcontainer.animate({left:oldTo},speed);
					newcontainer.animate({left:0},speed,callback);
					break;

				default:
					// v
					newcontainer.css({top:'0px','z-index':0}).show();
					oldcontainer.animate({top:ch},speed,callback);
					break;
			}
			break;

		default:
			switch (type.to) {
				case 'home':
					// v
					newcontainer.show().css({top:'0px','z-index':0});
					oldcontainer.css({top:menuTop}).children('.menu:first').hide();
					oldcontainer.animate({top:ch},speed,callback);
				break;

				default:
					switch (type.menu) {
						case 'menu':
							if (type.oldPos<type.newPos) {
								// <
								// newcontainer.show().css({top:menuTop,left:cw});
								newcontainer.show().css({left:cw});
								oldTo=-cw;
							}
							else {
								// >
								// newcontainer.show().css({top:menuTop,left:-cw});
								newcontainer.show().css({left:-cw});
								oldTo=cw;
							}
							// newcontainer.children('.menu:first').hide();
							// newcontainer.children('.footer:first').css({bottom:menuTop});
							oldcontainer.animate({left:oldTo},speed);
							newcontainer.animate({left:0},speed,callback);
							break;
						case 'submenu':
							if (type.oldPos<type.newPos) {
								// sub <
								// newcontainer.show().css({top:submenuTop,left:cw});
								newcontainer.show().css({left:cw});
								oldTo=-cw;
							}
							else {
								// sub >
								// newcontainer.show().css({top:submenuTop,left:-cw});
								newcontainer.show().css({left:-cw});
								oldTo=cw;
							}
							// newcontainer.children('.menu:first').hide();
							// newcontainer.children('.submenu:first').hide();
							// newcontainer.children('.footer:first').css({bottom:submenuTop});
							oldcontainer.animate({left:oldTo},speed);
							newcontainer.animate({left:0},speed,callback);
							break;
						case 'meubels':
							newcontainer.hide().css({top:submenuTop,left:'0px'});
							newcontainer.children('.menu:first').hide();
							newcontainer.children('.submenu:first').hide();
							newcontainer.children('.footer:first').css({bottom:submenuTop});
							newcontainer.fadeIn(speed,callback);
							break;
						case 'meubel':
							// ^
							newcontainer.css({top:ch});
							newcontainer.children('.menu:first').hide();
							newcontainer.children('.footer:first').css({bottom:menuTop});
							newcontainer.show().animate({top:menuTop},speed,callback);
							break;
					}
				break;
			}
		break;
	}
}

function removeOldContainer() {
	$('body').attr('class',newcontainer.children('#bodyclass').attr('class'));
	newcontainer.attr('id','old').css({width:'100%',height:'100%',top:'0px'});
	newcontainer.children('.menu:first').show();
	newcontainer.children('.submenu:first').show();
	newcontainer.children('.footer:first').css({bottom:'0px'});
	oldcontainer.remove();
	ReSize();
	// slide in lev3 submenu if any
	var submenu=('.submenu li.active>ul.lev3');
	if (submenu.length>0 && !$('body').hasClass('__meubels__alles')) {
		$(submenu).css({left:'100%'}).show();
		$(submenu).animate({left:'50%'},speed/2,function(){
			$('body').css({overflow:'auto'});
		});
	}
	else {
		$('body').css({overflow:'auto'});
	}
	
}


