//utils
strTrim = function(str){return
(str.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

//init stuff

$(function(){
	$('div.module1 img.bgimg').each(function(i,o){
		$(o).css('height',$(o).next().outerHeight(true)+"px");
	});
	$('div.module2 img.bgimg').each(function(i,o){
		$(o).css('height',(25+$(o).next().outerHeight(true))+"px");
	});
	setTimeout(function(){$('div.module2 img.bgimg').each(function(i,o){
		$(o).css('height',(25+$(o).next().outerHeight(true))+"px");
	});},1500);
	$('div.pbox img.bgimg').each(function(i,o){
		$(o).css('height',"165px");
	});
	$('div.module2 div.wrapper table:first').each(function(i,o){
		if ($(o).attr('width')=="100%")
			$(o).attr('width','514px');
	});
	$('input.smallInput').each(function(i,o){
		original=$(o).parent().html();
		$(o).replaceWith("<table cellpadding='0' cellspacing='0' border='0' class='siWrapper'><tr><td class='siLeft'>&nbsp;</td><td class='siBody'>"+original+"</td><td class='siRight'>&nbsp;</td></tr></table>");
	});
	$('input.blackSmallInput').each(function(i,o){
		original=$(o).parent().html();
		$(o).replaceWith("<table cellpadding='0' cellspacing='0' border='0' class='bsiWrapper'><tr><td class='siLeft'>&nbsp;</td><td class='siBody'>"+original+"</td><td class='siRight'>&nbsp;</td></tr></table>");
	});
	
	$("div#notLoggedInDialog").dialog({
			modal:true,
			show:"fade",
			title:"Who are you?",
			autoOpen:false,
			buttons:{"Ok":function(){$("div#notLoggedInDialog").dialog("close");}}
		});
	
	$("div#newsletter_message").dialog({
			modal:true,
			show:"fade",
			title:"Newsletter",
			autoOpen:false,
			buttons:{"Ok":function(){$("div#newsletter_message").dialog("close");}}
		});
	
	window.radio=new Object();
	$('input.eu_radio').each(function(i,o){
		var name=$(o).attr('name');
		var value=$(o).attr('value');
		var checked=$(o).attr('checked');
		var onc=$(o).attr('fn').toString().replace('"',"'","g");
		var st=$(o).attr('style');
		//alert(onc);
		$(o).replaceWith('<div class="eu_radio'+(checked?' eu_radio_checked':'')+'" name="'+name+'" value="'+value+'" onclick="eu_radio_handle(this);'+onc+'" style="'+st+'">&nbsp;</div>');
		
		if (!window.radio[name])
			window.radio[name]='';
		if (checked)
			window.radio[name]=value;
	});
	
	$("input.trSearchField").focus(function(){
		if ($(this).val()=="Search"){
			$(this).val('');
		}
		$(this).css("color","#333");
	});
	$("input.trSearchField").blur(function(){
		if ($(this).val()==""){
			$(this).val('Search');
		}
		$(this).css("color","#555");
	});
	
	//ibox1 fix for chrome/safari
	$("table.ibox1").each(function(i,o){
		$(o).find(".l,.r").css("height",(-7+$(o).find(".center").children().first().outerHeight())+"px");
	});
	/**/
});

function eu_radio_handle(o){
	var name=$(o).attr('name');
	if ($(o).hasClass("eu_radio_checked")) return false;
	$("div[name="+name+"].eu_radio_checked").removeClass("eu_radio_checked");
	window.radio[name]=$(o).attr('value');
	$(o).addClass("eu_radio_checked");
	//if (typeof($("input[name="+name+"]").val)!="undefined")
		$("input[name="+name+"]").val(window.radio[name]);
}

function resize_product_thumb(o){
	h=$(o).height();
	w=$(o).width();
	
	if (h>w){
		$(o).css("height","109px");
		$(o).css("margin-left",((109-$(o).width())/2)+"px");
	}else{
		$(o).css("width","109px");
		$(o).css("margin-top",((109-$(o).height())/2)+"px");
	}
	
	if ($(o).css('opacity')=='0')
		$(o).animate({'opacity':'1','filter':'alpha(opacity=100)'},1000);
	
	o=$(o).closest("div.body").find("img.bgimg");
	$(o).css('height',($(o).next().outerHeight(true))+"px");
}

function ourSuppliersBgHeight(o){
	var $img=$(o).closest("div.wrapper").prev();
	$img.css("height",$img.next().outerHeight(true)+"px");
	if ($("div.PPDropdown").length>0)
		$("div.PPDropdown").css({"left":($("div.PPSelect").offset().left+1)+"px","top":($("div.PPSelect").offset().top+$("div.PPSelect").outerHeight(true)-6)+"px"});
}

function addToCart(pid,baseURL,nrbuc){
	if (!window.logged){
		$("div#notLoggedInDialog").dialog("open");
		return false;
	}
	if (!nrbuc) nrbuc=$("input#nrbuc"+pid).val();
	window.location=baseURL+"mycart/add_"+pid+"_"+nrbuc;
	return false;
}

function subscribe(email,baseURL){
	$.ajax({
		url:baseURL+"ajax.php",
		data:{'module':'newsletter','action':'subscribe','email':email},
		type:'get',
		error: function(){
			$("div#newsletter_message").html("Our ajax service encountered an error. Please try again later. Thank you.").dialog("open");
		},
		success: function(r){
			$("div#newsletter_message").html(r).dialog("open");
		}
	});
}
