﻿/*global $, jQuery, HTMLElement*/

/*
    Remember to check rollercoaster in JSLint!
    http://www.jslint.com
    select "Good Parts" & "Assume a browser"
    then unselect "Disallow leading _ in identifiers"
    "Strict white space indentation" should be: 4.
    
    Rollecoaster should not add any (new) global variables to the DOM.
    Methods and functions in rollercoaster should not contain any unused variables.
*/

(function () {
    var rollercoaster, rc, ie6;
    
    /* -- misc DOM methods -- */
    Array.prototype.isArray = true;
    
    String.prototype.trim = function () {
        return this.replace(/(^\s+|\s$)/g, "");  
    };
    
    if (window.HTMLElement) {
        if (!document.body.outerHTML && HTMLElement.prototype.__defineGetter__) {
            HTMLElement.prototype.__defineGetter__("outerHTML", function () {
                var that = this, tag, start = "<", end = ">", ats = this.attributes, i, ohtml;
                
                tag = that.tagName.toLowerCase();
                
                if ((/(input|img|br|hr)/i).test(tag)) {
                    if (ats.length) {
                        for (i = 0; i < ats.length; i += 1) {
                            tag += " " + ats[i].name + "=\"" + ats[i].value + "\"";
                        }
                    }
                    
                    ohtml = start + tag + " /" + end;
                } else {
                    if (ats.length) {
                        for (i = 0; i < ats.length; i += 1) {
                            tag += " " + ats[i].name + "=\"" + ats[i].value + "\"";
                        }
                    }

                    ohtml = start + tag + end;
                    ohtml += that.innerHTML;
                    ohtml += start + "/" + that.tagName.toLowerCase() + end;
                }
                
                return ohtml;
            }); 
        }
    }

    /* -- /misc DOM methods -- */
    
     
    
    
    /* -- misc util functions & methods -- */
    function getStyle(elem, style) {
        var result, replacer;
        if (elem.currentStyle) {
            replacer = function (match) {
	            return match.replace(/\-/, "").toUpperCase();
            };
            style = style.replace(/\-[a-z]/, replacer);
            result = elem.currentStyle[style];
        } else {
            result = document.defaultView.getComputedStyle(elem, null).getPropertyValue(style);
        }
        return result;
    }
    
    document.getZIndex = function () {
        var all = document.getElementsByTagName("*"), zI = 0, i;
        for (i = 0; i < all.length; i += 1) {
            if (getStyle(all[i], "z-index") && parseInt(getStyle(all[i], "z-index"), 10) > zI) {
                zI = parseInt(getStyle(all[i], "z-index") + 1, 10);
            }
        }
        return parseInt(zI, 10);
    };
    
    document.getViewPort = function () {
        var vpo = {}, b;
        
        if ($.browser.msie) {
            window.innerHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.getElementsByTagName('body')[0].clientHeight;
            window.innerWidth = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.getElementsByTagName('body')[0].clientWidth;
        }
        
        if ($.browser.msie) {
            b = (document.compatMode && document.compatMode !== "BackCompat") ? document.documentElement : document.body;
            window.pageYOffset = b.scrollTop; 
            window.pageXOffset = b.scrollLeft;
        }
        
        vpo.top = window.pageYOffset;
        vpo.left = window.pageXOffset;
        
        return vpo;
    };
    /* -- /misc util functions & methods -- */
    
    ie6 = {
        cornerFix : function (container) {
            var dcn = container.getElementsByTagName("*"), i;
            for (i = 0; i < dcn.length; i += 1) {
                if ((/\.png['"]\)$/i).test(dcn[i].currentStyle.backgroundImage)) {
                    dcn[i].style.backgroundImage = dcn[i].currentStyle.backgroundImage.replace(/\.png/i, ".gif");
                }
            }
        }
    };
    
    rollercoaster = {
        config : {
            documentWrapperId : "wrapper"
        },
        imgLinkHeight : function () {
            var im = $(".rcLink img, .rcGallery a img"), i;
            for (i = 0; i < im.length; i += 1) {
                im[0].style.lineHeight = "normal";
                im[0].parentNode.style.lineHeight = "normal";
            }
        },
        init : function () {
            rc.preLoad();
            $(".rcLink").click(rc.open);
    
            $(window).bind("resize", rc.checkRCPositions);
        },
        preLoad : function () {
            var imgsLoading = 1, preLoadDiv = document.createElement("div"), isImg, isFlash, flCount = 0, htmlDiv = document.createElement("div"), htmlId;
            preLoadDiv.id = "preLoadRCDiv";
            htmlDiv.id = "htmlDiv";
            document.body.appendChild(preLoadDiv);
            document.body.appendChild(htmlDiv);
            $("a.rcLink, .rcGallery a").each(function () {
                var that = this, img, query = that.toString().match(/\?[\w\-\=\.\,\&]+/) ? that.toString().match(/\?[\w\-\=\.\,\&]+/).toString() : "";;
                isImg = (/\.(jpeg|jpg|gif|png)(\?|$)/).test(that);
                isFlash = (/\.swf(\?|$)/).test(that);
                
                if (isImg && !isFlash) {
                    img = document.createElement("img");
                    img.src = that;
                    preLoadDiv.appendChild(img);
                    img.onload = function () {
                        imgsLoading = 0;
                    };
                } else if (isFlash) {
                    rc.flashPreLoad(that, flCount, preLoadDiv);
                    flCount += 1;
                } else if (!(/\#$/).test(that)) {
                    htmlId = that.toString().match(/\#[\w]+/) ? that.toString().match(/\#[\w]+/) : "";
                    $(htmlDiv).load(that + " " + htmlId + " img");
                }
                
            });
        },
        flashPreLoad : function (flashURL, i, pld) {
            var newFlash = document.createElement("div");
            newFlash.id = "flc" + i;
            newFlash.className = "rcFlashCont";
            pld.appendChild(newFlash);
                
            $("#flc" + i).flash({
                swf: flashURL,  
                height: "100%",   
                width: "100%",
                params : {
                    "wmode" : "transparent",
                    "quality" : "high",
                    "menu" : "false",
                    "scale" : "exactfit",
                    "base" : ".",
                    "allowScriptAccess" : "always"
                }
            });
            
            flashURL.flashDiv = newFlash;
        },
        re : {
            pos : new RegExp("(x|y)\\-(left|right|top|bottom|center|middle|[\\d\\.]+(px|%|em|pt)?)", "g")
        },
        create : function () {
            var container = document.createElement("div");
            container.style.visibility = "hidden";
            $(container).addClass("rcContainer");
            
            container.addCorners = rc.addCorners;
            
            document.body.insertBefore(container, document.body.firstChild);
            
            container.style.top = 0;
            container.style.left = 0;
            
            return container;
        },
        remove : function () {
            var i, temp = [];
            for (i = 0; i < rc.list.length; i += 1) {
                if (rc.list[i] !== rc.rcContainer) {
                    temp.push(rc.list[i]);
                }
            }
            $(rc.rcContainer).remove();
            
            rc.list = temp;
        },
        open : function (evt) {
 
            var e = evt ? evt : window.event, that = this, p = that.pathname, h = that.hash ? that.hash.replace(/(?:(\?))[\w\_\=\/\:\.]+/i, "") : null,
            o = $(that).offset(), cn = that.className, 
            corners = (/(^|\s)corners(\-n)?(\s|$)/).test(cn) ? ((/(^|\s)corners(\-n)(\s|$)/).test(cn) ? false : true) : true,
            addClasses = (/(^|\s)addClasses\-([\w]+(,)?)+(\s|$)/).test(cn) ? cn.match(/(^|\s)addClasses\-([\w]+(,)?)+(\s|$)/).toString().replace(/(addClasses\-|,\s[\w|,]+$)/g, "").split(",") : [],
            i,
            drag = (/(^|\s)drag(\-n)?(\s|$)/).test(cn) ? ((/(^|\s)drag(\-n)(\s|$)/).test(cn) ? false : true) : true,
            width = cn.match(/(^|\s)width-\d+(px|em|pt|%)?/g) ? cn.match(/(^|\s)width-\d+(px|em|pt|%)?/g).toString().replace(/width\-/, "") : false,
            height = cn.match(/(^|\s)height-\d+(px|em|pt|%)?/g) ? cn.match(/(^|\s)height-\d+(px|em|pt|%)?/g).toString().replace(/height\-/, "") : false,
            posType = "rtoe",
            posId = null,
            afterLoad,
            buttons = (/(^|\s)buttons\-n(\s|$)/).test(cn) ? false : true,
            flashURL,
            zoom = (/(^|\s)zoom(\-n)?(\s|$)/).test(cn) ? ((/(^|\s)zoom(\-n)(\s|$)/).test(cn) ? false : true) : true,
            flashObj,
            flashContId,
            container = rc.create(),
            ou, sub, ni,
            query, st, sl,
            isImg = (/\.(jpg|jpeg|gif|png)(\?|$)/).test(that) ? true : (/(^|\s)type-img(\s|$)/).test(cn),
            isFlash = (/\.(swf)(\?|$)/).test(that) ? true : (/(^|\s)type-flash(\s|$)/).test(cn);
            
            /*
            linkType=img, html, flash...
            */
            
            $(document.body).addClass("rollercoaster");
            
            if (!(/^\//).test(p)) {
                p = "/" + p;
            }
            
            if ((/\?/).test(that.href)) {
                window.orgurl = that.href;
            } else {
                window.orgurl = null;
            }
            
            //rc.overlay.add();
            
            if ((/rtID/).test(posType)) {
                posType = "rtID";
            }
            
            if (!h || h === "#") {
                h = document.getElementById(rc.config.documentWrapperId) ? "#" + rc.config.documentWrapperId : "body";
            }
            
            
            
            
            afterLoad = function () {
                var name, value;
                that.posType = posType.toString().replace(/pos\=/, "");
                
                that.rcContainer = container;
                rc.rcContainer = container;
                
                container.orgChild = container.firstChild;
                
                container.opener = that;
                
                that.rcCont = container;
                
                if (!rc.list) {
                    rc.list = [];
                }
                
                if (cn.toString().match(rc.re.pos)) {
                    
                    st = parseInt(document.getViewPort().top + parseInt((window.innerHeight / 2) - (container.offsetHeight / 2), 10), 10) + "px";
                    sl = parseInt(document.getViewPort().left + parseInt((window.innerWidth / 2) - (container.offsetWidth / 2), 10), 10) + "px";
                    
                    if (!(/x\-/).test(cn)) {
                        cn += " x-" + sl;
                    }
                    
                    if (!(/y\-/).test(cn)) {
                        cn += " y-" + st;
                    }
                    
                    rc.ce = {
                        that : that,
                        cont : container,
                        o : o,
                        pos : cn.toString().match(rc.re.pos),
                        posType : that.posType,
                        posId : posId,
                        flashObj : flashObj
                    };
                    
                    setTimeout(rc.position, 0);
                } else {
                    
                    st = parseInt(document.getViewPort().top + parseInt((window.innerHeight / 2) - (container.offsetHeight / 2), 10), 10) + "px";
                    sl = parseInt(document.getViewPort().left + parseInt((window.innerWidth / 2) - (container.offsetWidth / 2), 10), 10) + "px";
                    
                    if (parseInt(st, 10) < 0) {
                        st = 0;
                    }
                    
                    rc.ce = {
                        that : that,
                        cont : container,
                        o : o,
                        /*pos : "y=" + parseInt(o.top + that.offsetHeight, 10) + "px,x=" + o.left + "px",*/
                        pos : "y=" + st + ",x=" + sl,
                        posType : that.posType,
                        posId : posId,
                        flashObj : flashObj
                    };
                    
                    container.style.top = st;
                    container.style.left = sl;
                }
                
                if (corners) {
                    container.addCorners(height, width);
                }
                
                if (addClasses) {
                    rc.addClasses(container, addClasses);
                }
                
                if (drag) {
                    rc.addDragPanel(container);
                }
                
                if (width || height) {
                    container.style.overflow = "hidden";
                }
                
                if (!flashURL) {
                    if (width) {
                        container.style.width = width;
                    }
                    if (height) {
                        container.style.height = height;
                    }
                }
                
                if (buttons) {
                    rc.buttons.init(that, container);
                }
                
                if (zoom) {
                    rc.zoom.elem = container;
                    
                    rc.zoom.ce = rc.ce;
                    rc.zoom.dir = "in";
                    setTimeout(rc.zoom.init, 0);
                }
                
                rc.list.push(container);
                document.body.appendChild(container);
                
                if (!flashURL) {
                    if (document.forms.length > 1) {
                        if (window.orgurl) {
                            ou = window.orgurl.match(/(?:\?)[\w\/\:\.\&\=]+/).toString().replace(/^\?/, "").split("&");
                            
                            for (i = 0; i < ou.length; i += 1) {
                                name = ou[i].split("=")[0];
                                value = ou[i].split("=")[1];
                                
                                if (document.forms[1][name]) {
                                    document.forms[1][name].value = value;
                                }
                            }
                        }
                        
                        i = 0;
                        
                        $.validator.messages = {
		                    required: "Det här fältet måste fyllas i",
		                    email: "Fyll i en giltig e-post adress",
		                    
		                    remote: "Please fix this field.",
		                    url: "Please enter a valid URL.",
		                    date: "Please enter a valid date.",
		                    dateISO: "Please enter a valid date (ISO).",
		                    dateDE: "Bitte geben Sie ein gültiges Datum ein.",
		                    number: "Please enter a valid number.",
		                    numberDE: "Bitte geben Sie eine Nummer ein.",
		                    digits: "Please enter only digits",
		                    creditcard: "Please enter a valid credit card number.",
		                    equalTo: "Please enter the same value again.",
		                    accept: "Please enter a value with a valid extension." /*,
		                    maxlength: $.format("Please enter no more than {0} characters."),
		                    minlength: $.format("Please enter at least {0} characters."),
		                    rangelength: $.format("Please enter a value between {0} and {1} characters long."),
		                    range: $.format("Please enter a value between {0} and {1}."),
		                    max: $.format("Please enter a value less than or equal to {0}."),
		                    min: $.format("Please enter a value greater than or equal to {0}.")*/
	                    };
	                    
	                    sub = function (e) {
	                        var that = this;
	                        if (!$.browser.msie) {
	                            $(that).trigger("submit");
	                        }
	                        $(".rcContainer form input[name=sendButton]").die("click", sub);
	                    };
	                    
	                    $(".rcContainer form input[name=sendButton]").live("click", sub);
                        
                        $(".rcContainer form").each(function () {
                            $(this).submit(function (e) {
                                
                                $(this).validate({
                                    submitHandler : function (form) {
                                        var d = $(form).serialize(), a = form.action, orgFormId = form.id, cb;
                                        
                                        cb = function (data) {
                                            var myData = /<body[^>]*>([\S\s]*?)<\/body>/im.exec(data)[1], part, orgForm;
                                            part = /<div\sid\=['"]formResponseText["'][\s]*\>([\S\s]*?)<\/div>/.exec(myData)[0];
                                            
                                            orgForm = $("#" + orgFormId)[0];
                                            orgForm.style.height = orgForm.offsetHeight + "px";
                                            
                                            if (part) {
                                                $("#" + orgFormId).html(part);
                                            } else {
                                                $("#" + orgFormId).html(myData);
                                            }
                                        };
                                        
                                        $.post(a, d, cb, "html");
                                    }
                                });
                                
                                e.preventDefault();
                            });
                        });
                    }
                    
                    rc.callback();
                    
                    setTimeout(rc.checkIds, 50);
                } else {
                    setTimeout(rc.callback, 1000);
                }
            };
            
            rc.callback = function () {};
            
            if (!that.rcCont) {
            
                query = that.toString().match(/\?[\w\-\=\.\,\&]+/) ? that.toString().match(/\?[\w\-\=\.\,\&]+/).toString() : "";
                    
                if (isFlash) {
                    if (!width) {
                        width = "100%";
                    }
                    if (!height) {
                        height = "100%";
                    }
                    
                    container.hasFlash = true;
                    
                    flashContId = 0;
                    if (document.getElementById("nvflashContainer" + flashContId)) {
                        while (document.getElementById("nvflashContainer" + flashContId)) {
                            flashContId += 1;
                        }
                    }
                    $(container).html("<div id=\"nvflashContainer" + flashContId + "\" style=\"height:" + height + ";width:" + width + ";\"></div>");
                    flashURL = that;
                    rc.callback = function () {
                        $("#nvflashContainer" + flashContId).flash({
                            swf: flashURL,  
                            height: "100%",   
                            width: "100%",
                            params : {
                                "wmode" : "transparent",
                                "quality" : "high",
                                "menu" : "false",
                                "scale" : "exactfit",
                                "base" : ".",
                                "allowScriptAccess" : "always"
                            }
                        });
                    };
                    afterLoad();
    		        
                } else if (isImg) {
                    //container.innerHTML = "<img src=" + p + " />";
                    //afterLoad();
                    
                    // Marianne: testa om det här funkar /Petter 2009-04-07
                    // Tänk på att bildgalleriet laddas på ett annat ställe (rad 1126).
                    ni = document.createElement("img");
                    
                    if (document.addEventListener) { 
                    // The reason for not using jQuery here is because some versions of Opera does not handle onload on an <img> very well.
                        ni.addEventListener("load", function () {
                            if (this.offsetHeight) {
                                afterLoad();
                            } else {
                                rc.forOpera(ni, afterLoad);
                            }
                        }, false);
                    } else {
                        ni.attachEvent("onload", function () {
                            // Timeout used to make sure that the container is loaded. (kindof.) 
                            // CHANGE THIS TIME-VALUE IF NEEDED!!!!
                            setTimeout(afterLoad, 10);
                        });
                    }
                    
                    ni.src = p;
                    container.appendChild(ni);
                } else {
                    $(container).load(p + query + " " + h, afterLoad);
                }
            }
            
            e.preventDefault();
        },
        forOpera : function (img, func) {
            if (img.offsetHeight) {
                func();
            } else {
                setTimeout(function () {
                    rc.forOpera(img, func);
                }, 10);
            }
        },
        close : function (evt) {
            var e = evt ? evt : window.event, that = this;
            
            if (that.rcContainer.startSize) {
                rc.zoom.elem = that.rcContainer;
                rc.zoom.out();
            } else {
                rc.rcContainer = that.rcContainer;
                rc.rcLink = that;
                
                setTimeout(rc.remove, 1);
                setTimeout(rc.toDefault, 1);
            }
            
            e.preventDefault();
        },
        closeRc : function (e) {
            var that = this;
            
            rc.zoom.out(that);
            e.preventDefault();
        },
        toDefault : function () {
            var that = rc.rcLink;
            $(that).bind("click", rc.open);
            $(that).unbind("click", rc.close);
        },
        position : function () {
            var t, c, o, p, pt, pId, i, y, x, yUnit, xUnit, h, w;
            if (rc.ce.cont && !rc.ce.cont.innerHTML) {
                setTimeout(rc.position, 10);
            } else {
                t = rc.ce.that;
                p = rc.ce.pos;
                o = rc.ce.o;
                c = rc.ce.cont;
                pt = rc.ce.posType;
                pId = rc.ce.posId;
                
                if (!$.browser.msie && t.getElementsByTagName("img").length) {
                    t = rc.ce.that.getElementsByTagName("img")[0];
                    o = $(t).offset();
                }
                
                if (pt === "rtoe") { // relative to opening element
                    if (p && p.isArray) {
                        for (i = 0; i < p.length; i += 1) {
                            if (!(/\d/).test(p[i])) {
                                if ((/(top|middle|bottom)/).test(p[i])) {
                                    if ((/top/).test(p[i])) {
                                        c.style.top = parseInt(o.top - c.offsetHeight, 10) + "px";
                                    } else if ((/middle/).test(p[i])) {
                                        c.style.top = parseInt(o.top - (c.offsetHeight / 2) + (t.offsetHeight / 2), 10) + "px";
                                    } else if ((/bottom/).test(p[i])) {
                                        c.style.top = parseInt(o.top + t.offsetHeight, 10) + "px"; 
                                    }
                                } else if ((/(left|center|right)/).test(p[i])) {
                                    if ((/left/).test(p[i])) {
                                        c.style.left = parseInt(o.left - c.offsetWidth, 10) + "px";
                                    } else if ((/center/).test(p[i])) {
                                        c.style.left = parseInt(o.left - ((c.offsetWidth / 2) - (t.offsetWidth / 2)), 10) + "px";
                                    } else if ((/right/).test(p[i])) {
                                        c.style.left = parseInt(o.left + t.offsetWidth, 10) + "px"; 
                                    }
                                }
                            }
                        }
                    }
                    y = c.offsetTop;
                    x = c.offsetLeft;
                } else if (pt === "rtw") { // relative to (browser) window
                    
                    y = p.toString().match(/(?:(y\=)[\d\.]+)/g) ? p.toString().match(/(?:(y\=)[\d\.]+)/g).toString().replace(/y\=/, "") : null;
                    x = p.toString().match(/(?:(x\=)[\d\.]+)/g) ? p.toString().match(/(?:(x\=)[\d\.]+)/g).toString().replace(/x\=/, "") : null;
                    
                    if (!y) {
                        h = $(window).height();
                        if ((/y\=middle/).test(p)) {
                            y = parseInt((h / 2) - (c.offsetHeight / 2), 10);
                        } else {
                            y = 0;
                        }
                        yUnit = "px";
                    } else {
                        yUnit = p.toString().match(/(?:(y\=)[\d\.]+)(px|em|pt|%)/g).toString().replace(/y\=[\d|\.]+/, "");
                    }
                    
                    if (!x) {
                        w = $(window).width();
                        if ((/x\=center/).test(p)) {
                            x = parseInt((w / 2) - (c.offsetWidth / 2), 10);
                        } else {
                            x = 0;
                        }
                        xUnit = "px";
                    } else {
                        xUnit = p.toString().match(/(?:(x\=)[\d\.]+)(px|em|pt|%)/g).toString().replace(/x\=[\d|\.]+/, "");
                    }
                    
                    
                    c.style.top = y + yUnit;
                    c.style.left = x + xUnit;
                } else if (pt === "rtID") {
                    
                    y = p.toString().match(/(?:(y\=)[\d\.]+)/g) ? p.toString().match(/(?:(y\=)[\d\.]+)/g).toString().replace(/y\=/, "") : null;
                    x = p.toString().match(/(?:(x\=)[\d\.]+)/g) ? p.toString().match(/(?:(x\=)[\d\.]+)/g).toString().replace(/x\=/, "") : null;
                    
                    if (!y) {
                        h = pId.offsetHeight;
                        if ((/y\=middle/).test(p)) {
                            y = parseInt((h / 2) - (c.offsetHeight / 2), 10);
                        } else {
                            y = 0;
                        }
                        y = parseInt(y + pId.offsetTop, 10);
                        yUnit = "px";
                    } else {
                        yUnit = p.toString().match(/(?:(y\=)[\d\.]+)(px|em|pt|%)/g).toString().replace(/y\=[\d|\.]+/, "");
                    }
                    
                    if (!x) {
                        w = pId.offsetWidth;
                        if ((/x\=center/).test(p)) {
                            x = parseInt((w / 2) - (c.offsetWidth / 2), 10);
                        } else {
                            x = 0;
                        }
                        x = parseInt(x + pId.offsetLeft, 10);
                        xUnit = "px";
                    } else {
                        xUnit = p.toString().match(/(?:(x\=)[\d\.]+)(px|em|pt|%)/g).toString().replace(/x\=[\d|\.]+/, "");
                    }
                    
                    c.style.top = y + yUnit;
                    c.style.left = x + xUnit;
                }
                
                //c.style.visibility = "visible";
                
                if (!y && p) {
                    y = p.toString().match(/y-\d+/) ? p.toString().match(/y-\d+/).toString().replace(/\D/g, "") : null;
                    if (y) {
                        yUnit = p.toString().match(/y-\d+(em|px|pt|%)/) ? p.toString().match(/y-\d+(em|px|pt|%)/)[0].toString().replace(/y\-\d+/, "") : "px";
                        c.style.top = y + yUnit;
                    }
                }
                if (!x && p) {
                    x = p.toString().match(/x-\d+/) ? p.toString().match(/x-\d+/).toString().replace(/\D/g, "") : null;
                    
                    if (x) {
                        xUnit = p.toString().match(/x-\d+(em|px|pt|%)/) ? p.toString().match(/x-\d+(em|px|pt|%)/)[0].toString().replace(/x\-\d+/, "") : "px";
                        c.style.left = x + xUnit;
                    }
                }
                
                if (c.style.top && (parseInt(c.style.top, 10) < 0)) {
                    c.style.top = 0;
                }
                c.orgPos = {
                    y : parseInt($(c).position().top, 10),
                    x : parseInt($(c).position().left, 10)
                };
            }
        },
        checkRCPositions : function () {
            var list = rc.list, i;
            if (list) {
                for (i = 0; i < list.length; i += 1) {
                    if ((/position\=rtoe/).test(list[i].opener) && !(/(^|\s)isDragged(\s|$)/).test(list[i].className)) {
                        rc.ce = {
                            that : list[i].opener,
                            cont : list[i],
                            o : $(list[i].opener).offset(),
                            pos : list[i].opener.toString().match(rc.re.pos),
                            posType : list[i].opener.posType
                        };
                        rc.position();
                    }
                }
            }
        },
        zIndex : function () {
            var that = this, cZI = parseInt(getStyle(that, "z-index"), 10), hZI = parseInt(document.getZIndex(), 10);

            if (hZI > cZI) {
                that.style.zIndex = parseInt(hZI + 1, 10);
            }
        },
        isdragged : function () {
            var that = this;
            $(that).addClass("isDragged");
        },
        addCorners : function (height, width) {
            var that = (this === window) ? rc.rcContainer : this, t = $(that), cm, i, w, c;
            
            if (!rc.count) {
                rc.count = 0;
            }
            
            t.addClass("corners");
            t.addClass("corner" + rc.count);
            
            function ac(t) {
                t.wrapInner("<div class=\"right side\"><div class=\"left side\"><div class=\"wrapper\"></div></div>");
                t.prepend("<div class=\"cornersTop topBottom\"><div class=\"left\"><!-- ie6 fix --></div><div class=\"right\"><!-- ie6 fix --></div><div class=\"filler\"><!-- ie6 fix --></div><div class=\"mid\"><!-- ie6 fix --></div></div>");
                t.append("<div class=\"cornersBottom topBottom\"><div class=\"left\"><!-- ie6 fix --></div><div class=\"right\"><!-- ie6 fix --></div><div class=\"filler\"><!-- ie6 fix --></div><div class=\"mid\"><!-- ie6 fix --></div></div>");
                
                
                if ((/MSIE\s[7]/).test(navigator.userAgent)) {
                    setTimeout(function () {
                        cm = $(".corner" + rc.count + " .mid");
                        
                        var w = 0;
                        
                        for (i = 0; i < cm.length; i += 1) {
                            if (w <= 0){
                                w = (cm[i].parentNode.parentNode.offsetWidth - 20);
                                /* Make sure the w integer always is positive. */
                                w = (w <= 0 ? -w : w);
                            }
                            cm[i].style.width = w + "px";
                        }
                        
                        cm = $(".corner" + rc.count + " .filler");
                        for (i = 0; i < cm.length; i += 1) {
                            cm[i].style.width = w  + "px";
                        }
                        
                        rc.count += 1;
                        rc.checkRCPositions();
                    }, 1100);
                } else if ((/MSIE\s[6]/).test(navigator.userAgent)) {
                    cm = $(".corner" + rc.count);
                    c = $(".corner" + rc.count + " .wrapper");
                    for (i = 0; i < cm.length; i += 1) {
                        cm[i].style.width = "10px";
                        cm[i].style.width = parseInt(c[i].offsetWidth + 10, 10) + "px";
                        
                        ie6.cornerFix(cm[i]);
                    }
                        
                    rc.count += 1;
                    rc.checkRCPositions();
                } else {
                    rc.count += 1;
                    rc.checkRCPositions();
                }
            }
            
            if (!that.innerHTML) {
                rc.rcContainer = that;
                setTimeout(that.addCorners, 100);
            } else {
                ac(t);
            }
        },
        addDragPanel : function (container) {
            var dragPanel = document.createElement("div"), c = container.orgChild;
            
            dragPanel.className = "dragPanel";
            dragPanel.container = container;
            
            try {
                $(c.parentNode).prepend(dragPanel);
            }
            catch (ex) {
                
            }
            
            $(dragPanel).mousedown(rc.dragPanel);
            $(dragPanel).mouseup(rc.dropPanel);
            
        },
        dragPanel : function (evt) {
            var that = this, cont = that.container, zi = parseInt(document.getZIndex(), 10);
            cont.style.zIndex = parseInt(zi + 1, 10);
            $(cont).draggable();
            //$(cont).bind("drag", rc.isdragged);
            //$(cont).mousedown(rc.zIndex);
        },
        dropPanel : function () {
            var that = this, cont = that.container;
            
            $(cont).draggable("destroy");
            
            //$(cont).unbind("drag", rc.isdragged);
            //$(cont).unbind("mousedown", rc.zIndex);
        },
        addClasses : function (container, addClasses) {
            var cClass, i;
            for (i = 0; i < addClasses.length; i += 1) {
                cClass = addClasses[i].replace(/^addClass\=/, "");
                $(container).addClass(cClass);
            }
        },
        checkIds : function () {
            var ids = $("*[id]"), i, j, cId, checkIds, countIds = 0, orgNbr, newId;
            for (i = 0; i < ids.length; i += 1) {
                cId = ids[i].id;

                checkIds = $("*[id=" + cId + "]");

                if (checkIds.length > 1) {
                    countIds = 0;
                    for (j = 0; j < checkIds.length; j += 1) {
                        if (countIds) {
                            orgNbr = cId.match(/\d+$/g, "") ? parseInt(cId.match(/\d+$/g, ""), 10) : 0;
                            newId = parseInt(orgNbr + countIds, 10);
                            checkIds[j].id = checkIds[j].id.replace(/\d+$/, "") + newId;
                        }
                        countIds += 1;
                    }
                }
            }
            rc.checkRCPositions();
        },
        te : function () {
            //alert("rc.te-f");
        }
    };
    
    rollercoaster.print = function () {
        var newHTMLStart = "<html><head><title>Print</title></head><body>", newHTMLEnd = "</body></html>", that = this, pHTML = "", win;
        
        if (that.parentNode.orgChild && that.parentNode.orgChild.tagName && that.parentNode.orgChild.tagName.toLowerCase() === "img") {
            pHTML = newHTMLStart + that.parentNode.orgChild.outerHTML + newHTMLEnd;
            pHTML = pHTML.replace(/width\:(\s+)?100%/, "");
        } else {
            pHTML = newHTMLStart + that.parentNode.orgChild.innerHTML + newHTMLEnd;
        }
        
        
        win = window.open();
        self.focus();
        win.document.open();
        win.document.write(pHTML);
        win.document.close();
        win.print();
        win.close();
    };
    
    rollercoaster.buttons = {
        init : function (that, container) {
            var cn = that.className, buttons, i;
            
            if ((/rcGallery\d/).test(that.className)) {
                cn = document.getElementById(that.className.match(/(^|\s)rcGallery\d+(\s|$)/g).toString().trim()).className;
            }
            
            buttons = (/(^|\s)buttons\-([a-z]+(\,)?)+(\s|$)/).test(cn) ? cn.match(/(^|\s)buttons\-([a-z]+(\,)?)+(\s|$)/g).toString().trim().replace(/^buttons\-/, "").split(",") : [];
                    
            if (!(/close/).test(buttons.toString())) {
                buttons.push("close");
            }
            
            for (i = 0; i < buttons.length; i += 1) {
                if (rc.buttons.add[buttons[i]]) {
                    rc.buttons.add[buttons[i]](container);
                }
            }
        },
        add : {
            close : function (elem) {
                var that = (this === window) ? rc.rcContainer : this, t = $(that), i, cbs, pn;
            
                function ab() {
                    
                    t.prepend("<a class=\"closeButton\" href=\"#\">" + rc.lang.close + "</a>");
                    
                    cbs = $(".rcContainer .closeButton").click(rc.closeRc);
                    for (i = 0; i < cbs.length; i += 1) {
                        pn = cbs[i].parentNode;
                        while (!$(pn).hasClass("rcContainer")) {
                            pn = pn.parentNode;
                        }
                        cbs[i].orgParent = pn;
                    }
                    
                    cbs = $(".rcGalleryContainer .closeButton").click(rc.gallery.close);
                    for (i = 0; i < cbs.length; i += 1) {
                        pn = cbs[i].parentNode;
                        while (!$(pn).hasClass("rcGalleryContainer")) {
                            pn = pn.parentNode;
                        }
                        cbs[i].orgParent = pn;
                    }
                }
                
                if (!that.innerHTML) {
                    rc.rcContainer = elem;
                    setTimeout(rc.buttons.add.close, 10);
                } else {
                    ab();
                }
            },
            print : function (elem) {
                var that = (this === window) ? rc.rcContainer : this, t = $(that), i, cbs, pn;
            
                function ab() {
                 
                    t.prepend("<a class=\"printButton\" href=\"#\">" + rc.lang.print + "</a>");
                    
                    cbs = $(".rcContainer .printButton").click(rc.print);
                    for (i = 0; i < cbs.length; i += 1) {
                        pn = cbs[i].parentNode;
                        while (!$(pn).hasClass("rcContainer")) {
                            pn = pn.parentNode;
                        }
                        cbs[i].orgParent = pn;
                    }
                    
                    cbs = $(".rcGalleryContainer .printButton").click(rc.print);
                    for (i = 0; i < cbs.length; i += 1) {
                        pn = cbs[i].parentNode;
                        while (!$(pn).hasClass("rcGalleryContainer")) {
                            pn = pn.parentNode;
                        }
                        cbs[i].orgParent = pn;
                    }
                }
                
                if (!that.innerHTML) {
                    rc.rcContainer = elem;
                    setTimeout(rc.buttons.add.print, 10);
                } else {
                    ab();
                }
            }
        }
    };
    
    rollercoaster.zoom = {
        speed : 30,
        getStart : {
            size : function (op) {
                var s = {}, elem = op.getElementsByTagName("img").length ? op.getElementsByTagName("img")[0] : op;
                
                s.width = elem.offsetWidth;
                s.height = elem.offsetHeight;
                
                return s;
            },
            pos : function (op) {
                var p = {}, elem = op.getElementsByTagName("img").length ? op.getElementsByTagName("img")[0] : op;
                
                p.top = parseInt($(elem).offset().top, 10);
                p.left = parseInt($(elem).offset().left, 10);
                
                return p;
            }
        },
        init : function () {
            var cbd = $(rc.zoom.elem).find(".cornersBottom");
            
            
            rc.zoom.elem.finalSize = {
                width : rc.zoom.elem.offsetWidth,
                height : rc.zoom.elem.offsetHeight
            };
            
            if (cbd.length) {
                rc.zoom.elem.finalSize.height = parseInt(cbd[0].offsetTop + cbd[0].offsetHeight, 10);
            }
            
            rc.zoom.elem.finalPos = {
                top : parseInt($(rc.zoom.elem).offset().top, 10),
                left : parseInt($(rc.zoom.elem).offset().left, 10)
            };
            
            rc.zoom.elem.startSize = rc.zoom.getStart.size(rc.zoom.elem.opener);
            rc.zoom.elem.startPos = rc.zoom.getStart.pos(rc.zoom.elem.opener);
            
            if (rc.zoom.dir === "in") {

                rc.zoom.elem.style.width = rc.zoom.elem.startSize.width + "px";
                rc.zoom.elem.style.height = rc.zoom.elem.startSize.height + "px";

                rc.zoom.elem.style.top = rc.zoom.elem.startPos.top + "px";
                rc.zoom.elem.style.left = rc.zoom.elem.startPos.left + "px";

                if (rc.zoom.elem.getElementsByTagName("img").length && rc.zoom.elem.getElementsByTagName("img").length === 1) {
                    rc.zoom.elem.getElementsByTagName("img")[0].style.width = "100%";
                    //rc.zoom.elem.getElementsByTagName("img")[0].style.height = "100%";
                }
                
                if (document.getZIndex) {
                    rc.zoom.elem.style.zIndex = document.getZIndex();
                }
                
                setTimeout(rollercoaster.zoom["in"], 0);
            }
        },
        "in" : function () {
            rc.zoom.elem.style.visibility = "visible";
            if ($.browser.msie) {
                rc.zoom.elem.style.filter = "alpha(opacity=10)";
                rc.zoom.elem.style.backgroundColor = "#fff";
            } else {
                rc.zoom.elem.style.opacity = ".1";
            }
                
            rc.callback = rc.gallery.zoom.callback;
            
            $(rc.zoom.elem).animate({
                width : rc.zoom.elem.finalSize.width + "px",
                height : rc.zoom.elem.finalSize.height + "px",
                top : rc.zoom.elem.finalPos.top + "px",
                left : rc.zoom.elem.finalPos.left + "px",
                opacity : "1"
            }, 500, rc.callback);
        },
        out : function (that) {
            rc.zoom.elem = that.orgParent;
            
            if (that.orgParent.orgChild.getElementsByTagName("object").length) {
                that.orgParent.orgChild.innerHTML = "";
            }
            
            if ($.browser.msie) {
                rc.zoom.elem.style.backgroundColor = "#fff";
            }
            
            $(".rcOvly").fadeOut("slow");
            
            $(rc.zoom.elem).animate({
                width : rc.zoom.elem.startSize.width + "px",
                height : rc.zoom.elem.startSize.height + "px",
                top : rc.zoom.elem.startPos.top + "px",
                left : rc.zoom.elem.startPos.left + "px",
                opacity : "hide"
            }, 500, rc.gallery.zoom.remove);
        },
        remove : function () {
            $(rc.zoom.elem).remove();
            rc.overlay.remove();
            $(document.body).removeClass("rollercoaster");
        }
    };
    
    rollercoaster.overlay = {
        add : function () {
            var ovly = document.createElement("div");
            ovly.className = "rcOvly";
            
            if (document.getZIndex) {
                ovly.style.zIndex = document.getZIndex();
            }
            
            document.body.appendChild(ovly);
            
            if ($.browser.msie) {
                ovly.style.filter = "alpha(opacity=0)";
            } else {
                ovly.style.opacity = ".0";
            }
            
            if (ovly.offsetHeight < document.body.offsetHeight) {
                ovly.style.height = parseInt(document.body.offsetHeight + 20, 10) + "px";
            }
        },
        remove : function () {
            $(".rcOvly").remove();
        }
    };
    
    rollercoaster.lang = {
        init : function () {
            var l = $("html").attr("lang"), url = "/lang/lang_" + l.toUpperCase() + ".xml", xml, setLang, txt, keys;
            
            for (keys in rc.lang["default"]) {
                if (typeof rc.lang["default"][keys] === "string") {
                    rc.lang[keys] = rc.lang["default"][keys];
                }
            }
            
            setLang = function (data, textStatus) {
                xml = $(data).find("rollercoaster").find("*").each(function () {
                    txt = this.textContent ? this.textContent : this.text;
                    rc.lang[this.tagName] = txt;
                });
                    
                if ($(".rcGalleryShow").length && rc.lang.show) {
                    $(".rcGalleryShow").each(function () {
                        this.innerHTML = rc.lang.show;
                    });
                }
            };
            
            $.get(url, {}, setLang, "xml");

        },
        "default" : {
            close : "Stäng",
            print : "Skriv ut",
            show : "Visa bildspel",
            play : "Spela upp bildspelet",
            stop : "Stanna bildspelet",
            previous : "Föregående",
            next : "Nästa"
        } 
    };
    
    rollercoaster.gallery = {
        init : function () {
            $(".rcGallery a").addClass("rcGalleryLink").click(rc.gallery.open);
            this.addClasses.init();
            this.button.init();
        },
        addClasses : {
            init : function () {
                var gs = $(".rcGallery"), i;
                for (i = 0; i < gs.length; i += 1) {
                    this.add(gs[i]);
                }
            },
            add : function (g) {
                var i, galCount, allLinks;
                
                if (!rc.gallery.galleries) {
                    rc.gallery.galleries = {};
                    galCount = 0;
                } else {
                    while (rc.gallery.galleries[galCount]) {
                        galCount += 1;
                    }
                }
                
                rc.gallery.galleries[galCount] = {};
                rc.gallery.galleries[galCount].links = $(g).find("a");
                
                g.info = rc.gallery.galleries[galCount];
                
                g.id = "rcGallery" + galCount;
                
                allLinks = $(g).find("a").addClass("rcGallery" + galCount);
                
                for (i = 0; i < allLinks.length; i += 1) {
                    $(allLinks[i]).addClass("rcGallery" + galCount + "Link" + i);
                }
                
                $(".rcGallery").removeClass("rcHidden");
            }
        },
        button : {
            init : function () {
                var gs = $(".rcGallery"), i;
                for (i = 0; i < gs.length; i += 1) {
                    this.create(gs[i], i);
                }
            },
            create : function (gl, i) {
                var gal = document.getElementById("rcGallery" + i), txt;
                
                if (gal.info.links.length === 1) {
                    gal.oneImg = true;
                    $(gl).append("<div class=\"rcGalleryButton\"></div>");
                } else {
                    gal.oneImg = false;
                    
                    txt = rc.lang.show ? rc.lang.show : "";
                    
                    $(gl).append("<div class=\"rcGalleryButton\"><a href=\"#\" class=\"rcGalleryShow rcGallery" + i + "Button\">" + txt + "</a></div>");
                }
                $(".rcGalleryButton a").click(rc.gallery.open);
            }
        },
        create : function (that) {
            var gal = document.createElement("div"), cg, opGal = document.getElementById(that.className.match(/rcGallery\d+/).toString()), rcCaption, imgNbr, cn = opGal.className, maxHeight = 0,
            addClasses = (/(^|\s)addClasses\-([\w]+(,)?)+(\s|$)/).test(cn) ? cn.match(/(^|\s)addClasses\-([\w]+(,)?)+(\s|$)/).toString().replace(/(addClasses\-|,\s[\w|,]+$)/g, "").split(",") : [];
            gal.className = "rcGalleryContainer";
            gal.id = that.className.match(/rcGallery\d+/).toString() + "Layer"; // Notice the "Layer" part of the ID...
            
            document.body.appendChild(gal);
            gal.style.visibility = "hidden";
            
            if ((/#$/).test(that)) {
                cg = document.getElementById(that.className.match(/rcGallery\d+/g).toString().trim());
                that = $(cg).find("a")[0];
                rc.gallery.startSlideShow = true;
            }
            
            if (!cg) {
                cg = document.getElementById(that.className.match(/rcGallery\d+/).toString());
            }
            
            $(cg).find("a").each(function () {
                var ni = document.createElement("img");
                ni.src = this;
                ni.style.position = "absolute";
                ni.style.top = "-100000px";
                
                document.body.appendChild(ni);
                
                if (ni.offsetHeight > maxHeight) {
                    maxHeight = ni.offsetHeight;
                }   
                
                document.body.removeChild(ni);
            });
            
            if (maxHeight) {
                $(gal).html("<div id=\"" + gal.id + "ImgWrapper\" style=\"height:" + maxHeight + "px\" class=\"rcGalleryWrapper\"><img id=\"" + gal.id + "Img\" src=\"" + that + "\"></div>");
            } else {
                $(gal).html("<div id=\"" + gal.id + "ImgWrapper\" class=\"rcGalleryWrapper\"><img id=\"" + gal.id + "Img\" src=\"" + that + "\"></div>");
            }
            
            rcCaption = $(opGal).find(".rcCaption");
            if (rcCaption.length) {
                
                imgNbr = that.className.match(/(^|\s)rcGallery\d+Link\d+(\s|$)/g).toString().match(/\d+$/g);
                opGal.info.cap = rcCaption;
                
                $(gal).append("<span class=\"rcCaption\">" + opGal.info.cap[parseInt(imgNbr, 10)].innerHTML + "</span>");
            }
                
            rc.ce = {
                that : that,
                cont : gal,
                o : $(gal.firstChild).offset()
            };
            
            gal.orgChild = gal.firstChild;
            
            if (!opGal.oneImg) {
                rc.gallery.nav.init(gal.firstChild);
            }
            
            gal.addCorners = rc.addCorners;
            
            gal.addCorners();            
            rc.addDragPanel(gal);
            
            //rc.buttons.add.close(gal);
            rc.buttons.init(that, gal);
            rc.addClasses(gal, addClasses);
            
            
            if (!rc.list) {
                rc.list = [];
            }
            
            rc.list.push(gal);
            
            return gal;
        },
        open : function (evt) {
            var that = this, gal, gid = that.className.match(/rcGallery\d+/).toString() + "Layer", e = evt ? evt : window.event;
            
            $(document.body).addClass("rollercoaster");
            
            rc.overlay.add();
            
            if (!document.getElementById(gid)) {
                gal = rc.gallery.create(that);
                rc.gallery.position(gal);
            } else {
                document.getElementById(gid + "Img").src = that;
            }
            
            if (gal) {
                gal.style.zIndex = document.getZIndex();
                rc.zoom.elem = gal;
                rc.zoom.elem.opener = document.getElementById(that.className.match(/rcGallery\d+/).toString());
                rc.zoom.ce = rc.ce;
                rc.zoom.dir = "in";
                
                setTimeout(rc.gallery.zoom.init, rc.zoom.speed);
            }
            e.preventDefault();
        },
        close : function (e) {
            var that = this;
            
            rc.zoom.elem = that.orgParent;
            rc.gallery.zoom.out(that);
            
            clearTimeout(rc.gallery.play);
            
            e.preventDefault();
        },
        position : function (gal) {
            var st, sl;
            
            st = parseInt(document.getViewPort().top + parseInt((window.innerHeight / 2) - (gal.offsetHeight / 2), 10), 10) + "px";
            sl = parseInt(document.getViewPort().left + parseInt((window.innerWidth / 2) - (gal.offsetWidth / 2), 10), 10) + "px";
            
            if (parseInt(st, 10) < 0) {
                st = 0;
            }
            
            gal.style.top = st;
            gal.style.left = sl;
            
            if (parseInt(gal.style.top, 10) < 0) {
                gal.style.top = "0px";
            }
        },
        nav : {
            init : function (galFC) {
                var t;
                
                $(galFC.parentNode).append("<div class=\"navpanel " + galFC.id.replace(/Wrapper/, "") + "\"><a href=\"#\" class=\"back\">" + rc.lang.previous + "</a><a href=\"#\" class=\"next\">" + rc.lang.next + "</a><a href=\"#\" class=\"play startLoop\">" + rc.lang.play + "</a></div>");
                $(".navpanel a.back").click(rc.gallery.nav.back);
                $(".navpanel a.play").click(rc.gallery.nav.play);
                $(".navpanel a.next").click(rc.gallery.nav.next);
                
                if (rc.gallery.startSlideShow) {
                    t = $(galFC.parentNode).find(".play")[0];
                    t.isPlaying = true;
                    t.innerHTML = rc.lang.stop; //"Stanna bildspelet";
                    $(t).addClass("stopLoop");
                    $(t).removeClass("startLoop");
                    rc.gallery.firstImg = true;
                    rc.gallery.play = setTimeout(rc.gallery.nav.galleryPlay, 2000);
                    rc.gallery.startSlideShow = false;
                }
            },
            getImg : function (that, gal) {
                var imgs, i, cImg, ind = 0, info, ci, caps;
                while (!gal.opener) {
                    gal = gal.parentNode;
                }
                
                imgs = gal.opener.info.links;
                ci = that.parentNode.className.match(/(^|\s)rcGallery\d+LayerImg(\s|$)/g).toString().trim();
                cImg = document.getElementById(ci); //that.parentNode.previousSibling;
                
                for (i = 0; i < imgs.length; i += 1) {
                    if (cImg.src.toString() === imgs[i].toString()) {
                        ind = i;
                        break;
                    }
                }
                
                caps = gal.opener.info.cap;
                
                info = {
                    img : cImg,
                    imgs : imgs,
                    index : ind,
                    cap : caps
                };
                
                return info;
            },
            back : function (e) {
                var that = this, gal = that.parentNode, im, orgIndex;
                
                im = rc.gallery.nav.getImg(that, gal);
                
                orgIndex = im.index;
                im.index -= 1;
                
                if (im.index < 0) {
                    im.index = im.imgs.length - 1;
                }
                
                im.img.src = im.imgs[im.index];
                rc.gallery.tween(orgIndex, im, im.img.parentNode, im.img);
                
                if (im.cap && im.cap[im.index] && $(that.parentNode.parentNode).find(".rcCaption").length) {
                    $(that.parentNode.parentNode).find(".rcCaption")[0].innerHTML = im.cap[im.index].innerHTML;
                }
                
                e.preventDefault();
            },
            next : function (e) {
                var that = this, gal = that.parentNode, im, orgIndex;
                
                while (!im) {
                    im = rc.gallery.nav.getImg(that, gal);
                }
                
                orgIndex = im.index;
                im.index += 1;
                
                if (im.index > im.imgs.length - 1) {
                    im.index = 0;
                }
                
                im.img.src = im.imgs[im.index];
                rc.gallery.tween(orgIndex, im, im.img.parentNode, im.img);
                
                if (im.cap && im.cap[im.index] && $(that.parentNode.parentNode).find(".rcCaption").length) {
                    $(that.parentNode.parentNode).find(".rcCaption")[0].innerHTML = im.cap[im.index].innerHTML;
                }
                
                e.preventDefault();
            },
            play : function (e) {
                
                if (!this.clicks) {
                    this.clicks = [];
                }
                
                this.clicks.push(this.isPlaying);
                
                if (!this.isPlaying) {
                    this.isPlaying = true;
                    this.innerHTML = rc.lang.stop;
                    $(this).addClass("stopLoop");
                    $(this).removeClass("startLoop");
                    rc.gallery.play = setTimeout(rc.gallery.nav.galleryPlay, 10);/*changed from 2000*/
                } else {
                    this.isPlaying = false;
                    this.innerHTML = rc.lang.play;
                    $(this).removeClass("stopLoop");
                    $(this).addClass("startLoop");
                    clearTimeout(rc.gallery.play);
                }
                      
                e.preventDefault();
            },
            galleryPlay : function () {
                $(".navpanel a.next").trigger("click");
                rc.gallery.play = setTimeout(rc.gallery.nav.galleryPlay, 2000);
            }
        },
        tween : function (oi, im, w, i) {
            var tweenImg = document.createElement("img");
            tweenImg.src = im.imgs[oi];
            tweenImg.id = "tweenImg";
            
            tweenImg.style.top = 0;
            tweenImg.style.left = 0;
            
            document.body.appendChild(tweenImg);
            w.appendChild(tweenImg);
            
            $(tweenImg).fadeOut(500, rc.gallery.removeTween);
        },
        removeTween : function () {
            $("#tweenImg").remove();
        },
        zoom : {
            init : function () {
                var o = $(rc.zoom.elem).offset(), op = $(rc.zoom.elem.opener).offset(), zI = 0;
                
                rc.zoom.elem.finalSize = {
                    height : rc.zoom.elem.offsetHeight,
                    width : rc.zoom.elem.offsetWidth
                };
                
                rc.zoom.elem.finalPos = {
                    top : o.top,
                    left : o.left
                };
                
                rc.zoom.elem.startSize = {
                    height : rc.zoom.elem.opener.offsetWidth,
                    width : rc.zoom.elem.opener.offsetHeight
                };
                
                rc.zoom.elem.startPos = {
                    top : parseInt(op.top, 10),
                    left : parseInt(op.left, 10)
                };
                
                rc.zoom.elem.style.top = op.top + "px";
                rc.zoom.elem.style.left = op.left + "px";
                
                rc.zoom.elem.style.width = rc.zoom.elem.opener.offsetWidth + "px";
                rc.zoom.elem.style.height = rc.zoom.elem.opener.offsetHeight + "px";
                
                if (rc.zoom.elem.getElementsByTagName("img").length && rc.zoom.elem.getElementsByTagName("img").length === 1) {
                    rc.zoom.elem.getElementsByTagName("img")[0].style.width = "100%";
                }
                
                rc.callback = rc.gallery.zoom.callback;
                
                if ((/MSIE\s\d/).test(navigator.userAgent)) {
                    zI = document.getZIndex();
                    rc.zoom.elem.style.zIndex = zI;
                    setTimeout(rc.gallery.zoom["in"], 10);
                } else {
                    setTimeout(rc.gallery.zoom["in"], 10);
                }
            },
            callback : function () {
                var m, i;
                rc.zoom.elem.style.height = "";
                rc.zoom.elem.style.filter = "";
                rc.zoom.elem.style.opacity = "";
                rc.zoom.elem.style.backgroundColor = "transparent";
                
                if ((/MSIE\s[5-7]/).test(navigator.userAgent)) {
                    m = $(rc.zoom.elem).find(".filler,.mid");
                    for (i = 0; i < m.length; i += 1) {
                        m[i].style.width = (m[i].parentNode.offsetWidth - 20) + "px";
                    }
                }
            }, 
            "in" : function () {
                rc.zoom.elem.style.visibility = "visible";
                if ($.browser.msie) {
                    rc.zoom.elem.style.filter = "alpha(opacity=10)";
                    rc.zoom.elem.style.backgroundColor = "#fff";
                } else {
                    rc.zoom.elem.style.opacity = ".1";
                }
                
                $(".rcOvly").animate({
                    "opacity" : "0.7"
                }, 500);
                
                $(rc.zoom.elem).animate({
                    width : rc.zoom.elem.finalSize.width + "px",
                    height : rc.zoom.elem.finalSize.height + "px",
                    top : rc.zoom.elem.finalPos.top + "px",
                    left : rc.zoom.elem.finalPos.left + "px",
                    opacity : "1"
                }, 500, rc.callback);
            },
            out : function (that) {
                rc.zoom.elem = that.orgParent;
                
                if ($.browser.msie) {
                    rc.zoom.elem.style.backgroundColor = "#fff";
                }
                
                $(".rcOvly").fadeOut("slow");
                
                $(rc.zoom.elem).animate({
                    width : rc.zoom.elem.startSize.width + "px",
                    height : rc.zoom.elem.startSize.height + "px",
                    top : rc.zoom.elem.startPos.top + "px",
                    left : rc.zoom.elem.startPos.left + "px",
                    opacity : "hide"
                }, 500, rc.gallery.zoom.remove);
            },
            remove : function () {
                rc.zoom.elem.opener.rcCont = null;
                $(rc.zoom.elem).remove();
                rc.overlay.remove();
                $(document.body).removeClass("rollercoaster");
            }
        }
    };
    
    rc = rollercoaster;
    rollercoaster.lang.init();
    rollercoaster.gallery.init();
    rollercoaster.init();
   })();
