var IE7 = (navigator.appVersion.search(/MSIE 7/) != -1) ? true : false;
var IE = Prototype.Browser.IE;


var Scroller = 
{
    element: false,
    inTransit: false,
    pages: [],
    curPage: 1,
    curInterval: null,
    curTimeout: null,
    prevPage: this.curPage,
    scrollers: null,
    options: 
    {
        width: false,
        rotate: false,
        animSpeed: 0.5,
        rotateSpeed: 1000,
        pauseInterval: 1000,
        className: '',
        tag: ''
    },
    
    init: function(id, options)
    {
        this.element = $(id);
        
        this.element.insert({top: this.element.childElements().last().cloneNode(true), bottom: this.element.childElements().first().cloneNode(true)});
        
        if (options)
        {
            if (options.className) this.options.className = '.'+options.className;
            if (options.tag) this.options.tag = options.tag;
            if (options.animSpeed) this.options.animSpeed = options.animSpeed;
            if (options.rotateSpeed) this.options.rotateSpeed = options.rotateSpeed;
            if (options.pauseInterval)
                this.options.pauseInterval = options.pauseInterval;
            else if (options.rotateSpeed)
                this.options.pauseInterval = options.rotateSpeed;
            if (options.rotate) this.options.rotate = options.rotate;
            
            this.options.width = (options.width) ? options.width : this.element.getWidth();
        }
        
        this.pages = $$(this.options.tag+this.options.className);

        if (options && options.scrollers) 
        {
            for(i=1; i<this.pages.length-1; i++)
                $(options.scrollers).insert('<div onclick="Scroller.scrollTo('+i+', false);"></div>');
            this.scrollers = $(options.scrollers).childElements();
            this.lightScrollers();
        }
        
        /*for (var i=0, l=this.pages.length-1; i<l; i++)
        {
            this.element
        }*/
        
        this.element.style.width = this.pages.length*this.options.width;
        this.element.style.left = '-' + this.options.width + 'px';    
        this.element.style.top = '0px';    
        this.element.style.position = 'relative';    
        
        if (this.options.rotate == true)
            this.startScroll();
        
    },
    
    stop: function()
    {
        this.options.rotate = false;
        this.stopScroll();
    },

    start: function()
    {
        this.options.rotate = true;
        this.startScroll();
    },

    stopScroll: function()
    {
        if(this.curInterval != null)
            clearInterval(this.curInterval);
    },
    
    startScroll: function(startNow)
    {
        if(startNow == undefined)
            startNow = false;
        this.stopScroll();
        this.curInterval = setInterval(function()
        {
            this.scroll(1);
        }.bind(this), this.options.rotateSpeed);
        if(startNow)
            this.scroll(1);
    },
    
    lightScrollers: function(light)
    {
        if(this.scrollers == null)
            return;
        if(light == undefined)
            light = true;

        this.scrollers.invoke('removeClassName', 'current');
        if(light)
            this.scrollers[this.curPage-1].addClassName('current');
        
    },
    
    scrollTo: function(page, atOnce)
    {
        if(atOnce == undefined)
            atOnce = true;

        if(this.inTransit)
            return;

        if(page < 0)
            return;
        if(page > (this.pages.length-1))
            page = page % this.pages.length;

        if(atOnce)
        {
//            this.lightScrollers(false);
            this.curPage = page;
            this.element.style.left = '-' + page*this.options.width + 'px';    
            this.lightScrollers();
        }
        else
        {
            this.scroll(page-this.curPage);
            this.stopScroll();
            if(this.curTimeout != null)
                clearTimeout(this.curTimeout); 
            if(this.options.rotate)
                this.curTimeout = setTimeout(function()
                {
                    this.startScroll(true);
                }.bind(this), this.options.pauseInterval);
        }
    },
    
    scroll: function(amount, restartInterval)
    {
        var realThis = this;
        if(restartInterval == undefined)
            restartInterval = false;
        
        amount = (amount > 0) ?
            (Math.abs(amount) % (this.pages.length-2)) :
            -(Math.abs(amount) % (this.pages.length-2));
        if(amount == 0)
            return;
            
        if((this.curPage + amount) < 0)
            amount = -this.curPage;
        else if((this.curPage + amount) > (this.pages.length-1))
            amount = this.pages.length-1-this.curPage;
        
        if(this.inTransit)
            return;
        this.inTransit = true;
//        this.lightScrollers(false);
        
        new Effect.Parallel(
        [
          new Effect.Move(realThis.element, {sync: true, x: -realThis.options.width*amount})
        ], 
        {
            duration: realThis.options.animSpeed,
            afterFinish: function()
            {                                
                realThis.inTransit = false;
                realThis.curPage += amount;
                if (realThis.curPage == (realThis.pages.length-1))
                    realThis.scrollTo(1);
                else if (realThis.curPage == 0)
                    realThis.scrollTo(realThis.pages.length-2);
                realThis.lightScrollers();
            }
        });
        
        if(this.options.rotate)
            if(restartInterval)
                this.startScroll();
            else
            {
                if(this.pages[this.curPage+1].readAttribute('interval') != null)
                {
                    this.stopScroll();
                    if(this.curTimeout != null)
                        clearTimeout(this.curTimeout); 
                    this.curTimeout = setTimeout(function()
                    {
                        this.startScroll(true);
                    }.bind(this), this.pages[this.curPage+1].readAttribute('interval'));
                }
            }
        
    }
    

}

function getDim()
{
    var result = {width:0, height:0, pxWidth:0, pxHeight:0};

    if (typeof window.innerWidth != 'undefined')
    {
        result.width = window.innerWidth;
        result.height = window.innerHeight;
        result.pxWidth = result.width + 'px';
        result.pxHeight = result.height + 'px';
    }
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0)
    {
        result.width = document.documentElement.clientWidth;
        result.height = document.documentElement.clientHeight;
        result.pxWidth = result.width + 'px';
        result.pxHeight = result.height + 'px';
    }
    
    return result;
}

function showOrderForm()
{
    var dim = getDim();
    var contactForm = $('contact_form')
 
    $('dark').setStyle({width: dim.pxWidth, height: dim.pxHeight, display: 'block'});
    
    contactForm.show();
    contactForm.setStyle({left: (dim.width-contactForm.getWidth())/2+'px', top: (dim.height-contactForm.getHeight())/2+'px'});    
}

var logic = 
{
    currentGalleryPage: 'b6',
    currentContactPage: '1',
    slideShowTpl: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="920" height="320"><param name="movie" value="slideshow/#{name}.swf" /><param name="quality" value="high" /><param name="allowFullScreen" value="false" /><param name="allowScriptAccess" value="always" /><param name="wmode" value="transparent"><embed src="slideshow/#{name}.swf" quality="high" type="application/x-shockwave-flash" WMODE="transparent" width="920" height="320" allowFullScreen="false" pluginspage="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="always" /></object>',
    blockRegistrationFinish: false
};

window.onload = function()
{
    if($('flash_fix') != null)
    {
        var flashMovie = document.createElement("div");
        flashMovie.innerHTML = $('flash_fix').innerHTML;
        $('flash_fix').replace(flashMovie); 
    }

    if($('slideshow') != null)
    {
        $('slideshow').innerHTML = new Template(logic.slideShowTpl).evaluate({name: "premium"});   
        
        $$('#buttons a').each(function(item)
        {
            item.observe('click', function()
            {
                if (logic.currentGalleryPage)
                {
                    var oldElement = $(logic.currentGalleryPage);
                    var slideShow = $('slideshow');
                    var itemName = item.href.split('#')[1];
                    oldElement.className = oldElement.id;    
                    item.className = item.id+'_active';
                    logic.currentGalleryPage = item.id;
                    
                    slideShow.innerHTML = new Template(logic.slideShowTpl).evaluate({name: itemName});
                }    
            });    
        }); 
    }
}



 
