var fxs = [];
var fx_curr = false;

Fx.Morph = Fx.Styles.extend({
 
    start: function(className){
 
        var to = {};
 
        $each(document.styleSheets, function(style){
            var rules = style.rules || style.cssRules;
            $each(rules, function(rule){
                if (!rule.selectorText.test('\.' + className + '$')) return;
                Fx.CSS.Styles.each(function(style){
                    if (!rule.style || !rule.style[style]) return;
                    var ruleStyle = rule.style[style];
                    to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
                });
            });
        });
        return this.parent(to);
    }
 
});
 
Fx.CSS.Styles = ["backgroundColor", "backgroundPosition", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];
 
Fx.CSS.Styles.extend(Element.Styles.padding);
Fx.CSS.Styles.extend(Element.Styles.margin);



window.addEvent('domready' , function(){
  
  $$('#SlideTickets .Ticket').each(function(el, index){
    //var fx = new Fx.Styles(el, {duration:200, wait:false});
    var fx = new Fx.Morph(el, {duration:200, wait:false});
    fxs[index] = fx;
    
    el.addEvent('click' , function(){
      go_through(index);
    });
  });
  
  if(fxs.length == 0) return;
  
  go_through(0);
  go_through.periodical(8000);
  
  PreloadImages();
});

var go_through = function Ticket_ChangeSelected(index){
  if(index == undefined){
    
    if (fx_curr < fxs.length - 1)
      index = fx_curr + 1;
    else
      index = 0;
  }
  
  
  if (fx_curr !== false){
    fxs[fx_curr].start('class1');
  }
  //if(window.ie)alert(index);
  
  fxs[index].start('class2')
  
  if(typeof(home_blocks) != "undefined"){
    $('FImage').setHTML(home_blocks[index].img);
    $('OText').setHTML(home_blocks[index].over);
  }
  //if(window.ie)return;
      //'padding-left':'9px'
  fx_curr = index;
}

function PreloadImages(){
    var Images = [];
    $each(home_blocks , function(item){
        $each(item , function(props){
            //Find everything starts with 'uploads/' and ends with '.jpg'
            $each(/uploads\/(.+?)\.jpg/.exec(props) , function(match){
                if(match.substring(0,7) == "uploads")
                    Images.push("/" + match);
            });
        });
    });
    
    new Asset.images(Images);
}

window.addEvent('domready' , function(){
    if(!$('login_button')) return true;
    $('login_button').addEvent('click' , function(ev){
        if(window.ie6) return;
        new Event(ev).stop();
        
        var PageBody = $E('body');
        /*var Box = new Element('iframe' , { 'src' : '/login_min/' ,'frameborder':'0' }).injectInside(
            
        )*/
        var InpBox = new Element("div" , {'id' : 'viframe_in'}).injectInside(
            new Element("div" , {'id' : 'viframe'}).injectInside(
                new Element("div" ,{'id':'overlay'}).setStyle('height',PageBody.getSize().size.y+'px').injectTop(PageBody).addEvent('click' , function(ev){
                    var targ;
                    if (!ev) var ev = window.event;
                    if (ev.target) targ = ev.target;
                    else if (ev.srcElement) targ = ev.srcElement;
                    if (targ.nodeType == 3) // defeat Safari bug
                        targ = targ.parentNode;
                        
                    if(targ.id != "overlay") return true;
                    $('overlay').remove();
                })
            )
        );
        
        
        new Ajax('/login_min/' , { 
            method:'get',
            update:InpBox,
            onComplete: function(cont){
                HookLogin();
            }
        }).request();
        /*Box.addEvent('load' , function(){
        
            var page_src = Box.contentDocument.location.toString();
            if(page_src.substring(page_src.length - 11 , page_src.length) != "/login_min/"){
                window.location = page_src;
            }
            //alert(page_src);
        });
        Box.contentDocument.onload = function(){
            alert("test");
        };*/
        
    });
});

function HookLogin(){
    var LoginErrorCells = $('LoginError');
    if(!LoginErrorCells) return false;
    var LoginForm = LoginErrorCells.getParent();
    
    LoginForm.addEvent('submit' , function(ev){
        new Event(ev).stop();
        var Jdata = {username: '' , password: '', action:'login'};
        $ES('input',LoginForm).each(function(el){
            if(el.name.contains("username"))
                Jdata.username = el.value;
            else if(el.name.contains("password"))
                Jdata.password = el.value;
        });
        
        new Json.Remote('/ajax.php',{
            'method' : 'post',
            onComplete : function(res){
                if(res.error){
                    LoginErrorCells.setHTML(res.error);
                } else if(res.redirect){
                    location.href = res.redirect;
                }
            }
        }).send(Jdata);
    });
    
}