if (typeof console == "undefined") {
	var console = {log:function(){}};
}

(function($){
	$.fn.dhtmlselect = function(options) {
	 	var opts = $.extend({}, $.fn.dhtmlselect.defaults, options);

		return this.each(function() {
    		 if ($(this).is('select')) {
    		 	var id = $(this).attr('id');
    		 	var title = $(this).attr('title');
    		 	var name = $(this).attr('name');
    		 	var value = $(this).find('option:selected').val();

    			$(this).removeAttr('id');
    		 	var div = $('<div id="'+id+'" class="dhtmlselect"></div>');
    		 	div.insertBefore($(this));
    		 	$(this).remove();

    		 	var text = $('<span>'+title+'</span>');
    		 	var dropdown = $('<div class="dropdown"></div>');
    		 	var input = $('<input type="hidden" name="'+name+'"></input>');

    		 	div.append(text);
    		 	div.append(input);
    		 	div.append(dropdown.css({'max-height': '300px', 'overflow':'auto'}).hide());

    			$(this).data('text', text);
    		 	$(this).data('dropdown', dropdown);
    		 	$(this).data('input', input);

    			var opt = $(this).find('> option');
    			for (var i=0; i < opt.length; i++) {
    				var a = $('<a href="'+opt[i].value+'">'+opt[i].text+'</a>')
    				dropdown.append(a);
    				if (typeof opts.onClick == "function") {
    					a.click(opts.onClick);
    				} else {
    					a.click( function(event){
    						event.preventDefault();
    						input.val($(this).attr('href'));
    						dropdown.hide();
    						text.text($(this).text());
    					});
    					if (opt[i].value == value) {
    						a.triggerHandler('click');
    					}
    				}

    			}
    			div
    				.css('zIndex', opts.combozindex--)
    				.mouseenter(function(){ dropdown.show();	})
    				.mouseleave(function(){ dropdown.hide();	});

    		 }
       	});
 	};

 	$.fn.dhtmlselect.defaults = {
		combodropimage: '',
		combodropoffsetY: 2,
		combozindex: 100,
		onClick: null
  	};


})(jQuery);


(function($){
	$.fn.suedmap = function(options) {
	 	var opts = $.extend({}, $.fn.suedmap.defaults, options);

	 	var targetContainer = $(opts.target).css('position', 'relative');
	 	var handleTargets = (targetContainer.length > 0);

	 	function load(href, target, targetHead, targetImg, targetText) {
	 		$.get(href, function(response) {
				var content = $(response).find('#main-wrapper');
				var title = content.find('h1:first').html();

				var txt = content.find('div.preview').html();
				var img = false;

				if (txt == null) {
					txt = content.find('.text-content p:first').html();
					if (txt != null) {
						txt = txt.replace(/^[\t ]*([^\.]*).*$/, "$1.");
						var nTxt = txt.replace(/^(([^ ]+[ \.]){1,42}).*$/, "$1");
						if (nTxt != txt) {
							txt = nTxt + " ...";
						}
					}
					img = content.find('#animate-teaser').css('background-image');
					if (img) {
						targetImg.attr('src', img.replace(/^.*\("?([^"]*)"?\).*$/, "$1"));
						targetImg.show();
					}
				}
				targetHead.html(title);
				targetText.html(txt);
				target.removeClass('loading');
				if ( targetHead.is(':visible') ) {
					Cufon.replace('.cufon.dynamic');
				}
			});
		}

	 	var preloading = opts.preloading;

		return this.each(function() {
			var mapName = $(this).attr('name');
			var img = $('img[usemap="#'+mapName+'"]');
			var baseUrl = img.attr('src');

			var areas = $(this).find('area');
			var openIdx = parseInt(Math.random() * areas.length);


    		areas.each(function(idx) {
    			var itms = $(this).attr('href').split('#');
				var href = itms[0];

				if (itms[1].match(/\.jpg$/)) {
					var src = baseUrl.replace(/map(\.jpg|_.*\.jpg)/, itms[1]);
				} else {
					var src = baseUrl.replace(/\/[^\/]*$/, '/'+itms[1]+".jpg");
				}

				setTimeout(function() {
					var objImage = new Image();
					objImage.src = src;
				}, 50);

				if (handleTargets) {
					var target = $('<div class="hidden loading" id="'+$(this).attr('id')+'_target"></div>').appendTo(targetContainer);

					var targetHead = $('<h4 class="cufon dynamic"></h4>').appendTo(target);
	 				var targetImg = $('<img src="" class="hidden" style="width:310px;"/>').appendTo(target);
	 				var targetText = $('<p style="overflow: hidden;"/>').appendTo(target);

	 				target.data('loaded', false);
	 				if (preloading && href) {
	 					if (openIdx == idx) {
	 						load(href, target, targetHead, targetImg, targetText);
	 					} else {
	 						setTimeout( function() { load(href, target, targetHead, targetImg, targetText); }, 300);
	 					}

	 				}
				}
	 			//$('<img src="'+src+'" class="hidden" />').appendTo(img.parent());

				if (href) {
					if (window.location.href.search(href) >= 0) {
						baseUrl = src;
						img.attr('src', src);
					};
				}

				var over = function() {
					img.attr('src', src);
					if (handleTargets) {
						targetContainer.find("> div:visible").fadeOut();
						target.fadeIn(500, function() { Cufon.replace('.cufon.dynamic'); } );
						if (!target.data('loaded')) {
							target.data('loaded', true);
							if (!preloading && href) {
	 							load(href, target, targetHead, targetImg, targetText);
	 						}
						}

					}
				};
				var out = function() {
				//	img.attr('src', baseUrl);
				//	if (handleTargets) {
				//		target.fadeOut();
				//	}
				};

				if (handleTargets) {
					if (openIdx == idx) {
						over();
					}
					$(this).hoverIntent( over, out );
				} else {
					$(this).hover( over, out );
				}

				var title = document.title.split('-');
				title = title[0].replace(/^\s*/, '');
				$(this).click(function(event) {
					event.preventDefault();
					if (href) {
						window.location.href = href += '?returnToLabel='+encodeURIComponent(title)+'&returnToUrl='+encodeURIComponent(window.location.pathname);;
					}
				});


    		});
       	});
 	}

 	$.fn.suedmap.defaults = {
		size: 'big',
		target: '.previewContainer',
		preloading: true
  	};

  	$('.suedmap').suedmap();



})(jQuery);


function closeLightBox() {
	$("#lightbox_bg").removeClass('loading').fadeOut();
	$("#imglightbox").fadeOut();
}

$.fn.center = function () {
	return $(this).each(function() {
		$(this)
		.css("position","absolute")
		.css("top", ( $(window).height() - $(this).height() ) / 2+$(window).scrollTop() + "px")
		.css("left", ( $(window).width() - $(this).width() ) / 2+$(window).scrollLeft() + "px");
	});
};


function openLightboxWithUrl(url, params, width, height, callback, type) {
	if ($('#lightbox_bg').addClass('loading').is(":hidden")) {
		$("#lightbox_bg").fadeTo('slow', 0.6);
	}

	if (typeof type == "undefined") {
		type = 'html';
	}

	$.ajax({
		url: url,
		dataType: type,
		data: params,
		success: function(data) {
			$("#imglightbox").css("width", width).css("height", height);
			$("#imglightbox").center();
			callback(
				data,
				function(content) {
					$("#imglightbox").html(content).fadeIn();
					$('#lightbox_bg').removeClass('loading');
				}
			);
		},
		error: function(xhr) {
			$("#imglightbox").css("width", width).css("height", height);
			$("#imglightbox").center();

			$(xhr.responseText).each(function() {
				if ($(this).is('div, body')) {
					$(this).find('#lightbox_bg, #imglightbox').remove();
					$("#imglightbox").html('').append($(this)).fadeIn();
					$('#lightbox_bg').removeClass('loading');
				}
			});
		}
	});
}

function openLightBox(id,type, picId) {


	var width;
	var height;
	var type;

	if(type == "img_small") {
		size="small";
		width="519px";
		height="360px";
	}
	if(type == "img_big") {
		size="big";
		width="934px";
		height="600px";
	}
	if(type == "detail") {
		width="726px";
		height="530px";
	}

	openLightboxWithUrl(
		"/ajaxContainer",
		"type="+type+"&id="+id+"&picid="+picId,
		width,
		height,
		function(data, showContent) {
			if(data.success) {
				if(data.startSlide == null) {
					startslide = 0;
				} else {
					startslide = data.startSlide;
				}

				showContent(data.html);

				$('#pictureCycle').cycle({
					fx:     'fade',
					speed:  'fast',
					timeout: 0,
					startingSlide: startslide
				});
				$('.thumb:eq('+startslide+')').trigger('click');
				Cufon.replace('.cufon');
				Cufon.replace('#mainnav ul li a.cufon', { fontFamily: 'Kievit' });
			}
		},
		'json'
	);
}

$(function() {
	Cufon.replace('.cufon');
	Cufon.replace('.cufonKievit', { fontFamily: 'Kievit' });

	$("#lightbox_bg").click(function () {
		closeLightBox();
		return false;
	});

	$('#search-query').focusin(function(event){
		if($(this).data("fieldvalue") === undefined) {
			$(this).data("fieldvalue",$(this).val());
		}
		if($(this).data("fieldvalue") == $(this).val()){
			$(this).val("");
		}
	});

	$('#search-query').focusout(function(event){
		if($(this).val() == "") {
			$(this).val($(this).data("fieldvalue"));
		}
	});


	$('.facebook a').click(function(event){
		title = $('title:eq(0)').html();
		url = window.location;
		window.location.href = "http://www.facebook.com/share.php?u="+escape(url)+"&t="+escape(title);
		event.preventDefault();
	});
	$('.twitter a').click(function(event){
		title = $('title:eq(0)').html();
		url = window.location;
		window.location.href = "http://twitter.com/home?status="+escape(title)+": "+escape(url);
		event.preventDefault();
	});

	$('.sidegal ul').jcarousel({
		wrap:'circular',
		visible:1,
		scroll:1,
		itemFirstInCallback: function(carous, item, index, action) {
			var target = $(item).closest('.sidegal').find('p.name');
			var title = $(item).find('.title').html();
			var descr = $(item).find('.desctiption').html();
			if (descr != "") {
				target.html(title + ": " + descr);
			} else {
				target.html(title);
			}
		}
	});


	$(".CheckBoxClass").change(function(event){
		if($(this).is(":checked")){
			$(this).next("label").addClass("LabelSelected");
		}else{
			$(this).next("label").removeClass("LabelSelected");
		}
	});
	$(".RadioClass").change(function(event){
		$(".RadioClass[name='"+$(this).attr('name')+"']").each(function() {
			$(this).next("label").removeClass("LabelSelected");
		});
		if($(this).is(":checked")){
			$(this).next("label").addClass("LabelSelected");
		}
	});
	$(".CheckBoxClass, .RadioClass").trigger('change');

	var title = document.title.split('-');
	title = title[0].replace(/^\s*/, '');
	$('a.returnToMe, :not(.startpage) > div.container div.containerText a').each(function() {
		var href = $(this).attr('href');
		if (href.substr(0,6) != "mailto") {
			if (href.indexOf('?') > 0) {
				href += '&returnToLabel='+encodeURIComponent(title)+'&returnToUrl='+encodeURIComponent(window.location.pathname);
			} else {
				href += '?returnToLabel='+encodeURIComponent(title)+'&returnToUrl='+encodeURIComponent(window.location.pathname);
			}
			$(this).attr('href', href);
		}
	});


	$('.sendsite:not(.nomail) a, a.sendsite:not(.nomail)').each(function() {
		$(this).closest('.not').removeClass('not');
		$(this).unbind('click');
		var href = $(this).attr('href');
		var title = document.title;
		if (href.substr(0,4) != "http" ) {
			var url = "";
			if (href == "/" || href == "" || href == "#") {
				url = window.location.href;
			} else if (href.substr(0,1) == "#") {
				url = window.location.href + href;
			} else {
				url = window.location.protocol + "//" + window.location.host + href;
			}
			href = url;
		}
		if ( $(this).attr('title') != '') {
			title = $(this).attr('title');
		}
		$(this).attr('href', 'mailto:?Subject='+encodeURIComponent(title)+'&Body='+encodeURIComponent(href) );
	});

});

