//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//emulate popup function...
var popWindow = function(u,w,h,popEl){
	if(!popEl){
		e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		if(e == 'png' || e == 'gif' || e == 'jpg')
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>",{overlay:20});
		else
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\"  width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>",{overlay:20});
	}
	popEl.modal({
			onOpen: function (dialog) {
						dialog.data.addClass('alert');
						dialog.container.css('height','auto');
						dialog.overlay.fadeIn('normal', function () {
							dialog.container.show('normal', function () {
								dialog.data.slideDown('normal'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('normal', function () {
						  dialog.container.hide('normal', function () {
							dialog.overlay.fadeOut('normal', function () {
							  $.modal.close(); // must call this to have SimpleModal
											   // re-insert the data correctly and
											   // clean up the dialog elements
							});
						  });
						});
					}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
}

//////////////////////////////////////////////////////////////////////// Same height

var make_same_height = function(selector){
	var max_height = 0;
	$(selector).each(function(){
		if($(this).height() > max_height){
			max_height = $(this).height();
		}
	});
	$(selector).height(max_height);	
}


$(document).ready(function(){

	make_same_height('#footer .block');
	
	$('form.form_newsletter table td.label,form.form_newsletter table td.spacer').hide();
	
/*	-------------------------------------------
	MENU
	----------------------------------------- */
	
	$("#menu > ul > li").hover(
		function(){
			$(this).children().addClass('open');
			i = $(this).children('ul:eq(0)');
			if(i.is(":hidden")) {
				i.show();
			}
		},
		function(){
			$(this).children().removeClass('open');
			i = $(this).children('ul:eq(0)');
			if(i.is(":visible"))
				i.hide();
		}
    );

/*	-------------------------------------------
	SLIDESHOW PHOTOS
	----------------------------------------- */

	function slideshowPhotos(i){
		t = $('.slideshow_module.photos .slides .slide').length-1;
		$('.slideshow_module.photos .slides .slide:eq('+ i +')').css('z-index','1').addClass('active').fadeTo(600,1,function(){
			if (i!=t){ i++; } else { i = 0; }
			$(this).fadeTo(10000,1,function(){
				$(this).removeClass('active').fadeTo(600,0,function(){
					$(this).css('z-index','0');
				});
				slideshowPhotos(i);
			});
		});
	}
	if($('.slideshow_module.photos').length){
		slideshowPhotos(0);
		$('.slide .slide_title').click(function(){
			var u = $(this).find('a').attr('href');
			if (u!=""){
				window.location = u;
			}
		});
	}

/*	-------------------------------------------
	SLIDESHOW CAFES
	----------------------------------------- */
	
	if($('.slideshow_module.cafes').length){
		$('.slideshow_module.cafes .menu a:eq(0),.slideshow_module.cafes .slides ul li:eq(0)').addClass('active');
	}
	$('.slideshow_module.cafes .menu a').click(function(e){
		e.preventDefault;
		var scroll_amount = parseInt(-180);
		var index = $(this).index();
		var new_scroll_pos = (index*scroll_amount);
		$('.slideshow_module.cafes .slides ul li,.slideshow_module.cafes .menu a').removeClass('active');
		$('.slideshow_module.cafes .slides ul li:eq('+index+'),.slideshow_module.cafes .menu a:eq('+index+')').addClass('active');
		$('.slideshow_module.cafes .slides ul').stop().addClass('animating').animate({'marginLeft':new_scroll_pos}, {queue:false, duration:1000, easing:"easeInOutExpo",complete:function(){
			$(this).removeClass('animating');
		}});
	
	});


/*	-------------------------------------------
	POPUP
	----------------------------------------- */

	var img = new Array();
	$(".article a").each(function(){
		i = 0;

		//change all onclick popups
		if($(this).attr('onclick')){
			i = $(this).attr('onclick').toString().indexOf("dow.open(this.href,");

			if(i){
				w = 640;
				h = 480;
				
				var u = $(this).attr('href');
				var e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
				
				if(e == 'png' || e == 'gif' || e == 'jpg'){
					var id = 'popwinlink_' + uid();
					$(this).attr('id',id);

					img[id] = new Image();
					img[id].onload = function(){
						img[id].onload = null;
						w = img[id].width;
						h = img[id].height;
	
						$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
					}
					img[id].src = u;
				}else{
					c = $(this).attr('onclick');
	
					h = c.substring(c.indexOf('height=') + 7);
					h = h.substring(0,h.indexOf(','));
	
					w = c.substring(c.indexOf('width=') + 6);
					w = w.substring(0,w.indexOf(','));
	
					$(this).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");				
				}
			}

		//change all movie files
		}else if($(this).attr('href') && $(this).attr('href').match(/.(flv|FLV)$/)){
			//get filename...
			u = $(this).attr('href').match(/(\/db\/.*.(flv|FLV)$)/)[1];
			$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/alittlecowsdream.nl/video.php?file=" + u + "',480,360);");		
		}
	});
	

});
