//enable firebug
var console;

//make uniqID
var vuid=0;
var uid = function(){
	return vuid++ ;
}

//dummy
var popInfo = function(){void(0);}


//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)
	if(h)
		$("#modalContainer").css('height',h);
	else
		h = $("#modalContainer").height();
	$("#modalContainer").css('margin-top',(h / 2) * -1);
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1);
	//.css('height',h).css('margin-top',(h / 2) * -1); 
}

function jumpScroll(){
	if($("#shop_scroller").length && !$("#shop_scroller").hasClass('stopped')){
		$("#shop_scroller").fadeTo(8000,1,function(){
			scrollRight(1);
			jumpScroll();
		});
	}
}

function scrollRight(scroll_view_amount){
	x = $("#shop_scroller div.scrollContent a.item:eq(0)").width() * scroll_view_amount;
	$("#shop_scroller div.scrollContent").animate({'left':(x * -1) + 'px'}, "slow","swing",function(){
		$("#shop_scroller div.scrollContent a.item:lt(" + scroll_view_amount + ")").appendTo("#shop_scroller div.scrollContent");
		$("#shop_scroller div.scrollContent").css('left','0px');
		jumpScroll();
	});
}

//start scripts
$(document).ready(function(){
	
	//kill empty titles.
	$("#left div.title,#right div.title").each(function(){
		if(!$(this).text()){
			$(this).remove();
		}
	});

	//hide blank br
	$(".article .content .module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});
	
	//hide title of menus
	$("#left .leftmenu").each(function(){
		$(this).parents('.article').find('div.title').remove();
	});
	
	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}

//    $("div.submenu_module ul li").hover(
//		function(){
//			i = $(this).children('ul:eq(0)');
//			if(i.is(":hidden")){
//				i.slideDown('normal');
//			}
//		},
//		function(){
//			i = $(this).children('ul:eq(0)');
//			if(i.is(":visible")/* && !$(this).find('.current_buttonID').length*/)
//				i.slideUp('normal');
//		}
//    );

	//fix main selected button...
	$("div.submenu_module li.current_buttonID").parents('li').addClass('current_buttonID');
	$('.current_buttonID').parents('li').find('a').addClass('current_buttonID');


	$('img[align="left"]').css('margin-right','5px');
	$('img[align="right"]').css('margin-left','5px');
	
	//change all popups...
	var img = new Array();
	//change all popups...
	var scanForPopLinks = function(o){
		o.each(function(){
			i = 0;
	
			//change all onclick popups
			if($(this).attr('onclick')){
				i = $(this).attr('onclick').toString().indexOf("dow.open(this.href,");
	
				if(i){
					u = $(this).attr('href');
					e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
					
					if(e == 'png' || e == 'gif' || e == 'jpg'){
						w = 640;
						h = 480;
						
						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('" + img[id].src + "'," + w + "," + h + ");");
						}
						img[id].src = u;
					}else{
						w = h = 0;
						c = $(this).attr('onclick').toString();
						wr = c.match(/width=(\d*)/);
						hr = c.match(/height=(\d*)/);
						if(wr && wr.length)
							w = parseInt(wr[1]);
						if(hr && hr.length)
							h = parseInt(hr[1]);
						if(!h)
							h = 600;
						if(!w)
							w = 800;
		
						href = $(this).attr('href');
						if(href.substr(0,10) == '/popup.php'){
							href = '/domains/metalart.nl' + href;
						}
						$(this).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + href + "'," + w + "," + h + ");");				
					}
				}
	
			//change all movie files
			}else if($(this).attr('href') && $(this).attr('href').indexOf('module=flash_flv_player') > -1){
				//get filename...
				u = $(this).attr('href').match(/(file=).*(.flv)/)[0];
				$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/metalart.nl/popup/video.php?" + u + "',300,240);");		
			}
		});

	
		var i = 0;
		$(".store_module .listing div.article").each(function(){
			if(i == 5){
				$(this).css('margin-right','0px');
				i = 0;
			}
			i++;
			$(this).find(".price_line:gt(0)").hide();
		});
		$(".store_module .listing .spec_group_menu select").change(function(){
			this.form.submit();
		});
		$(".store_module .item_details .article .title").wrapInner('<h2 class="title"></h2>');
		$(".shop_categories_module a,.shop_specifications_module a").removeClass('clear');
		
		/*
		if($('.store_module .item_details div.prices input').length){
			o = $('<a href="javascript:void(0);">bekijk prijslijst &raquo;</a>').css('float','right').click(function(){
				t = '';
				if($('.store_module div.tax').length){
					t = $('.store_module div.tax').html();
				}
				$(this).parents('.footer').css('font-size','10px').html(t);
				$('.store_module .item_details div.prices').slideDown('normal');
			});
			$('.store_module .item_details div.prices').hide();
			$('.store_module .item_details').parents('.article').children('.footer').append(o);
		}
		*/
		
		//store aticle click
		$(".store_module .listing div.article").hover(
			function(){$(this).addClass('hover');},
			function(){$(this).removeClass('hover');}		
		).click(function(e){
			var $target = $(e.target);
			if(!$target.parents('.ui').length)
				self.location.href = $(this).find(".thumb a").attr('href');
		});
		
		//order button
		$(".store_module div.ui .submit").mousedown(function(){
			o = $(this).parents('.ui').find('.input');
			if(!o.val())
				o.val(1);
		});
	
		//make every line same height
		if(!$("#middle .article .item_details").length){
			var templs = new Array();
				templs['default'] = 100;
				templs['half'] = 50;
				templs['thirth'] = 33;
				templs['quater'] = 25;
			var line_total = 0;
			var row_total = 0;
			var row_height = 0;
			$("#middle div.article.template").each(function(){
				nc = "";
				if($(this).next().length && $(this).next().next().length){
					nc = $(this).attr('class').split(' ')[1];
				}
				
				$(this).addClass('row_' + row_total);
		
				if($(this).height() > row_height){
					row_height = $(this).height();
				}
		
				line_total = line_total + templs[nc];
				if(line_total >= 100 || $("#middle .article").index(this) == ($("#middle .article").length - 1)){// || !nc){
					$(".article.template.row_" + row_total).height(row_height);
					$(".article.template.row_" + row_total + ":first").css('clear','left');
					row_height = line_total = 0;
					row_total++;
				}
			});
		}
	}
	scanForPopLinks($(".article a"));
		
	//change shop scroller.
	$("#shop_scroller div.article").append('<div class="popInfoBox"></div>').append('<div class="left"></div>').append('<div class="right"></div>');
	shop_scroller_AddItemHover = function(){
		$("#shop_scroller a.item").hover(
			function(){
				id = $(this).attr('id');
				o = $(this).offset();
				pib = $("#shop_scroller .popInfoBox").clone();
				$("#shop_scroller").append(pib);
				pib.attr('id','pib_' + id).css('left',o.left).html($('div.' + id).html());
				pib.fadeIn('slow');
				
				if($(this).hasClass('faded'))
					$(this).fadeTo('slow',1);
			},
			function(){
				$('#shop_scroller .popInfoBox').remove();
				if($(this).hasClass('faded'))
					$(this).fadeTo('slow',0.3);
			}
		);
		$("#shop_scroller .item_info").wrapInner('<div class="block"></div>');
	}
//	shop_scroller_AddItemHover();
	var scroll_view_amount = 5;
	$("#shop_scroller div.right").click(function(){
		scrollRight(scroll_view_amount);
	});
	$("#shop_scroller div.left").click(function(){
		x = $("#shop_scroller div.scrollContent a.item:eq(0)").width() * scroll_view_amount;
		i = $("#shop_scroller div.scrollContent a.item").length - (scroll_view_amount + 1);
		$("#shop_scroller div.scrollContent a.item:gt(" + i + ")").prependTo("#shop_scroller div.scrollContent");
		$("#shop_scroller div.scrollContent").css('left',(x * -1) + 'px');
		$("#shop_scroller div.scrollContent").animate({'left':'0px'}, "slow","swing",function(){
			jumpScroll();
		});
	});
	
	//change click to ajax call
	$("#shop_scroller a.item").click(function(e){
		e.preventDefault();
		$("#shop_scroller").addClass('stopped');
		$('#middle').load($(this).attr('href') + ' #middle',function(){
			scanForPopLinks($("#middle .article a"));
		});
		
		return false;
	});
	
	//rebuild store listing
	$(".store_module .listing div.article").addClass('article_container').addClass('clear');
	$(".store_module .listing .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").height(maxHeightTitle);
	//
	//	var maxHeightSpecs = 0;
	//	$(this).find("div.article .details").each(function(){
	//		if($(this).height() > maxHeightSpecs)
	//			maxHeightSpecs = $(this).height();
	//	});
	//	$(this).find("div.article .details").height(maxHeightSpecs);
	//});

	
	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);	
	}
	
	make_same_height(".store_module .listing div.article .title");
	make_same_height(".store_module .listing div.article .details");
	make_same_height(".store_module .listing div.article .prices");
	make_same_height(".store_module .listing div.article");
	
	jumpScroll();
});
