var ProductView;
var Popup;
var PopupZoom;
var PopupMain;
var PopupImage;
var PopupTitle;
var PopupName;
var PopupCount;
var PopupSize;
var PopupPrice;
var PopupDisplay;
var frontLink;
var backLink;
var ProductCheckForm = true;
var isIE = false;
/*@cc_on
isIE = true;
@*/

function closePopup()
{
    if(Popup.parentNode)
    {
        Popup.parentNode.removeChild(Popup);
    }
}

function buyProduct(id, express, showPopup)
{
    var reference = this;
    this.id = id;
    this.express = express;

    this.execute = function()
    {

if(isIE){ if(PopupMain.parentNode.nodeType == 11){ var parent = PopupZoom.parentNode; parent.removeChild(PopupZoom); parent.appendChild(PopupMain);}}else
        if(!PopupMain.parentNode)
        {
            var parent = PopupZoom.parentNode;
            parent.removeChild(PopupZoom);
            parent.appendChild(PopupMain);
        }

        //var sizeValue = dojo.byId("productSize" + reference.id);
	    var sizes = document.getElementsByName("productSize" + reference.id);
	    var sizeValue = '';
	    for(var i = 0; i < sizes.length; i++){
	    
	    	if(sizes[i].checked){
		        sizeValue = sizes[i].value;
	    	}
	    
	    }
	    
        //var sizeValue = dojo.byName("productSize" + reference.id);

        //if(sizeValue && !sizeValue.value)
        if(sizes.length>0 && sizeValue == '')
        {
        	ProductCheckForm = false;
            ErrorPopupText.nodeValue = "Wybierz rozmiar koszulki.";
            document.body.appendChild(ErrorPopup);
            return;
        }else{
	        ProductCheckForm = true;
        	//var sizeLabel = document.getElementById('iSize'+sizeValue).value;
        }

        var count = dojo.byId("productCount" + reference.id);
        if(count)
        {
            count = parseInt(count.value);
        }
        else
        {
            count = 1;
        }

        //podliczenie kontrolne, drugie ID
		if(dojo.byId("item_count")){
	        var count2 = dojo.byId("item_count");
	        if(count2.value){
	        
	        	count = parseInt(count2.value);
	        
	        }
		}
        
	    dojo.io.bind({
            id: reference.id,
            url: "/ajax.php?module=basket&command=" + (reference.express ? "express" : "shop") + "&count=" + count + "&product=" + reference.id + (sizeValue ? "&size=" + sizeValue : ""),
            error: function(type, data, event)
            {
            },
            load: function(type, data, event)
            {
                if(reference.express)
                {
                    top.location = "/koszyk";
                }
                else
                {
                
				
                    refreshBasket(type, data, event);

                    var price = document.getElementById("productPrice" + this.id);

                    for(var i = 0; i < price.childNodes.length; i++)
                    {
                        if(price.childNodes[i].nodeType == 3)
                        {
                            price = price.childNodes[i].nodeValue;
                            break;
                        }
                    }

                    var name = document.getElementById("productName" + this.id);

                    for(var i = 0; i < name.childNodes.length; i++)
                    {
                        if(name.childNodes[i].nodeType == 3)
                        {
                            name = name.childNodes[i].nodeValue;
                            break;
                        }
                    }
                    
                    //alert('Dodano produkt do koszyka');
					window.location.href = '/koszyk';
                    
                    /*
                    if(showPopup != false){

	                    PopupTitle.nodeValue = "Dodano do koszyka";
	                    PopupImage.src = "/upload/shops/products/middle/" + this.id + ".gif";
	                    PopupName.nodeValue = name;
	                    PopupCount.nodeValue = count;
	                    PopupSize.nodeValue = sizeLabel;//sizeValue.options[sizeValue.selectedIndex].text;
	                    PopupPrice.nodeValue = (price * count) + " PLN";
	
	                    document.body.appendChild(Popup);
                    }
                    */
                }
            },
            mimetype: "application/xml"
        });

        return false;
    };
}

function violationReporter(id)
{
    var reference = this;
    this.id = id;

    this.execute = function()
    {
        var message = prompt("W jaki sposób ten produkt narusza reguły lub prawo: ");

        if(message)
        {
            dojo.io.bind({
                id: reference.id,
                url: "/ajax.php?module=shops&command=report&id=" + reference.id + "&content=" + escape(message),
                error: function(type, data, event)
                {
                },
                load: function(type, data, event)
                {
                    alert("Twoje zgłoszenie zostało zapisane.");
                },
                mimetype: "application/xml"
            });
        }

        return false;
    };
}

function showBackView(id)
{
    var frontLinkTab = document.getElementById("frontPrintLink" + id);
    var backLinkTab = document.getElementById("topPrintLink" + id);
    var productViewImage = document.getElementById("productView" + id);

    frontLinkTab.className = "left_arrow";
    backLinkTab.className = "right_arrow";

    if( !productViewImage.src.match(/_back\.gif$/) )
    {
        productViewImage.src = productViewImage.src.replace(/\.gif$/, "_back.gif");
    }

    return false;
}

function showFrontView(id)
{
    var frontLinkTab = document.getElementById("frontPrintLink" + id);
    var backLinkTab = document.getElementById("topPrintLink" + id);
    var productViewImage = document.getElementById("productView" + id);

    frontLinkTab.className = "left_arrow";
    backLinkTab.className = "right_arrow";

    if( productViewImage.src.match(/_back\.gif$/) )
    {
        productViewImage.src = productViewImage.src.replace(/_back\.gif$/, ".gif");
    }

    return false;
}

function showFront()
{
    frontLink.parentNode.className = "current";
    backLink.parentNode.className = "";

    if( ProductView.src.match(/_back\.gif$/) )
    {
        ProductView.src = ProductView.src.replace(/_back\.gif$/, ".gif");
    }

    return false;
}

function showBack()
{
    frontLink.parentNode.className = "";
    backLink.parentNode.className = "current";

    if( !ProductView.src.match(/_back\.gif$/) )
    {
        ProductView.src = ProductView.src.replace(/\.gif$/, "_back.gif");
    }

    return false;
}

function showZoom()
{
if(isIE){ if(PopupZoom.parentNode.nodeType == 11) var parent = PopupMain.parentNode; parent.removeChild(PopupMain); parent.appendChild(PopupZoom);}else
    if(!PopupZoom.parentNode)
    {
        var parent = PopupMain.parentNode;
        parent.removeChild(PopupMain);
        parent.appendChild(PopupZoom);
    }

    if( ProductView.src.match(/_back\.gif$/) )
    {
        PopupDisplay.src = dojo.byId("zoomBack").value;
    }
    else
    {
        PopupDisplay.src = dojo.byId("zoomFront").value;
    }

    PopupTitle.nodeValue = "Nadruk";
    document.body.appendChild(Popup);
}

function initializeShop()
{
    ProductView = dojo.byId("productView");

    var zoomLink = dojo.byId("zoomLink");

    if(zoomLink)
    {
        zoomLink.onclick = showZoom;
    }

    buttons = document.getElementsByTagName("input");

    for(var i = 0; i < buttons.length; i++)
    {
        if(buttons[i].type == "image" && buttons[i].id.match(/^productBuy/) )
        {
            var buyer = new buyProduct( buttons[i].id.replace(/^productBuy/, ""), false, true);
            dojo.event.connect(buttons[i], "onclick", buyer, "execute");
        }
    }

    links = document.getElementsByTagName("a");

    for(var i = 0; i < links.length; i++)
    {
        if( links[i].id.match(/^productBuy/) )
        {
            var buyer = new buyProduct( links[i].id.replace(/^productBuy/, ""), false, true);
            dojo.event.connect(links[i], "onclick", buyer, "execute");
        }
        else if( links[i].id.match(/^productExpress/) )
        {
            var buyer = new buyProduct( links[i].id.replace(/^productExpress/, ""), true, true);
            dojo.event.connect(links[i], "onclick", buyer, "execute");
        }
        else if( links[i].id.match(/^reportViolation/) )
        {
            var violation = new violationReporter( links[i].id.replace(/^reportViolation/, "") );
            dojo.event.connect(links[i], "onclick", violation, "execute");
        }
    }

    frontLink = dojo.byId("frontPrintLink");
    backLink = dojo.byId("backPrintLink");

    if(frontLink)
    {
        frontLink.onclick = showFront;
    }

    if(backLink)
    {
        backLink.onclick = showBack;
    }

    Popup = document.createElement("div");
    Popup.className = "popup";

    var div = document.createElement("div");
    div.className = "bg_white";
    Popup.appendChild(div);

    div = document.createElement("div");
    div.className = "visible";
    Popup.appendChild(div);

    var box = document.createElement("div");
    box.className = "popBox";
    div.appendChild(box);

    div = document.createElement("div");
    div.className = "title";
    box.appendChild(div);

    var span = document.createElement("span");
    span.className = "left";
    div.appendChild(span);

    PopupTitle = document.createTextNode("Dodano do koszyka");

    span = document.createElement("span");
    span.className = "middle";
    span.appendChild(PopupTitle);
    div.appendChild(span);

    var a = document.createElement("a");
    a.className = "nowhere";
    a.onclick = closePopup;
    div.appendChild(a);

    span = document.createElement("span");
    span.className = "right";
    a.appendChild(span);

    PopupMain = document.createElement("div");
    PopupMain.className = "main";
    box.appendChild(PopupMain);

    PopupImage = document.createElement("img");
    PopupImage.className = "poz_img";
    PopupMain.appendChild(PopupImage);

    div = document.createElement("div");
    div.className = "opis";
    PopupMain.appendChild(div);

    var strong = document.createElement("p");
    strong.appendChild( document.createTextNode("Poniższy produkt został dodany do Twojego koszyka.") );
    div.appendChild(strong);

    strong = document.createElement("strong");
    strong.className = "orange";
    div.appendChild(strong);

    PopupName = document.createTextNode("nazwa");
    strong.appendChild(PopupName);

    var p = document.createElement("p");
    div.appendChild(p);

    strong = document.createElement("strong");
    strong.appendChild( document.createTextNode("Ilość: ") );
    p.appendChild(strong);

    PopupCount = document.createTextNode("ilosc");
    p.appendChild(PopupCount);

    p = document.createElement("p");
    div.appendChild(p);

    strong = document.createElement("strong");
    strong.appendChild( document.createTextNode("Rozmiar: ") );
    p.appendChild(strong);

    PopupSize = document.createTextNode("ilosc");
    p.appendChild(PopupSize);

    p = document.createElement("p");
    div.appendChild(p);

    strong = document.createElement("strong");
    strong.appendChild( document.createTextNode("Cena: ") );
    p.appendChild(strong);

    PopupPrice = document.createTextNode("c");
    p.appendChild(PopupPrice);

    div = document.createElement("div");
    div.className = "button";
    PopupMain.appendChild(div);

    var button = document.createElement("div");
    button.className = "button1";
    div.appendChild(button);

    span = document.createElement("span");
    span.className = "left";
    button.appendChild(span);

    span = document.createElement("span");
    span.className = "middle";
    button.appendChild(span);

    a = document.createElement("a");
    a.className = "nowhere";
    a.onclick = closePopup;
    span.appendChild(a);

    var img = document.createElement("img");
    img.src = "/images/arrowBack1.png";
    a.appendChild(img);

    span = document.createElement("span");
    span.className = "bold";
    span.appendChild( document.createTextNode("Kontynuuj zakupy!") );
    a.appendChild(span);

    span = document.createElement("span");
    span.className = "right";
    button.appendChild(span);

    button = document.createElement("div");
    button.className = "button1";
    div.appendChild(button);

    span = document.createElement("span");
    span.className = "left";
    button.appendChild(span);

    span = document.createElement("span");
    span.className = "middle";
    button.appendChild(span);

    a = document.createElement("a");
    a.href = "/koszyk";
    span.appendChild(a);

    img = document.createElement("img");
    img.src = "/images/basket.png";
    a.appendChild(img);

    span = document.createElement("span");
    span.className = "bold";
    span.appendChild( document.createTextNode("Idź do koszyka") );
    a.appendChild(span);

    span = document.createElement("span");
    span.className = "right";
    button.appendChild(span);

    PopupZoom = document.createElement("div");
    PopupZoom.className = "main";

    PopupDisplay = document.createElement("img");
    PopupDisplay.className = "zoom";
    PopupZoom.appendChild(PopupDisplay);

    div = document.createElement("div");
    div.className = "spacer1";
    PopupMain.appendChild(div);
}

dojo.addOnLoad(initializeShop);
