//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//emulate popup function...
var popWindow = function(u,w,h,popEl,callback){
	var c = callback;
	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.show();
						dialog.data.addClass('alert');
						dialog.container.css('height','auto');
						dialog.overlay.fadeIn('normal', function () {
							dialog.container.show('normal', c);
						});
					},
			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
							eval(c);
							});
						  });
						//});
					}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
}



$(document).ready(function(){
	$('ul.shop_categories a').removeClass('clear');

	//pop custom fields in shoppingcart
	$('.shop_cart div.custom_fields input').removeAttr('onchange').change(function(){
		$.post('/index.php?module=shop&cart=Y',$(this).serialize());
	});
	$('<a href="#" class="custom_fields_button"><b>Klik hier voor personaliseren boek</b></a>').toggle(
		function(){
			$(this).addClass('open');
			$('.shop_cart div.custom_fields').slideDown('normal');
			return false;
		},
		function(){
			$(this).removeClass('open');
			$('.shop_cart div.custom_fields').slideUp('normal');
			return false;
		}
	).insertBefore($('.shop_cart div.custom_fields'));

	//rebuild store listing
	$(".store_module .listing div.article").addClass('article_container')
	$(".store_module .specs").addClass('clear');
	$(".store_module .listing").each(function(){
		var maxHeightTitle = 0;
		$(this).find("div.article .title").each(function(){
			if($(this).height() > maxHeightTitle)
				maxHeightTitle = $(this).height();
		});
		$(this).find("div.article .title").css('display','none');

		var maxHeightSpecs = 0;
		$(this).find("div.article .details").each(function(){
			if($(this).height() > maxHeightSpecs)
				maxHeightSpecs = $(this).height();
		});
		$(this).find("div.article .details").height(maxHeightSpecs);
	});
	if($(".store_module .listing").length && $(".store_module > div.article > div.title").html()){
		t = $(".store_module > div.article > div.title").html();
		c = $(".store_module > div.article > div.content").html();
		$(".store_module > div.article").remove();
		a = $(".store_module").parents('.article');
		a.find('.content:first').prepend(c);
		a.find('.title:first').html(t);
	}
	
	//center image
	$(".store_module .listing div.thumb img").each(function(){
		$(this).parent().css('background-image','url(' + $(this).attr('src') + ')');
	});

	//order button
	$(".store_module div.ui .submit").mousedown(function(){
		i = $(this).parents('.ui').find('.input');
		if(!i.val()){
			i.val(1);
		}
		
		if($(this).parents('.listing').length && $(this).parents('.article_container').find('.custom_fields').length){
			o = $(this).parents('.article_container').find('.custom_fields');
			a = $(this).parents('form').attr('action');
			o.append(i.clone().css('visibility','hidden'));
			o.append('<div class="submit_but"><input type="submit" class="submit" name="cart" value="Plaats bestelling" /></div>');
			o.wrapInner('<form action="'+a+'" method="post"></form>');
			popWindow('',550,460,o);
			return false;
		}
		return true;
	});	

	//hide blank br
	$(".article .content .module_contents,#left > .module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});

	/* Image adjustments */
	$("img[align='left']").css('margin-right','15px');
	$("img[align='right']").css('margin-left','15px');

	/* Menu adjustments */
	$('#menu ul > li:first > a:first,#menu ul ul > li:first > a:first').css('border','0');
	$("#menu ul ul").wrapInner('<div class="submenu_bottom clear"><div class="submenu_top clear"></div></div>');
	$("#menu ul li").hover(
		function(){
			i = $(this).children('ul:eq(0)');
			if(i.is(":hidden"))
				i.show();
		},
		function(){
			i = $(this).children('ul:eq(0)');
			if(i.is(":visible"))
				i.hide();
		}
    );

	//change all popups...
	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;
				
				u = $(this).attr('href');
				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/boekerietje.nl/video.php?file=" + u + "',320,260);");		
		}
	});
	

});

