/**
 * Kantoorbalie scripts
 *
 * @author frenkie
 */

$(function(){

	Navigation.createMain($("#mainnavigation"));
	Navigation.attachPrint( $("#footer li.PRINT") );

	Search.setUp($("#search > form"));
	Newsletter.setUp($("#newsletter > form"));
	Bookmark.setUp($("li.BOOKMARK"));
	var feats = new SlideShow($("#slideshow"), 1);
	var fotos = new SlideShow($("#fotoslideshow"), 2);
	
	$(".MAILAFRIEND").css("display", "inline");
});


var Navigation = {
	
	SEARCH_CITY : (BASEURL + "kantoorruimte_huren/::city::/?"),
	SEARCH_REGION : "",
	
	
	createMain : function(listing){
		
		listing.find("> li").each(function(){
			
			if(!$(this).is(".active")){
				$(this).bind("mouseover", function(){
					$(this).addClass("active");
					
				}).bind("mouseout", function(){
					$(this).removeClass("active");
				});
			};		
		});
	},
	
	
	attachPrint : function(to){
	
		to.bind("click", function(){	
			window.print();
			this.blur();
			window.focus();
			return false;
		});
		to.css({
			display:"inline"
		}).show();
	}
};


var Search = {
	
	NOMAX : "geen",
	
	setUp: function(form){
		
		this.form = form;
		this.redirectSubmit();
		this.attachAreaChange();
	},
	
	redirectSubmit : function(){
		
		var ref = this;
		this.form.bind("submit", function(){
			
			if($(this).find("select.plaats")[0].selectedIndex != 0){
					
				var action = Navigation.SEARCH_CITY.replace(/::city::/ig, ref.escape($(this).find("select.plaats").val()));
					action += "min_opp="+ $(this).find("select.min").val();
					action += "&max_opp="+ $(this).find("select.max").val();
				
				ref.form.attr("action", action);
				return true;
				
			}
			return false;
		});
	},
	
	escape : function(str){
		return str.replace(/\s/ig, '_');
	},
	
	attachAreaChange : function(){
		
		var ref = this;
		this.form.find("select.min").bind("change", function(){
			
			var max = ref.form.find("select.max");
			var maxval = max.val();
			var val = $(this).val();
			
			if(maxval != ref.NOMAX){				
				if( parseInt(val) >= parseInt(maxval) ){
					
					var nextopt = $(this).find("option[@value='"+ val +"']").next("option");
					if( nextopt.length == 1 ){
						max.val( nextopt.attr("value") );
					}else{
						max.val(ref.NOMAX);
					}
				}				
			}
			window.focus();
			$(this).blur();
		});
		this.form.find("select.max").bind("change", function(){
			
			var min = ref.form.find("select.min");
			var minval = min.val();
			var val = $(this).val();
			
			if(val != ref.NOMAX){				
				if( parseInt(val) <= parseInt(minval) ){
					
					var prevopt = $(this).find("option[@value='"+ val +"']").prev("option");
					if( prevopt.length == 1 ){
						min.val( prevopt.attr("value") );
					}else{
						min.val(min.find("option").eq(0).val());
					}
				}				
			}
			window.focus();
			$(this).blur();
		});		
	},
	
	attachAutoComplete : function(){
		
		this.form.find("input.plaats").autocomplete(BASEURL +"scripts/cities.php" , {});
	}
};

var Newsletter = {
	
	form : null,
	
	setUp: function(form){
		var ref = this;
		this.form = form;		
		this.form.bind("submit", function(){
			ref['notify'].call(ref);
			return false;
		});
	},
	
	notify: function(){
		//*** check e-mail
		var ref = this;
		var objEmail = this.form.find("input[@name='email']").val();
		var objName = this.form.find("input[@name='naam']").val();

		if(typeof(objName) == "string" && objName == ""){
			alert("Vergeet niet uw naam!");
			return false;
		}
		if(typeof(objEmail) == "string" && ref.echeck(objEmail)){

			//*** show loader
			this.form.append('<img class="loader" src="images/loader.gif" />');

			//*** post and alert
			  $.ajax({
				url: this.form.attr("action"),
				type: "POST",
				dataType: "html",
				data: {email: objEmail, naam: objName},
				complete:function(){
					ref.form.find("img.loader").remove();
				},
				success: function(objResult){
					
					ref.form.find("input[@name='email']").val('');
					ref.form.find("input[@name='naam']").val('');					
					alert(objResult);
				},
				error: function(objErr){alert("Er trad een probleem op, probeer het nogmaals.");}
			  });
		}else{
			alert("Vul een geldig e-mailadres in!");
		}
	},
	
	echeck: function(str) {
		var objReg = /[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
		var matched = str.match(objReg);

		if(matched){
			if(matched.length == 1){
				return true;
			}

		}
		return false;
	}
};


function SlideShow(slideshow, type){
	this.setUp(slideshow, type);
}
SlideShow.prototype = {
	
	TYPE_FEATURED : 1,
	TYPE_FOTO : 2,
	
	visibleSlides : 0,
	slideshow: null,
	startLeft:0,
	leftOut:0,
	slideOffset:0,
	slides: [],
	strip:null,
	previous: null,
	next: null,
		
	setUp : function(slideshow, type){
		if(slideshow.length > 0){
			this.slideshow = slideshow;
			this.strip = this.slideshow.find("#strip");			
			this.slides = this.slideshow.find("li.slide");
			this.type = type;
			switch(type){
				
				case this.TYPE_FEATURED:					
					this.visibleSlides = 3;
					this.startOffset = parseInt(this.strip.css("left"));
					this.slideOffset = (this.slides.length > 0)? this.slides.eq(0).width() : 0;
					break;

				case this.TYPE_FOTO:
					this.slideOffset = 65;
					this.startOffset = parseInt(this.strip.css("top"));
					this.visibleSlides = 4;
					break;			
			}			
			this.create();
		}
	},
	
	create: function(){
		
		var ref = this;
		if(this.slides.length > this.visibleSlides){
			
			if(this.type == this.TYPE_FEATURED){
				this.slideshow.find('#strip').width( this.slides.length * this.slideOffset);
			}
			
			this.slideshow.append('<img src="'+ BASEURL +'images/arrow_'+ ((this.type == this.TYPE_FEATURED)? 'left' : 'up' ) +'.gif" class="previous" alt="vorige" title="vorige" />');
			this.previous = this.slideshow.find("img.previous").hide();
			this.previous.bind('click', function(){			
				if(ref.leftOut < 0){
					ref.next.show();//always
					ref.leftOut++;
					if(ref.leftOut == 0){
						$(this).hide();
					}
					ref.strip.stop();
					
					var params = {};
					switch(ref.type){
						
						case ref.TYPE_FEATURED:
							params = {
								left: ( parseInt(ref.strip.css("left")) + ref.slideOffset ) +"px"
							};
							break;
							
						case ref.TYPE_FOTO:
							params = {
								top: ( parseInt(ref.strip.css("top")) + ref.slideOffset ) +"px"
							};
							break;							
					}				
					ref.strip.animate(params, 300, "swing");
				}	
			});
			
			this.slideshow.append('<img src="'+ BASEURL +'images/arrow_'+ ((this.type == this.TYPE_FEATURED)? 'right' : 'down' ) +'.gif" class="next" alt="volgende" title="volgende" />');
			this.next = this.slideshow.find("img.next");
			this.next.bind('click', function(){			
				if(Math.abs(ref.leftOut) + ref.visibleSlides < ref.slides.length){
					ref.previous.show();//always
					ref.leftOut--;
					if(Math.abs(ref.leftOut) + ref.visibleSlides == ref.slides.length){
						$(this).hide();
					}
					ref.strip.stop();
					
					var params = {};
					switch(ref.type){
						
						case ref.TYPE_FEATURED:
							params = {
								left: ( parseInt(ref.strip.css("left")) - ref.slideOffset ) +"px"
							};
							break;
							
						case ref.TYPE_FOTO:
							params = {
								top: ( parseInt(ref.strip.css("top")) - ref.slideOffset ) +"px"
							};
							break;							
					}					
					ref.strip.animate(params, 300, "swing");
				}	
			});			
		}
		
		//preset
		if(this.type == this.TYPE_FOTO){
			this.goto( this.strip.find('> li').index(this.strip.find('> li.active')) );
		}
	},
	
	goto : function(index){
		
		if(index >= this.visibleSlides){
			
			this.leftOut = -1 * (index+1-this.visibleSlides);
			this.previous.show();
			if(index+1 == this.slides.length){
				this.next.hide();
			};
			
			this.strip.stop();

			var params = {};
			switch(this.type){

				case this.TYPE_FEATURED:
					params = {
						left: ( this.startOffset - Math.abs(this.leftOut) * this.slideOffset ) +"px"
					};
					break;

				case this.TYPE_FOTO:
					params = {
						top: ( this.startOffset - Math.abs(this.leftOut) * this.slideOffset ) +"px"
					};
					break;							
			}					
			this.strip.animate(params, 300, "swing");
		}
	}
}

var Bookmark = {

	container:null,

	setUp: function(container){
		
		if(container.length > 0){
			this.container = container;
			this.create();
			this.container.show().css("display", "inline");
		}
	},

	create : function(){
		
		var url = escape(document.location.href);
		var title = escape(document.title);
		var bookmarks = $('<div class="bookmark">'+
			'<strong>Deel deze pagina via:</strong>'+
			'<ul>'+
				'<li><a rel="nofollow" target="_blank" href="http://digg.com/submit?phase=2&amp;url='+ url +'&amp;title='+ title +'" title="Digg"><img src="'+ BASEURL +'images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>'+
				'<li><a rel="nofollow" target="_blank" href="http://sphinn.com/submit.php?url='+ url +'&amp;title='+ title +'" title="Sphinn"><img src="'+ BASEURL +'images/sphinn.gif" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>'+
				'<li><a rel="nofollow" target="_blank" href="http://del.icio.us/post?url='+ url +'&amp;title='+ title +'" title="del.icio.us"><img src="'+ BASEURL +'images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>'+
				'<li><a rel="nofollow" target="_blank" href="http://www.facebook.com/sharer.php?u='+ url +'&amp;t='+ title +'" title="Facebook"><img src="'+ BASEURL +'images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>'+
				'<li><a rel="nofollow" target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk='+ url +'&amp;title='+ title +'" title="Google"><img src="'+ BASEURL +'images/googlebookmark.png" title="Google" alt="Google" class="sociable-hovers" /></a></li>'+
				'<li><a rel="nofollow" target="_blank" href="http://extension.fleck.com/?v=b.0.804&amp;url='+ url +'" title="Fleck"><img src="'+ BASEURL +'images/fleck.gif" title="Fleck" alt="Fleck" class="sociable-hovers" /></a></li>'+
				'<li><a rel="nofollow" target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u='+ url +'&amp;h='+ title +'" title="NewsVine"><img src="'+ BASEURL +'images/newsvine.png" title="NewsVine" alt="NewsVine" class="sociable-hovers" /></a></li>'+
				'<li><a rel="nofollow" target="_blank" href="http://www.stumbleupon.com/submit?url='+ url +'&amp;title='+ title +'" title="StumbleUpon"><img src="'+ BASEURL +'images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>'+
				'<li><a rel="nofollow" target="_blank" href="http://technorati.com/faves?add='+ url +'" title="Technorati"><img src="'+ BASEURL +'images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>'+
			'</ul></div>');	
			
		this.container.append(bookmarks);
		var ref = this;
		$(document).click(function(){
			ref.container.find('div.bookmark').hide();
		});
		this.container.find('> a').bind('click', function(){
			$(this).parent().find('> div.bookmark').toggle();
			window.focus();
			$(this).blur();
			return false;
		});
		
	}
}