var Url = {

	// public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }
        return string;
    },

	// private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

	utf2win : function (str) {
		var trans = [];
		for (var i = 0x410; i <= 0x44F; i++)
		  trans[i] = i - 0x350;
		trans[0x401] = 0xA8; 
		trans[0x451] = 0xB8; 
		
		var ret = [];

		for (var i = 0; i < str.length; i++) {
			var n = str.charCodeAt(i);
			if (typeof trans[n] != 'undefined')
			  n = trans[n];
			if (n <= 0xFF)
			  ret.push(n);
		 }
		 return escape(String.fromCharCode.apply(null, ret));
	},

	urldecode : function(str) {
		var trans=[];
		var snart=[];
		for(var i=0x410;i<=0x44F;i++) {
			trans[i]=i-0x350;
			snart[i-0x350] = i;
		}
		trans[0x401]= 0xA8;
		trans[0x451]= 0xB8;
		snart[0xA8] = 0x401;
		snart[0xB8] = 0x451;

		var ret=[];
		str = unescape(str);
		for(var i=0;i<str.length;i++) {
			var n=str.charCodeAt(i);
			if(typeof snart[n]!='undefined')
				n = snart[n];
			ret.push(n);
		}
		return String.fromCharCode.apply(null,ret);
	},

	urlencode : function (str) {
										 
		var ret = str;
		
		ret = ret.toString();
		ret = encodeURIComponent(ret);
		ret = ret.replace(/%20/g, '+');
	 
		return ret;
	}
}


var PHP = {

	print_r : function(array, return_val) {    
	 
		var output = "", pad_char = " ", pad_val = 4;
	 
		var formatArray = function (obj, cur_depth, pad_val, pad_char) {
			if(cur_depth > 0)
				cur_depth++;
	 
			var base_pad = repeat_char(pad_val*cur_depth, pad_char);
			var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
			var str = "";
	 
			if(obj instanceof Array) {
				str += "Array\n" + base_pad + "(\n";
				for(var key in obj) {
					if(obj[key] instanceof Array) {
						str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
					} else {
						str += thick_pad + "["+key+"] => " + obj[key] + "\n";
					}
				}
				str += base_pad + ")\n";
			} else {
				str = obj.toString(); // They didn't pass in an array.... why? -- Do the best we can to output this object.
			};
	 
			return str;
		};
	 
		var repeat_char = function (len, char) {
			var str = "";
			for(var i=0; i < len; i++) { str += char; };
			return str;
		};
	 
		output = formatArray(array, 0, pad_val, pad_char);
	 
		if(return_val !== true) {
			document.write("<pre>" + output + "</pre>");
			return true;
		} else {
			return output;
		}
	},

	implode : function (sep, arr) {
	    return arr.join(sep);
	},

	explode : function(delim, str) {    // Split a string by separator
 
    	return str.toString().split ( delim.toString() );
	}, 

	trim : function (str) {
		return str.replace(/(^ *)|( *$)/gi, "");
	},

	strip_tags : function(html) {
	  
		html = html.replace(/[\n\r]+/gi, " ");
		// strip script and style tags
		html = html.replace(/<s(cript|tyle)[^>]*?>.*?<\/s\1>/gi, "");
		// strip comment tags
		html = html.replace(/<!--.*?-->/gi, "");
		// strip other tags
		html = html.replace(/<\/?[^>]+>/gi, " ");
		// strip entities
		html = html.replace(/&[\w]+;/gi, " ");
		// strip signs
		html = html.replace(/\.|,|:|!|\?|;| - |--|[\d]+|\/|\(|\)|'|"|_/gi, " ");
		// replace all english words
		html = html.replace(/[a-zA-Z-_]+/gi, " ");
		// trim document
		html = html.replace(/[ \s\t\f]+/gi, " ");
	
		return html;
	}
}


var Coockie = {

    getExpDate : function (days, hours, minutes) {
		var expDate = new Date();
		if (typeof days == "number" && typeof hours == "number" && typeof hours == "number") {
			expDate.setDate(expDate.getDate() + parseInt(days));
			expDate.setHours(expDate.getHours() + parseInt(hours));
			expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
			return expDate.toGMTString();
		}
    },

	getCookieVal : function (offset) {
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1) {
			endstr = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, endstr));
	},

	getCookie: function (name) {
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen) {
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg) {
				return this.getCookieVal(j);
			}
			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) break; 
		}
		return null;
	},

	setCookie : function (name, value, expires, path, domain, secure) {
		document.cookie = name + "=" + escape (value) +
			((expires) ? "; expires=" + expires : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	},

	deleteCookie : function (name,path,domain) {
		if (this.getCookie(name)) {
			document.cookie = name + "=" +
				((path) ? "; path=" + path : "") +
				((domain) ? "; domain=" + domain : "") +
				"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
	}
}


var LIB = {
	addelem : function(name, attr) {

		var tag = document.createElement(name);
		for(var key in attr) {
			tag.setAttribute(key, attr[key]);
		}		
		document.getElementsByTagName('head')[0].appendChild(tag);
	},

	sleep : function ( numberMillis ) {
        var dialogScript = "window.setTimeout( function () { window.close(); }, " + numberMillis + ");";
        var result = window.showModalDialog("javascript:document.writeln(\"<script>" + dialogScript + "<\/script>\")");
	},

	replaceslash : function(str) {
		try {
			var res = str.replace(/\//gi, "|");
		} catch(e) {
			return "";
		}
		return res;
	},

	addLoadEvent : function (func) {
	    var oldonload = window.onload;
	    if (typeof window.onload != 'function') {
			window.onload = func;
	  	} else {
		    window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
	  	}
	},

    getScrollY : function () {
		
  	    var scrOfY = 0;
	    if( typeof( window.pageYOffset ) == 'number' ) {
		    //Netscape compliant			
		    scrOfY = window.pageYOffset;
	    } else if( document.body && (document.body.scrollLeft || document.body.scrollTop ) ) {
		    //DOM compliant
		    scrOfY = document.body.scrollTop;
	    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop )) {
		    scrOfY = document.documentElement.scrollTop;
	    }
	    return scrOfY;
	},
	getHeightY : function() {
		
		var heightY = 0;
		var heightY1 = 0;
		var heightY2 = 0;

		if( document.body ) {
		    //DOM compliant
		    heightY1 = document.body.clientHeight;
	    } 
		if(document.documentElement) {
		    heightY2 = document.documentElement.clientHeight;			
	    }
		if(!heightY1) {
			return heightY2;
		}
		if(!heightY2) {
			return heightY1;
		}
		heightY = (heightY1 < heightY2) ? heightY1 : heightY2;
	    return heightY;
	},
	getBlocks : function() {
		try{		ps_reflink = ps_reflink+""; }catch(e){ps_reflink = "1";}
		var ps_params = "pid=" + ps_pid + "&cols=" + ps_cols + "&rows=" + ps_rows + "&cat=" + 
					ps_cat + "&partn=" + ps_partn + "&feed_type=" + ps_feed_type + "&feed_search=" + 
					Url.urlencode(ps_feed_search) + "&embed=" + ps_embed + "&stretch=" + ps_stretch + "&tmpl=" + 
					ps_tmpl + "&rand=" + ps_rand + "&charset=" + ps_charset + "&reflink=" + ps_reflink + "&r=" + Math.floor(Math.random()*1000);
		
		// request
		attr = Array();
		attr['type'] = "text/javascript";
		attr['language'] = "JavaScript";
		attr['src'] = "http://www.goodbody.ru/banner/goods.js?" + ps_params;
		this.addelem("script", attr);  
	}
}

var ps_div = {
	hide : function() {
		document.getElementById('ps_hidden_div_out').style.display = 'none';
	},
	show : function(href) {
		var width = document.body.clientWidth;
		var height = document.body.clientHeight;
		var elem = document.getElementById('ps_hidden_div_out');
		elem.style.width = width + 'px';				
		elem.style.top = LIB.getScrollY()+(LIB.getHeightY()-560)/2 + 'px'; 
		document.getElementById('ps_hidden_div').innerHTML = "<img src='http://www.goodbody.ru/banner/loading.gif' alt='' vspace='10'>";
		// check if user agent ie
		var ua = (navigator.userAgent.indexOf("MSIE") != -1) ? "ie": "all";
		// load order form
		var attr = Array();
		attr['type'] = "text/javascript";
		attr['language'] = "JavaScript";
		attr['src'] = href + "&ua=" + ua + "/order.js";
		LIB.addelem("script", attr);

		elem.style.display = 'block';		
	},
	order_submit : function() {

		var ps_form = document.ps_order_form;
		
		var ps_pid = ps_form.pid.value;
		var ps_partn = ps_form.partnumber.value;
		var ps_size = (ps_form.size) ? ps_form.size.value : "";
		var ps_color = (ps_form.color) ? ps_form.color.value : "";
		var ps_quant = ps_form.quant.value;
		var ps_name = LIB.replaceslash(ps_form.contact_name.value);
		var ps_addr = LIB.replaceslash(ps_form.contact_addr.value);
		var ps_email = LIB.replaceslash(ps_form.contact_email.value);
		var ps_phone = LIB.replaceslash(ps_form.contact_phone.value);
		var ps_addit = LIB.replaceslash(ps_form.additional.value);

		if(ps_quant < 1 ) {
			alert(ps_error[0]);
			return false;
		}
		if(ps_name.length < 3 ) {
			alert(ps_error[1]);
			return false;
		}
		if(ps_addr.length < 10 ) {
			alert(ps_error[2]);
			return false;
		}
		if(ps_phone.length < 7 ) {
			alert(ps_error[3]);
			return false;
		}
		
		// check if user agent ie
		var ua = (navigator.userAgent.indexOf("MSIE") != -1) ? "ie": "all";
		var ref = LIB.replaceslash(Coockie.getCookie("ps_ref"));

		var ps_params = "pid=" + ps_pid + "&size=" + Url.urlencode(ps_size) + "&color=" + Url.urlencode(ps_color) + "&quant=" + ps_quant
						+ "&name=" + Url.urlencode(ps_name) + "&addr=" + Url.urlencode(ps_addr) + "&email=" + Url.urlencode(ps_email)
						+ "&phone=" + Url.urlencode(ps_phone) + "&addit=" + Url.urlencode(ps_addit) + "&pn=" + ps_partn  
						+ "&ref=" + ref + "&charset=" + ps_charset + "&ua=" + ua + "&send=1";
		
		document.getElementById('ps_hidden_div').innerHTML = "<img src='http://www.goodbody.ru/banner/loading.gif' alt='' vspace='10'>";
		// request
		attr = Array();
		attr['type'] = "text/javascript";
		attr['language'] = "JavaScript";
		attr['src'] = "http://www.goodbody.ru/banner/submit.js?" + ps_params;
		LIB.addelem("script", attr);
	}
}

if(ps_feed_type == 'all') {
	LIB.getBlocks();
} else {
	LIB.addLoadEvent(function() {
		if(ps_feed_type == 'con') {			
			Context.get_keyword(5);
		} 
		LIB.getBlocks();	
	});
}