/**
* Common JS
*/

function trim(str){
	if(!str || typeof str != 'string')
		return null;
	return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}

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

// start scripts
var global = this; 
$(document).ready(function(){

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


	//add menu
	$("<img width=\"901\" height=\"140\" src=\"/images/spacer.gif\" usemap=\"#menu\" class=\"menuMapper\" border=\"0\" />").insertAfter("#menu");
	$("map#area area").each(function(){
		i = $(this).attr('href').split('pos')[1];
		$(this).attr('href',$("#menu ul:eq(0)>li>a:eq(" + i +")").attr('href'));
	});

	//left template
	if(!trim($("#submenu").html()))
		$("#submenu").remove();
	$("#left div.article, #submenu").wrapInner('<div class="top clear"><div class="bottom clear"></div></div>');

	//submenu
	$(".shop_categories_module ul li:first,.shop_manufacturers_module ul li:first, #submenu ul li:first").css('border','none');
	//.shop_categories_module ul li, .shop_manufacturers_module ul li, 
	$("#submenu 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');
		}
	);

	//add left padding...
	$("#left div.article").each(function(){
		if($(this).find('ul').length == 0)
			$(this).addClass('article_padding');
	});

	//forms
/*
	$("form input:submit, input:button").addClass("roundedButton").wrap("<div class=\"roundedButton\"><div class=\"roundedButton_left\"><div class=\"roundedButton_right\"></div></div></div>");
	$('form div.roundedButton').click(function(){
		var onClickAttr = $(this).find('input').attr('onclick');
		if(!onClickAttr && $(this).find('input').attr('type') == 'submit'){
			n = $(this).find('input').attr('name');
			v = $(this).find('input').attr('value');
			if(n)
				$('<input type="hidden" value="' + v + '" name="' + n + '" />').appendTo($(this).find('input').parent());
			$(this).parents("form")[0].submit();
		}else{
			if(window.execScript){
				window.execScript('(' + onClickAttr.toString() + ')();');
			}else{
				(global.eval) ? global.eval(onClickAttr) : eval(onClickAttr);
			}
		}
	});
    $("form input:submit, input:button").css('display','none').each(function(){
		s = $(this).val();
		i = $(this).parent().html();
		$(this).parent().html(i + "<div class=\"roundedButtonBack\">" + s + "</div>");
	});
*/
	$("form input:text,form input:password").addClass("roundedInput").wrap("<div class=\"roundedInput\"><div class=\"roundedInput_left\"><div class=\"roundedInput_right\"></div></div></div>")
	$("form textarea").addClass("roundedArea").addClass("clear").attr('style','').wrap("<div class=\"roundedArea clear\"><div class=\"roundedArea_top\"><div class=\"roundedArea_bottom\"><div class=\"roundedArea_left\"><div class=\"roundedArea_right\"><div class=\"roundedArea_tl\"><div class=\"roundedArea_tr\"><div class=\"roundedArea_bl\"><div class=\"roundedArea_br\"></div></div></div></div></div></div></div>");

	//change site search value
	$("#site_search .input").val('search').focus(function(){
		if($(this).val() == 'search')
			$(this).val('');
	}).blur(function(){
		if(!$(this).val())
			$(this).val('search');
	});


});