//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); 
}


$(document).ready(function(){
	
	$('#left .shop_categories_module ul li a').addClass('clear');
	
	//reset search box
	$('#left .search_module input.input').val('ZOEKEN').focus(function(){
		if(!$(this).hasClass('focus')){
			$(this).addClass('focus').val('');
		}
	}).attr('title','Geef uw zoek opdracht en druk op enter...');
	
	//change menu
	$('.shop_categories_module ul li a').removeClass('clear');
/*	$('.shop_categories_module > ul > li > a').click(function(e){
		e.preventDefault();
		if($(this).parent().children('ul').length){
			$(this).parent().children('ul').slideDown('fast');
		}else{
			return true;
		}
		return false;
	});
*/	$('.shop_categories_module ul li a.current_category').parent().find('ul').show();
	
	$('#left .spec_group_menus .spec_group_menu select').change(function(e){
		$(this).parents('form').trigger('submit');
	});
	
	//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.hasClass('order_button') && !$target.hasClass('input') && !$target.hasClass('size'))
			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);
		}
	});


	var getPriceSelection = function(){
		size = $(this).find('select.size').val();
		
		$(this).parents('.prices').children('.price_line').each(function(){
			if($(this).find('.article').text() == size){
				$(this).find('.dynPriceUI').html($(this).find('span.price').html());//.replace(/(.*)(,[0-9]{2})/,'$1<sup>$2</sup>'));
			}
		});
	}

	//build UI menus
	var buildPriceUI = function(obj){
		obj.each(function(){
			var selectedPrice = "";
			var sizeSelect = $('<select class="size" />').change(getPriceSelection);
			$(this).children('.price_line').not('.dummy').each(function(){
				//build size
				v = $(this).find('.article').text();
				if(!sizeSelect.find('option[value="' + v + '"]').length){
					o = $("<option />").val(v).text(v);
					sizeSelect.append(o);
				}
				
				t = $(this).find('span.price').html();
				if(t)
					selectedPrice = t;//.replace(/(.*)(,[0-9]{2})/,'$1<sup>$2</sup>');
			});
			f = $('<form />').submit(function(e){
				e.preventDefault();
				
				am = $(this).find('input.input').val();
				if(!am)
					am = 1;
				size = $(this).find('select.size').val();
				
				l = $(this).parents('.prices').children('.price_line');
				if(l.length > 1){
					l.each(function(){
						if($(this).find('.article').text() == size){
							o = $(this).find('.amount input').attr('name');
							self.location.href='/winkelmand?' + o + '=' + am;
						}
					});
				}else{
					o = $(this).parents('.prices').find('.amount input').attr('name');
					self.location.href='/winkelmand?' + o + '=' + am;
				}
				return false;
			});

			if($(this).children('.price_line').not('.dummy').length){
				f.append(sizeSelect);

				f.append('<input type="text" class="input" value="" size="2" /><input type="submit" class="order_button" value="Bestel" /><span class=\"dynPriceUI clear\">' + selectedPrice + '</span>');
				$(this).append(f);
			}
		});
	}
	buildPriceUI($('.store_module div.article .prices'));

	if($('#container.home').length){
		$('#middle .store_module .listing form:eq(1)').prepend($('<div class="mainAdd" />'));
	}

	//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');//.replace(/(\/db\/.*\/)(.*)$/,'$1popup.$2');
				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').toString();

					h = parseInt(c.match(/height=([0-9]+)/)[1]) + 25;
					w = parseInt(c.match(/width=([0-9]+)/)[1]) + 25;
	
					$(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/huijgsport.nl/video.php?file=" + u + "',320,260);");		
		}
	});

})
