var oGRI = {
    gMapKey:'ABQIAAAA9wdbbhBA8Yh85p07mNuv5BRxT52i47HGkUgHc97o5Hbs4gAK0hTPmdFweWUVkEpGG-KZUXSEXfJ7Mw'
}
var _oRI, cRI;
var doOnLoadList = new Array();
var doTheEndList = new Array();
window.onerror = function() {
    return true;
}

function RI() {
	var r="",a=arguments, l=a.length, f, c, i;
	if (!_oRI) {
		_oRI = new cRI();
	}
	if (l) {
		f = a[0]+'#'+l;
		if (!_oRI[f] && _oRI[a[0]]) {
			c = "return this."+a[0]+"(";
			for (i=1; i<l; i++) {
				c += (i!=1?", ":"")+"a["+i+"]";
			}
			c += ")";
			_oRI[f] = new Function("a", c);
		}
		if (_oRI[f]) {
			r = _oRI[f](a);
		}
	}
	else {
		r = 0;
	}
	return r;
}

cRI = function() {
	this._bUa = navigator.userAgent.toLowerCase();
    this._bOp = (this._bUa.indexOf("opera") != -1 ? 1 : 0);
    this._bIe = (this._bUa.indexOf("msie") != -1 && !this._bOp ? 1 : 0);
    this._bIe4 = (this._bIe && (this._bUa.indexOf("msie 2.") != -1 || 
    this._bUa.indexOf("msie 3.") != -1 || this._bUa.indexOf("msie 4.") != -1) && !this._bOp ? 1 : 0);
    this._bKh = (this._bUa.indexOf("khtml") != -1 ? 1 : 0);
    this._bGe = (this._bUa.indexOf("gecko") != -1 && !this._bKh ? 1 : 0);
    this._bN4 = (document.layers ? 1 : 0);
    this._bMc = (this._bUa.indexOf("mac") != -1 ? 1 : 0);
}

cRI.prototype = {
    isOpera: function() { return this._bOp; },
    isGecko: function() { return this._bGe; },
    isOldNetscape: function() { return this._bN4; },
    isMac: function() { return this._bMc; },
    isLinux: function() { return (this._bUa.indexOf("linux")!=-1) },
    isKhtml: function() { return this._bKh; },
    isIE: function() { return (this._bIe == 1 || this._bIe4 == 1) ? 1 : 0; },
    setHTML: function(id, txt) { document.getElementById(id).innerHTML=txt; },
    write: function(txt) { document.write(txt); },
	$: function(id) {
		try { return document.getElementById(id);}
		catch(oError) {}
	},
	getCssClass: function(id) {
		return this.$(id).className;		
	},
	setCssClass: function(oid, cssClass) {
	    oid = typeof oid == "string" ? this.$(oid) : oid;
	    if (!cssClass) oid.removeAttribute("class");
		oid.className = cssClass;
	},
	externalLink: function() {
	    try {
	        var i=0;
	        var l = document.getElementsByTagName("a");
	        for (i=0; i < l.length; i++) {
	            if (l[i].rel.indexOf('external') == 0) {
	                l[i].target = '_blank';	             
	            }	            
	        }
	    }
	    catch (oError) { }
	},
	addToDoOnLoad: function(f) {
        if(RI("isIE")) {
            window.onload = readDoOnLoad;
            doOnLoadList[doOnLoadList.length] = f;
        }
        else if(window.onload){
            if (window.onload != readDoOnLoad) {
                doOnLoadList[0] = window.onload;
                window.onload = readDoOnLoad;
            }
            doOnLoadList[doOnLoadList.length]=f;
        }
        else {
            window.onload = f;
        }
    },
    addToDoTheEnd: function(f) {
        doTheEndList[doTheEndList.length] = f;
    },
    getScriptFile: function(f, set) {
        var d = document; set = set||"H";
        if (set == "H") {
            var s = d.createElement("script");
            s.setAttribute("type", "text/javascript");
            s.setAttribute("src", f);
            d.getElementsByTagName("head")[0].appendChild(s);
        }
        else if(set == "F") {
            d.write(unescape('%3Cscript%20type%3D%22text/javascript%22%20src%3D%22'+f+'%22%3E%3C/script%3E'));
        }
    },
    getCssFile: function(f) {
        var css = document.createElement("link");
        css.setAttribute("type", "text/css");
        css.setAttribute("rel", "stylesheet");
        css.setAttribute("href", f);
        document.getElementsByTagName("head")[0].appendChild(css);
    },
    setDefCss: function(oid, regula) {
        var element = typeof oid == 'object' ? oid : RI("$", oid);
        if (RI("czyIE")) {
            element.style.setAttribute("cssText",regula);
        } 
        else {
            element.style.cssText = regula;
        }
    },
    createElementIn: function(refOid, newTag, attr) {
        var refObj = typeof refOid == 'object' ? refOid : RI("$", refOid)||document.body;
        var obj = document.createElement(newTag);
        for (k in attr) {
            if (k == "style") {
                RI("setDefCss", obj, attr[k]);
            }
            else {
                obj.setAttribute(k, attr[k]);
            }
        }	    
	    refObj.appendChild(obj);
	    
	    return obj;
    },
    showHidden: function(oId, aS, reg) {
        reg = reg||"block";
        aS = aS||[];
        for (k in oId) {
            var ob = typeof oId[k] == 'object' ? oId[k]: this.$(oId[k]);
            if (ob == null) continue;
            if (typeof aS[k] == "undefined") {
                aS[k] = ob.style.display == "none" ? 1 : 0;
            }
            switch (aS[k]) {
                case 1:
                    ob.style.display = reg;
                    break;
                default:
                    ob.style.display = "none";
                    break;
            }
        }
        return false;
    },
    getPosition: function(oid) {
        oid = (typeof oid == "object") ? oid : RI("$", oid);
        var T = 0, L = 0;
        while(oid.tagName != 'HTML') {
            T += oid.offsetTop||0;
            L += oid.offsetLeft||0;
            if (oid.offsetParent) oid = oid.offsetParent;
            else break;
	    }
        T += oid.offsetTop||0;
        L += oid.offsetLeft||0;
        return {x:L, y:T};
    },
    getScrollPosition: function() {
        var dd = document.documentElement, db = document.body;    
        if (self.pageYOffset) { return [self.pageXOffset, self.pageYOffset]; } 
        if (dd && dd.scrollTop) { return [dd.scrollLeft, dd.scrollTop]; } 
        if (db) { return [db.scrollLeft, db.scrollTop]; }
        return [0, 0];
    },

    getSize: function() {	
        var iWX, iWY, iPX, iPY, iSX, iSY;
        var dd = document.documentElement, db = document.body, w = window;
    	   
        if (window.innerHeight && window.scrollMaxY) { iSX = db.scrollWidth; iSY = w.innerHeight + w.scrollMaxY; } 
        else if (db.scrollHeight > db.offsetHeight){ iSX = db.scrollWidth; iSY = db.scrollHeight; } 
        else { iSX = db.offsetWidth; iSY = db.offsetHeight; }	
    	
        if (self.innerHeight) { iWX = self.innerWidth; iWY = self.innerHeight; } 
        else if (dd && dd.clientHeight) { iWX = dd.clientWidth; iWY = dd.clientHeight; } 
        else if (db) { iWX = db.clientWidth; iWY = db.clientHeight; }	
    	
        iPX = (iSX < iWX) ? iWX : iSX;
        iPY = (iSY < iWY) ? iWY : iSY;   
    	   
        return [iPX,iPY,iWX,iWY,screen.width,screen.height]; 
    }
}

function readDoOnLoad() {
    try {
        for (var i = 0; i < doOnLoadList.length; i++) {
            doOnLoadList[i]();
        }
    }
    catch (oError) {}
}
function readDoTheEnd() {
    for (var i = 0; i < doTheEndList.length; i++) {
        doTheEndList[i]();
    }
}

addToDoOnLoad = cRI.prototype.addToDoOnLoad;

RI("addToDoOnLoad", new Function("RI('externalLink')"));


wstawSWF = function(plik, id, szerokosc, wysokosc, jakosc) {
    var _jakosc = jakosc ? jakosc : 'high';        
    
    this.parametry = new Array();
    this.atrybuty = new Array();
    
    this.tekstZastepczy = "";
    
    this.ustawAtrybut('type', 'application/x-shockwave-flash');
    this.ustawAtrybut('data', plik);
	
	if(id) { this.ustawAtrybut('id', id); }
	if(szerokosc) { this.ustawAtrybut('width', szerokosc); }
	if(wysokosc) { this.ustawAtrybut('height', wysokosc); }
    
    this.ustawParametr('movie', plik);    
    
	this.ustawParametr('quality', _jakosc);
	this.ustawParametr('wmode', 'transparent');
    this.ustawParametr('pluginspage', 'http://www.macromedia.com/go/getflashplayer');   	
}

wstawSWF.prototype = {    
    ustawAtrybut: function(nazwa, wartosc) {
        this.atrybuty[nazwa] = wartosc;
    },        
    pobierzAtrybuty: function() {
        return this.atrybuty;    
    },        
    ustawParametr: function(nazwa, wartosc) {
        this.parametry[nazwa] = wartosc
    },    
    pobierzParametry: function() {
        return this.parametry;
    },  
    ustawTekstZastepczy: function(tekst) {
        this.tekstZastepczy = tekst;
    },    
    generujSWFHTML: function() {
        var parametry = this.pobierzParametry(), atrybuty = this.pobierzAtrybuty(), swf = "";
        
        swf += '<object ';
        for(var nazwa in atrybuty) {
            swf += nazwa+'="'+atrybuty[nazwa]+'" ';
        }
        swf += '>';        
        
		for(var nazwa in parametry) {
            swf += '<param name="'+ nazwa +'" value="'+ parametry[nazwa] +'" />';
        }
        if (this.tekstZastepczy != "") {
            swf += "<p>"+this.tekstZastepczy+"</p>";
        }
		swf += "</object>";
		
		return swf;
    },    
    wstaw: function(elementId) {        
        if (typeof elementId == 'string') {
            RI("setHTML",elementId, this.generujSWFHTML());
        }
		else {
		    RI("write",this.generujSWFHTML());
		}
    }
}

var HTTPRequest = function(){};
with({$:HTTPRequest.prototype}){
    $.czyObsluguje = function(){
		return !!this.polaczenie();
	};
	$.zdarzenia = ["start", "open", "send", "load", "end"];
    $.filtr = function(url) {
        var utf16 = ['%u0104','%u0106','%u0118','%u0141','%u0143','%u015A','%u017B','%u0179','%u0105','%u0107','%u0119',
                     '%u0142','%u0144','%u015B','%u017C','%u017A'];
        var utf8 = ['%A1','%C6','%CA','%A3','%D1','%A6','%AF','%AC','%B1','%E6','%EA','%B3','%F1','%B6','%BF','%BC'];
        url = escape(url);
        var matches = url.match(/%u[a-z0-9]{4}/gi);
        if (matches != null) {
            for (var i = 0; i < matches.length; i++) {
                var utf8code = "";
                for(var j=0; j<utf16.length; j++){
                    if (utf16[j] == matches[i]) {
                        utf8code = utf8[j];
                        break;
                    }
                }
                url = url.replace(matches[i], utf8code);
            }
        }
        return url;
	};
	$.polaczenie = function(){
		var s = ["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"], ob;
        try {
            return new XMLHttpRequest();
        }
        catch (oBlad) {
            for (var i = 0; i < s.length; i++) {
            	try {
                	return new ActiveXObject(s[i]);
                	break;
            	}
            	catch (oBlad){}
            }
        }
	};
	$.formatujParametry = function(parametry){
		var i, r = [];
		for(i in parametry) {
		    r[r.length] = i + "=" + (this.filtr ? this.filtr(parametry[i]) : parametry[i]);
		}
		return r.join("&");
	};
	$.get = function(url, parametry, funkcjaObslugi, oczekiwanaOdpowiedz){
        return this.request("GET", url + (url.indexOf("?") + 1 ? "&" : "?") + 
		       this.formatujParametry(parametry), null, funkcjaObslugi, null, oczekiwanaOdpowiedz);
	};
	$.post = function(url, parametry, funkcjaObslugi, oczekiwanaOdpowiedz){
		return this.request("POST", url, parametry = this.formatujParametry(parametry), funkcjaObslugi, {
			"Connection": "close",
			"Content-Length": parametry.length,
			"Method": "POST " + url + " HTTP/1.1",
			"Content-Type": "application/x-www-form-urlencoded; charset=iso-8859-2"
		}, oczekiwanaOdpowiedz);
	};
	$.request = function(metoda, url, parametry, funkcjaObslugi, naglowki, oczekiwanaOdpowiedz){
		var i, o = this.polaczenie(), f = funkcjaObslugi instanceof Function;
		try{
			o.open(metoda, url, !oczekiwanaOdpowiedz);
			oczekiwanaOdpowiedz || (o.onreadystatechange = function() {
				var s = $.zdarzenia[o.readyState];
				f ? funkcjaObslugi(o) : s in funkcjaObslugi && funkcjaObslugi[s](o);
			});
			o.setRequestHeader("HTTP_USER_AGENT", "XMLHttpRequest");
			try {
				for(i in naglowki) {
					o.setRequestHeader(i, naglowki[i]);
				}
					o.overrideMimeType('text/html; charset=ISO-8859-2');
			}
			catch(oBlad) {}
			o.send(parametry);
			oczekiwanaOdpowiedz && (f ? funkcjaObslugi(o) : funkcjaObslugi["end"] && funkcjaObslugi["end"](o));
			return true;
		}
		catch(oBlad){
			return false;
		}
	};
}
var Zdarzenia = new Object;
Zdarzenia.zaczep = function(cel, typ, fn, uC) {
    uC = uC||false;
    if (cel.addEventListener) cel.addEventListener(typ, fn, uC);
    else if (cel.attachEvent) cel.attachEvent("on"+typ, fn);
    else cel["on"+typ] = fn; 
};
Zdarzenia.odczep = function(cel, typ, fn, uC) {
    uC = uC||false;
    if (cel.removeEventListener) cel.removeEventListener(typ, fn, uC);
    else if (cel.detachEvent) cel.detachEvent("on"+typ, fn);
    else cel["on"+typ] = null; 
};
Zdarzenia.formatuj = function(oZd) {
    if (RI("isIE")) {
        oZd.charCode = (oZd.type == "keypress") ? oZd.keyCode : 0;
        oZd.eventPhase = 2;
        oZd.isChar = (oZd.charCode > 0);
        oZd.pageX = oZd.clientX + document.body.scrollLeft;
        oZd.pageY = oZd.clientY + document.body.scrollTop;
        oZd.preventDefault = function() { this.preventDefault = false; };
        if (oZd.type == "mouseout") oZd.relatedTarget = oZd.toElement;
        else if (oZd.type == "mouseover") oZd.relatedTarget = oZd.fromElement;
        oZd.stopPropagation = function () { this.cancelBubble = true; };
        oZd.target = oZd.srcElement;
        oZd.timeStamp = (new Date).getTime();
    }
    return oZd;
};
Zdarzenia.pobierz = function() {
    if (window.event) return this.formatuj(window.event);
    else return Zdarzenia.pobierz.caller.arguments[0]; 
};

var oAjax = new HTTPRequest();

cRI.prototype.initNewsList = function(oId) {
    oId = RI("$", oId)||null;
    if (!oId) return false;    
    var aA = oId.getElementsByTagName("a"), rel;
    for (var i = 0; i < aA.length; i++) {
        rel = aA[i].rel.split("-");
        if (rel[0] == "news") {
            aA[i].onclick = function () {
                var self = this;
                oAjax.get("/ajax/", {"model":"news", "newsId":this.rel.replace("news-","")}, {
                    "end":function(o) {
                        //if (!o.responseText) return false;
                        var aA = self.parentNode.parentNode.getElementsByTagName("a");
                        for (var i = 0; i < aA.length; i++) {
                            if (aA[i].className) {
                                aA[i].className = "";
                            }
                        }
                        self.className = "active";
                        var oN = oId.parentNode.getElementsByTagName("div").item(0);
                        oN.innerHTML = o.responseText;                        
                    } }
                );
                return false;
            }
        }
    }
};

cRI.prototype.initGallery = function(oId, imgSrc) {
    oId = (typeof oId == "object" ? oId : RI("$", oId)||null);
    if (!oId) return false;
    var img = oId.getElementsByTagName("img"), _img = [], oImgL = img.length, r = oImgL-4;
    var block = oId.getElementsByTagName("div").item(0);    
    block.style.width = ((oImgL*63))+"px";
    block.style.left = 0;
    
    //alert(block.style.width+" -- "+block.style.left+" == "+(parseInt(block.style.width)+parseInt(block.style.left)));
    if (oImgL > 4) {
    RI("$", "gallery-next").onclick = function() {
        if (63*r + parseInt(block.style.left) == 0) { return false; }
        block.style.left = (parseInt(block.style.left)-63)+"px";
    }
    RI("$", "gallery-prev").onclick = function() {
        if (parseInt(block.style.left) == 0) { return false; }
        block.style.left = (parseInt(block.style.left)+63)+"px"; 
    }
    }
    else {
        RI("$", "gallery-next").style.display = "none";
        RI("$", "gallery-prev").style.display = "none";
    }
    for (var i = 0; i < img.length; i++) {
        _img[i] = new Image();
        _img[i].src = img[i].src.replace("/s_","/");
        img[i].onclick = function() {
            RI("$", imgSrc).src = this.src.replace("/s_", "/");
        }
    }
};

cRI.prototype.initBasket = function(oId) {
    oId = RI("$", oId)||null;
    if (!oId) return false;    
    var aA = oId.getElementsByTagName("a"), rel;
    for (var i = 0; i < aA.length; i++) {
        rel = aA[i].rel.split("-");
        if (rel[0] == "addBasket") {
            aA[i].onclick = function () {
                var self = this;
                oAjax.get("/ajax/", {"model":"addbasket", "id":this.rel.replace("addbasket-","")}, {
                    "end":function(o) {
                        //if (!o.responseText) return false;
                        var aA = self.parentNode.parentNode.getElementsByTagName("a");
                        for (var i = 0; i < aA.length; i++) {
                            if (aA[i].className) {
                                aA[i].removeAttribute("class");
                            }
                        }
                        self.className = "active";
                        var oN = oId.parentNode.getElementsByTagName("div").item(0);
                        oN.innerHTML = o.responseText;                        
                    } }
                );
                return false;
            }
        }
    }
}

function postcodeV(ob) {
    var v = parseInt(ob.value);
    if (v >= 1181 && v <= 1189) {
        RI("$", "basket-take-away").checked = false;
        RI("$", "basket-delivery").disabled = false;
        RI("$", "info").style.display = "none";
    }
    else {
        RI("$", "basket-take-away").checked = true;
        RI("$", "basket-delivery").disabled = true;
        RI("$", "info").style.display = "block";
    }
    
    RI("$","basket-postcode").value = RI("$","basket-fill-postcode").value;
}

function add(ob) {
    var oF = RI("$", "basket-form");
    if (!oF) { return false; }
    var aRel = ob.rel.split("-");
    
    var _price = RI("$", "prd-prcie1-"+aRel[1]).checked == false ? 
        RI("$", "prd-prcie2-"+aRel[1]) : RI("$", "prd-prcie1-"+aRel[1]);
    var _count = RI("$", "prd-count-"+aRel[1]).value;
    
    if (!RI("$", "ordernow-stepprdlist")) {
        RI("$", "ordernow-id").value = aRel[1];
    }
    else {
        RI("$", "ordernow-stepprdlist").value = aRel[1];
    }
    RI("$", "ordernow-count").value = _count;
    RI("$", "ordernow-price").value = _price.value;
    RI("$", "ordernow-pricevalue").value = _price.title;
    
    oF.action = ob.href;
    oF.submit();
    return false;
}

function add3(ob, id, price) {
    var oF = RI("$", "basket-form"), aTmp = [];
    if (!oF) { return false; }
    
    price = price||0;
    RI("$", "ordernow-id").value = id;
    RI("$", "ordernow-count").value = parseInt(RI("$", "ordernow-count-"+id).value);   
    RI("$", "ordernow-pricevalue").value = parseFloat(RI("$", "ordernow-pricevalue").value)+parseFloat(price); 
    
    var aIn = RI("$", "step-2").getElementsByTagName("input");
    for (var i = 0; i < aIn.length; i++) {
        if (aIn[i].checked == true) { aTmp.push(aIn[i].value); }
    }
    
    var aIn = RI("$", "step-3").getElementsByTagName("input");
    for (i = 0; i < aIn.length; i++) {
        if (aIn[i].checked == true) { aTmp.push(aIn[i].value); }
    }
    
    if (RI("$", "ordernow-stepprdlist")) {
        RI("$", "ordernow-stepprdlist").value = aTmp.join(",");
    }
    RI("$", "ordernow-pricevalue").value = "::"+RI("$", "ordernow-pricevalue").value;  
    oF.action = ob.href+"0,sep3";
    oF.submit();
    return false;
}

function showStep(id, price) {
    if (!RI("$", "step-"+id)) { return false; }
    
    price = price||0;    
    if (price) {
        RI("$", "ordernow-pricevalue").value = parseFloat(RI("$", "ordernow-pricevalue").value)+parseFloat(price);    
    }
    
    RI("$", "step-"+id).style.display = "block";
    location.hash = "step-"+(id-1);
    return false;    
}

function showStep3(ob, id) {
    var step = RI("$", "step-"+id), _id = 3;      
    while (_id > 1) {
        if (_id == id) { _id--; continue; }
        RI("$", "step-"+_id).style.display = "none";
        _id--;
    }
    if (!step) { return false; }
    if (step.style.display == "block") {
        step.style.display = "none";
    } else {
        var p = RI("getPosition", ob);
        step.style.top = (parseInt(p.y)+22)+"px";
        step.style.left = (parseInt(p.x)-5)+"px";        
        step.style.display = "block";
        location.hash = "step-1";
    }
    
    return false;    
}

function setStepPrice(price) {
    price = price||0;    
    if (price) {
        RI("$", "ordernow-pricevalue").value = parseFloat(RI("$", "ordernow-pricevalue").value)+parseFloat(price);    
    }
}

function basketInfo() {
    var _oB = document.body;
    var _wrapper = RI("$", "wrapper");
    
    RI("setCssClass", "wrapper", "op");
    
    var _div = RI("createElementIn", document.body, "div", {"id":"basket-info", "style":"position: absolute; width: 250px; height: 100px; line-height: 100px; vertical-align: middle; background: #000; color: red; font-size: 125%; border: 3px solid #575757;"});
    
    _div.style.top = "250px";
    _div.style.left = ((_oB.offsetWidth-250)/2)+"px";
    _div.innerHTML = "Product was added";
}
