﻿var inTransition = false;
function ExpandStartGift() {
    if (BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6) {
        window.location.href = "/ge-en-gava"; return;
    }
    if (!inTransition) {
        inTransition = true;
        if ($('startgive').style.left == '175px')
            new Effect.Move('startgive', { duration: 0.5, x: 650, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, afterFinish: function () { inTransition = false; } });
        else
            new Effect.Move('startgive', { duration: 0.5, x: 175, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, afterFinish: function () { inTransition = false; } });
    }
}

function ExpandMGift() {
    if (BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 7) {
        window.location.href = "/ge-en-gava"; return;
    }

    if (!inTransition) {
        inTransition = true;
        if ($('mgive').style.left == '-443px')
            new Effect.Move('mgive', { duration: 0.5, x: 12, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, afterFinish: function () { inTransition = false; } });
        else
            new Effect.Move('mgive', { duration: 0.5, x: -443, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, afterFinish: function () { inTransition = false; } });
    }
}

function ExpandMGift2() {
    if (BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 7) {
        window.location.href = "/ge-en-gava"; return;
    }

    if (!inTransition) {
        var height = $('rc2incp').offsetHeight + 20
        var cheight = $('rc2incp2').offsetHeight + 20
        inTransition = true;
        if ($('mgive').style.left == '-443px') {
            new Effect.Move('mgive', { duration: 0.6, x: 12, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, afterFinish: function () { inTransition = false; } });
            new Effect.Fade('rc2inner', { duration: 0.3, afterFinish: function () {
                $('rc2inner').style.height = '0px';
                $('rc2inner').show();
            }
            });
            new Effect.Morph('rc2inner2', {
                style: 'height:' + cheight + 'px;', // CSS Properties
                duration: 0.5 // Core Effect properties
            });
            //Effect.BlindUp('rc2inner', { duration: 0.5, scaleMode: 'box' });
            //            new Effect.Morph('rc2inner', {
            //                style: 'height:0px;', // CSS Properties
            //                duration: 0.4, // Core Effect properties
            //                afterFinish: function() {
            //                    new Effect.Morph('rc2inner2', {
            //                        style: 'height:' + cheight + 'px;', // CSS Properties
            //                        duration: 0.4 // Core Effect properties
            //                    });
            //                }
            //            });
        }
        else { //expand
            new Effect.Move('mgive', { duration: 0.5, x: -443, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal, afterFinish: function () { inTransition = false; } });
            new Effect.Fade('rc2inner2', { duration: 0.3, afterFinish: function () {
                $('rc2inner2').style.height = '0px';
                $('rc2inner2').show();
            }
            });
            new Effect.Morph('rc2inner', {
                style: 'height:' + height + 'px;', // CSS Properties
                duration: 0.5 // Core Effect properties
            });
            //            new Effect.Morph('rc2inner2', {
            //                style: 'height:0px;', // CSS Properties
            //                duration: 0.4, // Core Effect properties
            //                afterFinish: function() {
            //                    new Effect.Morph('rc2inner', {
            //                        style: 'height:' + height + 'px;', // CSS Properties
            //                        duration: 0.4 // Core Effect properties
            //                    });
            //                }
            //            });
            //Effect.BlindDown('rc2inner', { duration: 1, scaleMode: 'box' });
        }
    }
}

function SwitchImgS2() {
    var obj = $('giveis');
    if (obj.src.indexOf('/Images/giftsmall.jpg') >= 0)
        obj.src = '/Images/giftsmallo2.jpg';
    else
        obj.src = '/Images/giftsmall2.jpg';
}

function SwitchImg() {
    var obj = $('givei');
    if (obj.src.indexOf('/Images/gift.jpg') >= 0)
        obj.src = '/Images/gifto.jpg';
    else
        obj.src = '/Images/gift.jpg';
}

function ShowDDMenu(id) {

    if (curid > 0 && curid != id) {
        var obj2 = $('ddmenu_' + curid);
        if (obj2) {
            obj2.hide();
            curid = 0;
        }
    }

    if (timeo) {
        clearTimeout(timeo);
        timeo = undefined;
    }

    var obj = $('ddmenu_' + id);
    if (curid != id && obj) {
        curid = id;
        var width = 5;
        obj.style.visibility = 'hidden';
        obj.show();
        obj.select('ul.arrowl').each(function (el) {
            width += el.getWidth() + 30;
        });
        if (width < $('menu_' + id).getWidth()) {
            width = $('menu_' + id).getWidth() + 20;
        }
        obj.select('div.tab').each(function (el) {
            el.style.width = ($('menu_' + id).getWidth() + 8) + 'px';
        });
        obj.style.width = width + 'px';
        obj.style.visibility = 'visible';
    }
}


function ShowDDMenuBread(id) {

    if (curid > 0 && curid != id) {
        var obj2 = $('ddmenuBread_' + curid);
        if (obj2) {
            obj2.hide();
            curid = 0;
        }
    }

    if (timeo) {
        clearTimeout(timeo);
        timeo = undefined;
    }

    var obj = $('ddmenuBread_' + id);
    if (curid != id && obj) {
        curid = id;
        var width = 5;
        obj.style.visibility = 'hidden';
        obj.show();
        obj.select('ul.arrowl').each(function (el) {
            width += el.getWidth() + 30;
        });
        if (width < $('menubread_' + id).getWidth()) {
            width = $('menubread_' + id).getWidth() + 20;
        }
        obj.select('div.tab').each(function (el) {
            el.style.width = ($('menubread_' + id).getWidth() + 8) + 'px';
        });
        obj.style.width = width + 'px';
        obj.style.visibility = 'visible';
    }
}




function OpenNews(el) {
    var hide = false;
    var pnode = $(el.parentNode.parentNode);
    if (pnode.className.indexOf('expanded') > -1)
        hide = true;

    $$('#newslist div.expanded').each(function (e) {
        e.removeClassName('expanded');
    });

    if (!hide)
        $(pnode).addClassName('expanded');
}

function fonth(data) {
    return data + "&b=2&blc=255%2c255%2c255";
}

function gotPageList(data) {
    if (window.addthis) {
        window.addthis.ost = 0;
        window.addthis.ready();
    }
}

var timeo = undefined;
var curid = 0;
function HideDDMenu(id) {
    var obj = $('ddmenu_' + id);
    currentMenu = undefined;
    if (obj && !timeo) {
        timeo = setTimeout(function () { obj.hide(); curid = 0; }, 100);
    }
}

function HideDDMenuBread(id) {
    var obj = $('ddmenuBread_' + id);
    currentMenu = undefined;
    if (obj && !timeo) {
        timeo = setTimeout(function () { obj.hide(); curid = 0; }, 100);
    }
}


var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();

//shopa
function GotBasket(res) {
    $("baskettable").innerHTML = "";
    if (res.Articles.length < 1) {
        $('basketcontainer').hide();
        var span = document.createElement("span");
        span.className = "emptyCart";
        span.innerHTML = "Din förfrågan är tom";

        $("baskettable").appendChild(span);
        showBasket = false;
    } else {
        showBasket = true;
        //Hududet på tabellen
        var table = document.createElement("table");
        //        var thead = document.createElement("thead");
        //        var trHead = document.createElement("tr");
        //        var tdHead1 = document.createElement("th");
        //var tdHead2 = document.createElement("th");
        //var tdHead4 = document.createElement("th");
        //tdHead2.innerHTML = "Antal";
        //tdHead4.innerHTML = "Benämning";

        //trHead.appendChild(tdHead1);
        //trHead.appendChild(tdHead2);
        //trHead.appendChild(tdHead4);
        //thead.appendChild(trHead);
        //table.appendChild(thead);

        //resten
        var tbody = document.createElement("tbody");
        var trSum = document.createElement("tr");
        table.appendChild(tbody);

        for (var i = 0; i < res.Articles.length; i++) {
            var tr = document.createElement("tr");
            //var td1 = document.createElement("td");
            //var td2 = document.createElement("td");
            var td4 = document.createElement("td");
            td4.className = "f";
            //td1.className = "delLink";
            //td1.onclick = function () { if (confirm("Är du säker på att du vill ta bort produkten?")) wdShop.DeleteItem(this.idn, GotBasket); };

            //td1.idn = res.Articles[i].Id;
            //td2.innerHTML = res.Articles[i].Count;
            td4.innerHTML = '<a href="' + res.Articles[i].PageLink + '" title="' + res.Articles[i].Name + '">' + res.Articles[i].Name + '</a>';
            //tr.appendChild(td1);
            //tr.appendChild(td2);
            tr.appendChild(td4);
            tbody.appendChild(tr);
        }
        $("baskettable").appendChild(table);
        ShowBasket();
    }
    EndBasketLoading();
}

function ShowAddLoading() {
    $('addbtn').enabled = false;
    $('addbasketloading').show();
}

function HideAddLoading() {
    $('addbtn').enabled = true;
    $('addbasketloading').hide();
    //window.location.href = '/checkout';
}

function ShowBasket() {
    $('basketcontainer').show();
}

function ChangeAmount(el, id) {
    wdShop.UpdateQuantity(id, el.value, function (res) {
        if (res) {
            new Effect.Highlight(el, { startcolor: '#A7FA98', endcolor: '#ffffff' });
            wdShop.GetLimitedCart(GotBasket, BasketError);
        }
        else
            new Effect.Highlight(el, { startcolor: '#FC8181', endcolor: '#ffffff' });
    }, function () {
        new Effect.Highlight(el, { startcolor: '#FC8181', endcolor: '#ffffff' });
    });

}

function EndBasketLoading() {
    var obj = $('basketloading');
    if (obj)
        obj.hide();
}

function StartBasketLoading() {
    var obj = $('basketloading');
    if (obj)
        obj.show();
}

function GetBasket() {
    StartBasketLoading();
    wdShop.GetLimitedCart(function (res) { GotBasket(res); }, BasketError);
}

function BasketError() {
    showBasket = false;
    EndBasketLoading();
}

function ProdAdded() {
    var obj = $('prodconfirm');
    if (obj) {
        obj.appear();
        setTimeout(function () { obj.fade(); }, 5000);
    }
}

function AddToBasket(name, aid, noi) {
    ShowAddLoading();
    wdShop.AddToBasket(aid, name, noi, function () { HideAddLoading(); GetBasket(); ProdAdded(); });
}

document.observe("dom:loaded", function () { if ($('basketcontainer')) setTimeout(function () { GetBasket(); EndBasketLoading(); }, 100); });



var ci = 0;
var to;
function switchImages(jumpto) {
    if (jumpto)
        ci = jumpto - 1;
    var imgs = $('topimgs').select('a');
    var links = $('swParent').select('a');
    if (ci >= imgs.length)
        ci = 0;
    imgs.each(function (a, idx) {
        if (ci == idx) {
            a.appear({ duration: 0.4, from: 0, to: 1 });
        }
        else
            a.fade({ duration: 0.4, from: 1, to: 0 });
    });
    try {
        links.each(function (a) { a.className = ''; });
        links[ci].className = "selidx";
    }
    catch (ex) {
    }
    ci++;
    if (to)
        clearTimeout(to);

    to = setTimeout(function () { switchImages(); }, 7000);

}
function initImages() {
    var p = $('swParent');
    $('topimgs').select('a').each(function (a, idx) {
        var a = document.createElement('a');
        a.innerHTML = idx + 1;
        a.idx = idx;
        a.onclick = function () {
            switchImages(this.idx + 1);
        }
        p.appendChild(a);
    });
}
