
document.addEvent('domready',function(){
	TVFox.domReady();
});

window.addEvent('load',function(){
	TVFox.windowLoad();
});

var TVFox = {
	fxMarqueeCaption : null,
	imgPathMenuBottomUp : null,
	imgPathMenuBottomDown : null,
    
    loading : false,
    animating : false,
	
	windowLoad : function(){
		$('loading').destroy();
		$('content').setStyle('display','');	
		
		//check if there is station from the querystring
		if(TVFox.station.url){
			TVFox.play();
		}
		
	},
	
	domReady : function(){
		
		this.imgPathMenuBottomUp = TVFox.domain + 'img/menu_bottom_up.jpg';
		this.imgPathMenuBottomDown = TVFox.domain + 'img/menu_bottom_down.jpg';
        
		
		this.setMenusHoverEffect();
		this.setMenuItemsHoverEffect();

		this.createFxMarquee();
		
		if(!this.station.url){
			this.fxMarqueeCaption.announce({
				message: 'Live TV',
				delay: 1, 
				revert: false
			});
		}
		

		
        
		//autocomple search:
		new Autocompleter.Local('txtSearch', TVFoxSearch.tokens, {
			'minLength': 2, // We wait for at least one character
			'overflow': false, // Overflow for more entries
			'filterSubset' : true,
			'selectMode': 'pick'                        
		});
		

		
		$('txtSearch').addEvent('keydown',function(event){
			if(event.key == 'enter'){
				TVFoxSearch.submit();
			}
		});
		
	},
    
    setMenusHoverEffect : function(elParent){
        
        if(!elParent){
            elParent = document;
        }
        
        var menus = elParent.getElements('.mt-menu');
        menus.each(function(el) {
                el.addEvent('mouseenter', function(event) {
                    el.tween('color','#FF0000');
                });
        
                el.addEvent('mouseleave', function(event) {
                    el.tween('color', '#47494B');
                });
            });    
    },
    
    setMenuItemsHoverEffect : function(elParent){   
        
        if(!elParent){
            elParent = document;
        }
        
        var menuItems = elParent.getElements('.mt-menuitem');
        menuItems.each(function(el) {
                el.addEvent('mouseenter', function(event) {
                    el.tween('color','#000E79');
                });
        
                el.addEvent('mouseleave', function(event) {
                    el.tween('color', '#47494B');
                });
            });    
    },
    
    createFxMarquee : function(){
        if(!this.fxMarqueeCaption){
            this.fxMarqueeCaption = new Fx.Marquee($('spnNowPlaying'), {
                duration: 500,
                showEffect: {
                    top: [0,0], //reset the top every time to zero
                    left: [-100, 0],
                    opacity: [0,1]
                },
                hideEffect: {
                    top: [0,20]
                },
                revertEffect: {
                    top: [-30, 0],
                    left: [0,0] //reset the left back to zero on revert
                }
            });
         }
    },
    
    notLegalClick : function(){
        return this.loading || this.animating;       
    },
	
	toggleMenu : function(identifier){
        if(this.notLegalClick())return;
        
        var elMenu = $('div' + identifier);
        var elContainer = elMenu.getLast();
        var elLoading = elContainer.getFirst();
         
        var imgCloseOpen = $('img' + identifier + 'closeopen');
		
        if(elLoading.get('class') == 'mt-loading-menu'){
            this.loading = true;
            
            elLoading.set('reveal', {
                onComplete : function(){ 
                    new Request.HTML({
                        url: TVFox.domain + 'ajax/menu.php?identifer=' + identifier,
                        method: 'get',
                        update: elContainer,
                        noCache: true,
                        onComplete: function(){
                            TVFox.loading = false;
                            elMenu.store('open',true); 
                            imgCloseOpen.set('src',TVFox.imgPathMenuBottomUp);
                            
                            TVFox.setMenusHoverEffect(elContainer); 
                        }
                    }).send();    
                }
            });

            elLoading.reveal(); 
        }
        else{  
            this.animating = true;
            var fxr = new Fx.Reveal(elMenu,{
                onComplete : function(el){
                    TVFox.animating = false;
                    elMenu.store('open',!elMenu.retrieve('open'));
                    var img = elMenu.retrieve('open') ? TVFox.imgPathMenuBottomUp : TVFox.imgPathMenuBottomDown;
                    imgCloseOpen.set('src',img);    
                }
            });
            
            fxr.toggle();
        }
	},
    
    toggleMenuItem : function(el,identifier){
        if(this.notLegalClick())return;
        
        var hashId = Base64.encode(el.get('html').toLowerCase());
        var elMenuItems = el.getParent().getNext().getNext();
        var elLoading = elMenuItems.getFirst();
        
        if(elLoading.get('class') == 'mt-loading-menu-item'){
            this.loading = true;

            elLoading.set('reveal', {
                onComplete : function(){ 
                    new Request.HTML({
                        url: TVFox.domain + 'ajax/menuitem.php?hashId=' + hashId + '&identifer=' + identifier,
                        method: 'get',
                        update: elMenuItems,
                        noCache: true, 
                        onComplete: function(){
                            TVFox.loading = false;
                            TVFox.setMenuItemsHoverEffect(elMenuItems);
                        }
                    }).send();    
                }
            });
           
            elLoading.reveal();          
        }
        else{
            this.animating = true;
            var fxr = new Fx.Reveal(elMenuItems,{
                onComplete : function(el){
                    TVFox.animating = false;
                }
            });
            
            fxr.toggle();    
        }      
    },
    
	toggleSubMenu : function(id){	
		new Fx.Reveal($(id)).toggle();
	},
    
    menuItem : function(el){
        //go in the dom to find the correct captions:
        //see 'gui.php' the html layout
        var station = el.get('html');
        var category = el.getParent().getParent().getPrevious().getPrevious().getLast().get('html');
        
        station = this.getQSKey(station);
        category = this.getQSKey(category);
        
        if(TVFox.domain.indexOf('localhost') != -1){
            location.href = TVFox.domain + '?category=' + category + '&station=' + station;
        }
        else{
            location.href = TVFox.domain + category + '/' + station;    
        }
    },
    
    getQSKey : function(str){
        return str.toLowerCase().replace(/ /g,'_');    
    },
	
	play : function(){
        this.createFxMarquee();
        var url = Base64.decode(TVFox.station.url);
			
		$('imgNowPlaying').set('src',TVFox.domain + 'site/img/' + TVFox.station.categoryImage);
		TVFox.fxMarqueeCaption.announce({
				message: TVFox.getPlayerHTMLCaption(TVFox.station.stationName,TVFox.station.categoryName),
				revert: false
		});   
		
		$('iframePlayer').set('src',url);	
	},
	
	getPlayerHTMLCaption : function(stationName,CategoryName){
		return '<span style="color:#FD671B;">' + CategoryName + '</span> - ' + stationName;
	}
	
	/*getQSValue : function(key){
		var res = '';
		var reg = key + '=([^&]+)';
		var oReg = new RegExp(reg,'i');
		var match = oReg.exec(location.href);
		if(match){
			res = match[1];
		}
		
		return res;
	}*/
		
};


var TVFoxSearch = {
	
	submit : function(){
	 	var term = $('txtSearch').get('value');
	 	var term = TVFox.getQSKey(term);
        location.href = TVFox.domain + '?searchterm=' + term;    
        
        
    }
	
};


