// common js stuff...
$(document).ready(function(){

	//hide blank br
	$(".article .content .module_contents").each(function(){
		if($(this).prev('br').length == 1)
			$(this).prev('br').css('display','none');
	});
	
	//submenus
	$(".shop_categories_module ul li").hover(
		function(){
			if($(this).children('ul:eq(0)').is(':hidden'))
				$(this).children('ul:eq(0)').css('display','block');
		},
		function(){
			if($(this).children('ul:eq(0)').is(':visible'))
				$(this).children('ul:eq(0)').css('display','none');
		}
    );
	
	//fix colom margin and height
	var c = 0;
	var h = 0;
	$("div.article_container").each(function(){
		if($(this).hasClass("colom")){
			if($(this).find(".bottom:eq(0)").height() > h){
				h = $(this).find(".bottom:eq(0)").height();
				for(i=0;i<c;i++)
					$(this).prevAll('.colom:eq(' + i + ')').find(".bottom:eq(0)").height(h);
			}
			$(this).find(".bottom:eq(0)").height(h);

			if(!c)
				$(this).css('margin-left','0');
			else if(c == 2)
				h = c = 0;
			c++;
		}else if($(this).hasClass("whole")){
			c = 0;
		}
	});

	$("#left .shop_manufacturers_module, #left .shop_categories_module, #left .shop_specifications_module, #right .shop_manufacturers_module, #right .shop_categories_module, #right .shop_specifications_module").parents('.article_container').addClass('shop_menu').wrapInner('<div class="top"><div class="bottom clear"></div></div>');

	//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").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);
	});
	$("table.tracks tr").find("td:eq(0)").addClass('nr');
	$("table.tracks tr:odd").addClass('odd');
	$("table.tracks tr:even").addClass('even');
	$("table.tracks tbody").prepend("<tr><td colspan='3' class='header'>Tracks:</td></tr>");
	$(".store_module .listing div.article").each(function(){
		$(this).find(".price_line:gt(0)").hide();
	});
	$(".store_module .listing .spec_group_menu select").change(function(){
		this.form.submit();
	});
	
	//change all popups...
	var img = new Array();
	$(".article a").each(function(){
		s = "dow.open(this.href,";
		i = 0;
		if($(this).attr('onclick'))
			i = $(this).attr('onclick').toString().indexOf(s);
	   
		if(i){
			w = 640;
			h = 480;
		   
			u = $(this).attr('href');
			var id = 'popwinlink_' + uid();
			e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		   
			$(this).attr('id',id);
			if(e == 'png' || e == 'gif' || e == 'jpg'){
				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 = $("a#" + id).attr('onclick');
				c = $("a#popwinlink_0").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(','));
			   
				$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
			}
		}
	});
		
	//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);
	});
	
	//strip extra text random articles
	$(".random_article_module .random_info").each(function(){
		t = $(this).text();
		p = t.match(/([^ ]+)/);
		txt = "";
		for(i=1;i<30;i++)
			if(p.length && p[i] != null)
				txt+= p[i] + ' ';
		txt.substring(0,200);
		txt+= (p.length >= 30 ? '...' : '')
		$(this).text(txt);
	});
	
	$("#middle .random_article_module").each(function(){
		var maxHeightTitle = 0;
		$(this).find(".random_article").each(function(){
			if($(this).height() > maxHeightTitle)
				maxHeightTitle = $(this).height();
		});
		$(this).find(".random_article").height(maxHeightTitle);
	});
	
});

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

//emulate popup function...
function popWindow(u,w,h){
	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>");
	else
		popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>");
	popEl.modal({
		onOpen: function (dialog) {
					dialog.overlay.fadeIn('slow', function () {
						dialog.container.slideDown('slow', function () {
							dialog.data.fadeIn('slow'); // See Other Notes below regarding
													   // data display property and
													   // iframe details
						});
					});
				},
		onClose: function (dialog) {
					dialog.data.fadeOut('slow', function () {
					  dialog.container.slideUp('slow', function () {
						dialog.overlay.fadeOut('slow', function () {
						  $.modal.close(); // must call this to have SimpleModal
										   // re-insert the data correctly and
										   // clean up the dialog elements
						});
					  });
					});
				}
				
	});
	$("#modalContainer").css('height',h).css('width',w).css('margin-left',(w / 2) * -1);
}