
$(window).load(function(){
	Utils.init();
});

var Utils = {
	DEBUG : true,
	menuYloc: 160,
	arrowYoffset: 328,
	arrowYloc: 0,
	arrowOriginalY: 543,
	documentScrollTop: 0,
	showArrows: false,
	isiPad: false,
	isiPhone: false,
	browser: null,
	
	init: function(){
		Utils.browser = Utils.checkBrowser();
		Utils.controlLoadImages();
		Utils.checkPage();
		HashController.init();	
		Utils.controlMenu();
		Utils.placeholder();
		Utils.initSearch();
		Utils.initGrid();
		
		//Manage scroll
		Utils.arrowYloc = Math.max( $('body').height() - $( '#footer' ).height() - 1000, 100 );
		$(window).scroll(function () {Utils.checkScroll( false );});
		$(window).resize(function(){Utils.centerSearch(false);});
		
		Utils.checkScroll( true );
		Utils.initEventListeners();
		
		Utils.checkIe();
		if( $('body').attr('id') == 'body_monthly'){
			
		}
	},
	
	controlLoadImages: function(){
		$( '.wrapper_content > img' ).lazyload({
			effect : "fadeIn",
			event : "lazyloaded",
			effectspeed: 200
		});
		setTimeout(function(){
			$( '.wrapper_content > img' ).trigger( 'lazyloaded' );
		},300)
	},
	
	checkIe: function(){
		if( isFirstTime && Utils.browser == 'Internet Explorer'){
			$( '#ie_click' ).click();
		}
	},
	
	checkPage: function(){
		if( typeof(bodyId) !== 'undefined' ){
			$('body').attr( 'id', bodyId );
		}
	},
	
	checkBrowser: function(){
		Utils.isiPad = navigator.userAgent.match(/iPad/i) != null;
		Utils.isiPhone = navigator.userAgent.match(/iPhone/i) != null;
		if( Utils.isiPad ){	$('body').addClass( 'ipad' );}
			else{$('body').addClass( 'noipad' );}
		if( Utils.isiPhone ){	$('body').addClass( 'iphone' );}
			else{$('body').addClass( 'noiphone' );}
		
		var agt=navigator.userAgent.toLowerCase();
		if (agt.indexOf("opera") != -1) return 'Opera';
		if (agt.indexOf("staroffice") != -1) return 'Star Office';
		if (agt.indexOf("webtv") != -1) return 'WebTV';
		if (agt.indexOf("beonex") != -1) return 'Beonex';
		if (agt.indexOf("chimera") != -1) return 'Chimera';
		if (agt.indexOf("netpositive") != -1) return 'NetPositive';
		if (agt.indexOf("phoenix") != -1) return 'Phoenix';
		if (agt.indexOf("firefox") != -1) return 'Firefox';
		if (agt.indexOf("safari") != -1) return 'Safari';
		if (agt.indexOf("skipstone") != -1) return 'SkipStone';
		if (agt.indexOf("msie") != -1) return 'Internet Explorer';
		if (agt.indexOf("netscape") != -1) return 'Netscape';
		if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
		if (agt.indexOf('\/') != -1) {
			if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
				return navigator.userAgent.substr(0,agt.indexOf('\/'));
			}
			else return 'Netscape';
		} else if (agt.indexOf(' ') != -1) {
			return navigator.userAgent.substr(0,agt.indexOf(' '));
		}
		else 
			return navigator.userAgent;

	},
	
	initSearch: function(){
		$( '#searchbox #submit' ).click(function(){
			Utils.submitSearch( $( '#searchbox textarea' ).val() );	
		});
		
		$(".fancy_search").fancybox({
			'autoDimensions'	: true,
			'autoScale'			: true,
			'overlayOpacity'	: '.89',
			'overlayColor'		: '#fff',
			'showCloseButton'	: false,
			'width'				: 700,
			'height'			: 530,
			'scrolling'			: 'no',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'inline',
			'onClosed'			: HashController.clearHash,
			'onComplete'		: function(){
				Utils.centerSearch(true);
				$( '#searchbox textarea' ).val( '' );
			}
		});
		
		$( '#ie_click' ).fancybox({
			'autoDimensions'	: true,
			'autoScale'			: true,
			'overlayOpacity'	: '.89',
			'overlayColor'		: '#fff',
			'showCloseButton'	: false,
			'width'				: 1024,
			'height'			: 530,
			'scrolling'			: 'no',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'inline',
			'onClosed'			: HashController.clearHash,
			'onComplete'		: function(){
				Utils.centerSearch(true);
				$( '#searchbox textarea' ).val( '' );
			}
		});
	},
	
	centerSearch: function(fancy){
		var position = $( '#content' ).position();
		$( '#searchbox' ).css({
			'position' : 'fixed',
			'left' : (position.left + 150) + 'px',
			'top' : '100px'
		});
		
		if(fancy){
			$( '#searchbox textarea' ).focus();
		}
	},
	
	enterkeysearch: function(evt,thisObj) {
        evt = (evt) ? evt : ((window.event) ? window.event : "")
        if (evt) {
            // process event here
            if ( evt.keyCode==13 || evt.which==13 ) {
                Utils.submitSearch( $( '#searchbox textarea' ).val() );
            }
        }
	},
	
	submitSearch: function( value ){
		$.fancybox.close();
		location.href = '/list/?search=' + value;
		
	},
	
	
	initGrid: function(){
		var h = 0;
		$( '#grid_content li' ).each(function(){
			h = $( 'h3' , $(this) ).parent().height(); 
			$( 'h3' , $(this) ).css( 'padding-top', ((340 / 2)-(h / 2)) + 'px' );
		});
		$( '#grid_content li a' ).removeClass( 'loading' );
		$( '#grid_content li' ).hover(function(){
			$( '.hover', $(this) ).fadeIn();
		}, function() {
			$( '.hover', $(this) ).fadeOut();
		});
		
		
	},
	
	controlMenu: function(){

		$( '#nav > div' ).width( $( '#nav.the_nav > div > ul' ).outerWidth() + 1 ).removeClass( 'loading' );
		
		var navFixed = $( '#nav' ).clone();
		navFixed.attr( 'id', 'nav_fixed' ).appendTo( $('body') );
	
		var temp = $('body').attr('id').split('_');
		switch( temp[ 1 ] ){
			case 'info':
			case 'search':
			case 'support':
				$( '.nav_' + temp[ 1 ] ).addClass( 'selected' );
			break; 
			case 'home':
			case 'monthly':
				$( '.nav_' + temp[ 1 ] ).addClass( 'selected' );
				Utils.showArrows = true;
			break; 
			
		}
		
		
		
	},
	
	initEventListeners: function(){
		$( '#bottombar .arrow_icon' ).click(function(){
			$('html,body').animate({
				scrollTop: 0
			}, 1500, 'easeOutQuint' );
		});
	},		
	
	checkScroll: function( isFirstTime ){
		Utils.documentScrollTop = $(document).scrollTop();
	    if( Utils.documentScrollTop >= Utils.menuYloc + 50 ){
	    	Nav.fixed( isFirstTime );
	    }
	    else{
	    	Nav.unfixed( isFirstTime );
	    }
	    
	    if( Utils.showArrows ){
	    	if( Utils.documentScrollTop > (Math.max( $('body').height() - $( '#footer' ).height() - 700, 100 )) ){
		    	Arrows.unfixed( isFirstTime );
		    }
		    else{
		    	Arrows.fixed( isFirstTime );
		    }
	    }
	    
	    
	    
	    
		
	},
	
	placeholder: function(){
		$('.placeholder-compatibility').focus(function() {
		  var input = $(this);
		  if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		  }
		}).blur(function() {
		  var input = $(this);
		  if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		  }
		}).blur();
		
		$('.placeholder-compatibility').parents('form').submit(function() {
		  $(this).find('.placeholder-compatibility').each(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
			  input.val('');
			}
		  })
		});
	},
		
	log : function(message){
		if( Utils.DEBUG ){
			console.info(message);
		}
	}
};

var Nav = {
	obj: null,
	isFixed: false,
	
	fixed: function( isFirstTime ){
		if( Nav.obj == null){
			Nav.obj = $( '#nav_fixed' );
		}
		if( !Nav.isFixed ){
			Nav.isFixed = true;
			Nav.obj.fadeIn( 1000 );
		}
		
	},
	
	unfixed: function( isFirstTime ){
		if(Nav.isFixed){
			Nav.isFixed = false;
			Nav.obj.fadeOut( 200 );
		}
		
	}
};

var Arrows = {
	obj: null,
	isFixed: false,
	
	fixed: function( isFirstTime ){
		if( Arrows.obj == null){
			Arrows.obj = $( '.arrows' );
		}
		if( isFirstTime || !Arrows.isFixed ){
			Arrows.isFixed = true;
			Arrows.obj.css( {
	    		'position' : 'fixed',
	    		//'top' : Utils.arrowOriginalY + 'px'
	    		'top' : '50%'
	    	} ).fadeIn( 300 );
		}
		
	},
	
	unfixed: function( isFirstTime ){
		//console.log( 'unfixed' );
		if( Arrows.obj == null){
			Arrows.obj = $( '.arrows' );
		}
		if( isFirstTime || Arrows.isFixed ){
			Arrows.isFixed = false;
			/*
$( '.arrows' ).css( {
	    		'position' : 'absolute',
	    		'top' : Utils.arrowYloc + Utils.arrowOriginalY + 'px'
	    	} );
*/	
			Arrows.obj.fadeOut( 300 );
		}
		
	}
};

/*=================================================*/
/*=================MANAGE HASHES==================*/
var HashController = {
	hash_reload : false,
	anchor: '!',
	init: function(){
		$(window).hashchange( function(){
			HashController.controlHash();
		});
		hash_reload = true;
	    $(window).hashchange();
	},
	
	controlHash: function(){
		var hash = HashController.getHashName();
		switch(hash){
			case '#_=_':
				HashController.clearHash();
			break;
			case '#live':
				if( isLive ){
					Live.init( 'event' );
				}
				else{
					HashController.clearHash();	
				}
				
			break;
			case '#liveradio':
				if( typeof( isRadioLive ) != 'undefined' && isRadioLive == true ){
					Live.init( 'radio' );
				}
				else{
					HashController.clearHash();
				}
			break;
		}
		
		
	},
	
	
	getHashName: function(){
		return location.hash.split("/")[0];
	},
	
	getHashParam: function(index){
		temp = location.hash.split("/");
		if(temp.length > 1){
			return temp[index];
		}
		else{
			return 0;
		}
		
	},
	clearHash: function(){
		location.hash=HashController.anchor;
		$('a[name=anchor]').attr('name','');
	}
		
}
/*=================END MANAGE HASHES==================*/
/*====================================================*/
