var rootsite = "http://www.bergmanoptiek.nl/";
var slider_mouse_is_down = false;
var reset_slider_timer = null;
var reset_time = 8000;
	window.onload = function(){ vertical_align_collection(); }
$(document).ready(function(){
	$("#search input").focus(function(){
		if($(this).val() == "Zoeken") $(this).val("");
	}).blur(function(){
		if($(this).val() == "") $(this).val("Zoeken");
	}).keypress(function(e){
		if((e.keyCode ? e.keyCode : e.which) == 13) {
			window.location = rootsite + 'search/keywords/' + $(this).val().replace('/','').replace('\\','') + '/';
		}
	});
	launch_selector_application();
	$("#css-small").click(function(){change_size(1); small_font();});
	$("#css-medium").click(function(){change_size(2); medium_font();});
	$("#css-big").click(function(){change_size(3); big_font();});
	$("#button-route").click(function(){wijsdeweg($("#textbox-route").val());});
	$("#button-print").click(function(){popuproute($("#textbox-route").val());});
	$("#slide-application").mousemove(function(e){
		if(slider_mouse_is_down) $(".bulp").css('marginLeft', get_mouseslider_position_from_page(e.pageX)+'px');
	});
	$(document).mouseup(function(){if(slider_mouse_is_down){reset_slider_timer = setTimeout('reset_slider()', reset_time);}slider_mouse_is_down=false;});
	$(".bulp").mousedown(function(){
		$(this).stop();
		slider_mouse_is_down = true;
		clearTimeout(reset_slider_timer);
	});
	$(".slide, .slide-line").mousedown(function(e){
		$(".bulp").stop().css('marginLeft', get_mouseslider_position_from_page(e.pageX)+'px');
		clearTimeout(reset_slider_timer);
		reset_slider_timer = setTimeout('reset_slider()', reset_time)
	});
});
var get_mouseslider_position_from_page = function(x) {
	var click = x;
	var width = 800;
	if($(document).width() > 800) width = $(document).width();
	var half = ((width/2)-400)+274;
	var position = click - half;
	return position;
}
var change_size = function(size) {
	var exp = new Date();
	exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 365));
	set_cookie('CKY_TEXT_SIZE',size, exp);
}
var set_cookie = function(name, value, expires) {
	document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString()); 
}
var replaceimages = function(){
	$("#products .product img").each(function(){
		if($(this).height()<120){
			$(this).css('marginTop',parseInt((120-$(this).height())/2));
		}
	});
	$("#brands .brand img").each(function(){
		if($(this).height()<150){
			$(this).css('marginTop',parseInt((150-$(this).height())/2));
		}
	});
}
var vertical_align_collection = function () {
	vertical_align($(".text"), 350);
	replaceimages();
}
var small_font = function() {
	$("#font-css").attr('href', rootsite + 'css/style_small.css');
	vertical_align_collection();
}
var medium_font = function() {
	$("#font-css").attr('href', rootsite + 'css/style_medium.css');
	vertical_align_collection();
}
var big_font = function() {
	$("#font-css").attr('href', rootsite + 'css/style_big.css');
	vertical_align_collection();
}
var slider_check_timer = null;
var sliders = new Array();
var slider_options = function() {
	var show_from = 0;
	$(".slide").each(function(){
		show_from += $(this).width();
		sliders[sliders.length] = show_from;
	});
}
var prev_id = null;
var slide_speed = 1000;
var slider_transfer = function() {
	var margin = $(".bulp").css('marginLeft');
	var split = margin.split(".");
	var output = split[0].replace("px","");
	var pre_val = 0;
	for(var i = 0; i < sliders.length; i++) {
		if(pre_val <= output && sliders[i] >= output) {
			if($("#slide-image-"+(i+1)).css('display') == 'none') {
				var id = "slide-image-"+(i+1);
				if(prev_id != null) $("#"+prev_id).hide();
				$("#slide-application .text").animate({'opacity':'0'},200,function(){
					$("#slide-application .text span").html($("#"+id).attr('ftitle'));
					$("#slide-application .text p").html($("#"+id).attr('ftext'));
					vertical_align($(".text"), 350);
					$(this).animate({'opacity':'1'},200);
				});
				$("#" + id).css({'opacity':'0','display':'block'}).animate({'opacity':'1.0'},slide_speed, function(){
					$(".images").css('background',"url('"+$("#" + id).attr('src')+"')");
				});
				prev_id = id;
			}
			pre_val += sliders[i];
		}
	}
}
var start_slider = function() {
	slider_check_timer = setInterval("slider_transfer()", 10);
	$(".bulp").animate({marginLeft:'525px'},(sliders.length * 10000),'linear', function(){
		$(this).css('marginLeft','0px');
		clearInterval(slider_check_timer);
		start_slider();
	});
}
var reset_slider = function() {
	$(".bulp").animate({marginLeft:'0px'},1000, function(){
		start_slider();
	});
}
var vertical_align = function(element, relative_height) {
	var top = relative_height / 2;
	var substract = $(element).height() / 2;
	$(element).css('marginTop', (top - substract) + "px");
}
var eventdate = function() {
	$.post("./times/",{date: $("#date .title").html()}, function(data) {
		$("#time .title").html("Kies een tijdstip");
		$("#time .value-container").val("");
		$("#time .items").html(data);
		reset_selector_application("time");
	});
	
}
var selection_timer = null;
var launch_selector_application = function() {
	$(".select-control .items .item").mouseenter(function(){
		$(this).css('background', '#eaeff7');
	}).mouseleave(function(){
		$(this).css('background', '#ffffff');
	}).click(function(){
		$(this).parent().parent().children('.title').html($(this).html());
		$(this).parent().parent().children('.value-cotainer').val($(this).html());
		eval("try{event"+$(this).parent().parent().attr('id')+"();}catch(e){}");
	});
	$(".select-control").click(function(){
		if($(this).children(".items").css('display') == 'block') {
			$(this).children(".items").css('display', 'none');
		} else {
			$(".select-control .items").css('display', 'none');
			$(this).children(".items").css('display', 'block');
		}
	});
	$(".select-control .items").mouseenter(function(){
		if(selection_timer != null) clearTimeout(selection_timer);
	}).mouseleave(function() {
		var id = $(this).parent().attr("id");
		selection_timer = setTimeout("$(\"#"+id+" .items\").css('display','none');", 2000);
	});
}
var reset_selector_application = function(id) {
	$("#"+id+" .items .item").mouseenter(function(){
		$(this).css('background', '#eaeff7');
	}).mouseleave(function(){
		$(this).css('background', '#ffffff');
	}).click(function(){
		$(this).parent().parent().children('.title').html($(this).html());
		$(this).parent().parent().children('.value-cotainer').val($(this).html());
		eval("try{event"+$(this).parent().parent().attr('id')+"();}catch(e){}");
	});
}
