//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.show();
						//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
								$('#alert,.video_details').remove();
							
							});
						  });
						//});
					}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
}




var console;
$(document).ready(function(){
	
	//change layouts
	$(".menu").each(function(){
		$(this).find("a:eq(0)").addClass('first');
	});
	if($("#topbar li").length == 2){
		$("#topbar li:eq(1)").addClass('right');
	}


	var resizeMenu = function(obj){
		obj.find('ul:eq(0)').css('left',obj.parent().offset().left + 'px');
	}

	//valign button texts..
	$("#left .leftmenu ul li a,#left .shop_categories_module ul li a span,#left .catalog_category_menu_module ul li a").wrapInner("<span></span>");
	$("#left .leftmenu ul li a span,#left .shop_categories_module ul li a span,#left .catalog_category_menu_module ul li a span").each(function(){
		$(this).css('padding-top',parseInt(($(this).parent().height() - $(this).height()) / 2) + 'px');
	});
	
	
	$("#menu > ul > li").mouseover(function(){
	   if($(this).find("ul").length && !$(this).hasClass('open')){
			if($("#menu > ul > li.open").length){
				$("#menu > ul > li.open > ul").fadeOut('normal',function(){
					$(this).parent('li').removeClass('open');
				});
			}
			resizeMenu($(this));
			$(this).find('a:first').addClass('open');
			$(this).addClass('open').find('ul:first').fadeIn('normal');
			$("#content").animate({marginTop:'30px'},'normal');
	   }
	});
	$("#menu ul ul").each(function(){
		$(this).find('ul').remove();
		$(this).find("a:last").addClass('last');
	});
	if($.browser.msie){
		$("#menu").css('padding-bottom','0px');
		if($("#middle.right_middle").length)
			$("#left,#middle.right_middle").css('margin-top','10px');
	}
	if($("#menu a.current_buttonID").length){
		$("#menu a.current_buttonID").parents('li:eq(0)').trigger('mouseover');
	}
	
	$("#middle.right_middle .catalog_module .listing form.option_menu select").change(function(){
		$(this).parents('form').submit();
	});

	
	$(window).resize(function(){
		resizeMenu($("#menu ul li.open:eq(0)"));
	});
	

	var last_item_height = 0;
	var last_title_height = 0;
	var last_options_height = 0;
	var last_item_nr = 0;
	$("#middle.right_middle .catalog_module .listing .item").each(function(){
		if($(this).find('div.title:eq(0)').height() > last_title_height)
			last_title_height = $(this).find('div.title:eq(0)').height();
		if($(this).find('div.options:eq(0)').height() > last_options_height)
			last_options_height = $(this).find('div.options:eq(0)').height();			
		if($(this).height() > last_item_height)
			last_item_height = $(this).height();
			
		$(this).addClass('item_nr_' + last_item_nr);
		
		last_item_nr++;
		c = $("#middle.right_middle .catalog_module .listing .item").index(this);
		if(last_item_nr > 2 || c == $("#middle.right_middle .catalog_module .listing .item").length - 1){
			for(i=c;i>c - 3;i--){
				$("#middle.right_middle .catalog_module .listing .item:eq(" + i + ") div.title:eq(0)").height(last_title_height);
				$("#middle.right_middle .catalog_module .listing .item:eq(" + i + ") div.options:eq(0)").height(last_options_height);
			}
			last_options_height = last_title_height = last_item_nr = 0;
		}
	});
	$("#middle.right_middle .catalog_module .listing .item").height(last_item_height);


	//kill MS word stuff!
	$(".MsoNormal,.MsoNormal *").removeAttr('style');

	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}
	
	//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').indexOf('module=flash_flv_player') > -1){
			//get filename...
			u = $(this).attr('href').match(/(file=).*(.flv)/)[0];
			$(this).addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/implantdirect.nu/popup/video.php?" + u + "',300,240);");		
		}
	});
	
	
	if($('.video_module .video_file').length){
		$('.video_module .video_file a').click(function(e){
			e.preventDefault();
			
			$.get($(this).attr('href'),function(data){
				f = $(data).find('.video_details');
				f.css('visibility','hidden').appendTo('body');
				w = parseInt(f.find('#mediaplayer_wrapper').width()) + 20;
				h = f.height() + 80;
				f.css('visibility','visible');
				popWindow('',w,h,f);
			});
		
			return false;
		});
	}
});
