//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('fast', function () {
							dialog.container.show('fast', function () {
								dialog.data.slideDown('fast'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('fast', function () {
						  dialog.container.hide('fast', function () {
							dialog.overlay.fadeOut('fast', 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); 
}


var foundOnloadAlerts = new Array();
var alert = function(str){
	foundOnloadAlerts[foundOnloadAlerts.length] = str;
}


$(document).ready(function(){

	//hide blank br
	$(".article .content .module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});
	
    $("#menu ul li").hover(
		function(){
			i = $(this).children('ul:eq(0)');
			if(i.is(":hidden")){
				if(!i.hasClass('moved')){
					l = $(this).position().left - $('#menu').position().left;
					i.width(i.width() - l).css({paddingLeft:l + 'px',marginLeft:(l * -1) + 'px'}).addClass('moved');
				}
				i.slideDown('slow');
			}
		},
		function(){
			i = $(this).children('ul:eq(0)');
			if(i.is(":visible"))
				i.slideUp('slow');
		}
    );
	$('#menu ul > li:first > a:first,#menu ul ul > li:first > a:first').css('border','0');

	if(!$("#content .right .article.default:eq(0) div.title:eq(0)").text()){
		t = $("#content .right div.title:eq(1) h1.title").html();
		if(!$("#content .right div.title:eq(1) h1.title").text())
			t = "Webshop";
			
		$("#content .right .article.default:eq(0) div.title:eq(0) h1.title").html(t + '&nbsp;');
		if($("#content .right div.title:eq(1) h1.title").text())
			$("#content .right div.title:eq(1)").remove();
	}
	if(!$("#content .left .article.default:eq(0) div.title:eq(0)").text()){
		$("#content .left .article.default:eq(0) div.title:eq(0) h1.title").html('&nbsp;');
	}

	$(".shop_categories a").removeClass('clear');

	//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('.prices').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 price line.
	if($(".store_module div.item_details .prices .price_line").length != 1){
	//	$(".store_module div.article .prices").height($("#middle .store_module .item_details .prices .price_line:eq(0)").height()).css('overflow','hidden');
		$(".store_module div.article .prices .price_line").hide().find('input').attr('disabled','disabled');
		
		$(".store_module div.article").each(function(){
			var art = $(this);
	
			//build select menu
			var opts = $('<select name="selector"><option value="">- selecteer -</option></select>').change(function(){
				i = $(this).val();
				if(i){
					$(this).next().html(art.find(".prices .price_line:eq(" + i + ") div.ui").html());
					$(this).next().find('input').removeAttr('disabled');
				}
				//order button
				art.find(".price_selector .submit").mousedown(function(){
					o = $(this).parents('.price_selector').find('.input');
					if(!o.val())
						o.val(1);
				});
			});
			var p;
			art.find(".prices .price_line").each(function(){
				p = $(this).find('span.new_price').html();
				t = $(this).children('div.price').find('span.article').text();
				opts.append($('<option value="' + art.find(".prices .price_line").index($(this)) + '">' + t + '</option>'));
			});
			art.find(".prices").prepend($('<div class="price_selector"><span class="selector_ui"></span></div>')).find('.price_selector').prepend(opts);
			art.find("div.specs").append($('<div class="spec_item spec_item_"><div class="spec spec_line_75 spec_value"><label>Price:</label> <span class="value">' + p + '</span></div></div>'));
			art.find(".prices").prepend($('<div class="new_price" style="">Price: <h2 style="display:inline;">'+ p + '</h2></div>'));
			opts.trigger('change');
		});
	}
	
	//change shop scroller.
	$("#shop_scroller").append('<div class="popInfoBox"></div>');
	$("#shop_scroller div.items").prepend('<div class="left"></div>').append('<div class="right"></div>');
	shop_scroller_AddItemHover = function(){
		$("#shop_scroller div.items a.item").hover(
			function(){
				id = $(this).attr('id');
				o = $(this).offset();
				pib = $("#shop_scroller .popInfoBox").clone();
				$("#shop_scroller .items").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 .items .popInfoBox').remove();
				if($(this).hasClass('faded'))
					$(this).fadeTo('slow',0.3);
			}
		);
		$("#shop_scroller .item_info").wrapInner('<div class="block"></div>');
	}
	shop_scroller_AddItemHover();
	$("#shop_scroller .items div.right").click(function(){
		x = $("#shop_scroller div.scrollContent a.item:eq(0)").outerWidth() * 4;
		$("#shop_scroller div.scrollContent").animate({'left':(x * -1) + 'px'}, "slow","swing",function(){
			$("#shop_scroller div.scrollContent a.item:lt(4)").appendTo("#shop_scroller div.scrollContent");
			$("#shop_scroller div.scrollContent").css('left','0px');
		});
	});
	$("#shop_scroller .items div.left").click(function(){
		x = $("#shop_scroller div.scrollContent a.item:eq(0)").outerWidth() * 4;
		i = $("#shop_scroller div.scrollContent a.item").length - 5;
		$("#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(){
		});
	});
	$("#shop_scroller .categories a.category").click(function(e){
		e.preventDefault;
		i = o = s = null;
		$("#shop_scroller .categories a.current_categoryID").removeClass('current_categoryID');
		$(this).addClass('current_categoryID');
		h = $(this).attr('href');
		if(h.indexOf('categoryID') > 0)
			i = h.match(/categoryID=(\d+)/)[1];
		if(h.indexOf('on_sale') > 0)
			o = parseInt(h.match(/on_sale=(\d+)/)[1]);
		if(h.indexOf('shopID') > 0)
			s = h.match(/shopID=(\d+)/)[1];
		
		url = '/domains/huset-design.nl/modules/item_listing/listing.php?shopID=' + s + '&' + (o ? 'on_sale=1' : 'categoryID=' + i);
		
		$("#shop_scroller div.items .scrollContent").load(url,shop_scroller_AddItemHover);

		return false;
	});

	//my own alert...
	alert = function(str){
		popWindow('',300,'',$('<div id="alert clear"><span class="string">' + str + '</span></div>'));
	}
	//show all found alerts
	if(foundOnloadAlerts.length > 0){
		$.each(foundOnloadAlerts,function(){
			alert(this);
		});
	}
	
	//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('" + img[id].src + "'," + w + "," + h + ");");
					}
					img[id].src = u;
				}else{
					c = $(this).attr('onclick').toString();
					h = 600;
					w = 800;
					if(c){
						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('" + $(this).attr('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/huset-design.nl/video/video.php?" + u + "',300,240);");		
		}
	});

});