var categoriShow 					= 0 ; 
var TEXT_SMOOTH_ROLLOVER_TIME       = 250 ;            
var TEXT_SMOOTH_ROLLOVER_COLOR      = '#F26938' ;      
var IMAGE_SMOOTH_ROLLOVER_TIME      = 400 ;         
var SIMPLE_TIP_SHOWTIME             = 300 ;          
var IMAGE_SMOOTH_ROLLOVER           = IMAGE_SMOOTH_ROLLOVER_TIME > 0 ? true : false ;
var TEXT_SMOOTH_ROLLOVER            = TEXT_SMOOTH_ROLLOVER_TIME  > 0 ? true : false ;
var OVER_KEYWORD                    = '_over';
var OVER_LINK_ACTION                = true ;
var OVER_LINK_ROLLOVER              = true ;
    OVER_LINK_ROLLOVER              = OVER_LINK_ROLLOVER &&  OVER_LINK_ROLLOVER ? true : false ;
var SITE_LANG = SITE_LANG ? SITE_LANG : 'es' ; 
var SITE_ROOT = '/' ; 	
var SITE_VIDEO_POPUP_URL = '' ; 
var RESOURCES_FOLDER =  'resources/'+SITE_LANG + '/' ;
var XML_FOLDER = 'resources/'+SITE_LANG+'/xml/' ;
var RESOURCES_IMAGES_FOLDER = 'resourse/' +  SITE_LANG; 


var BOOM_CDN_URL = 'http://i.cdn.turner.com/toonla/images/boomix/assets/site' ;

window.alert = function(text, title) {
    try {
        StickyWin.alert((title || 'Boomerang'), text, BOOM_CDN_URL + '/design/assets/img/common');
    } catch (e) {
        window.parent.StickyWin.alert((title || 'Boomerang'), text, BOOM_CDN_URL + '/design/assets/img/common');
    }
}

function updateNews( data ) {
	if (data) {
		var DURATION = 350 ;
		$('news_cntr').set( 'tween' , { link : 'chain' , duration : DURATION});
		$('news_cntr').fade('out').fade('in');
		var myChain = new Chain();
		var updateData = function(){	
			$('news_avatar').setStyle( 'background-image' , 'url(' + data.avatar + ')'  ) ; 

			$('news_title').innerHTML = data.title ;
			$('news_title').setStyle('cursor' , 'pointer' ) ;
			if ( data.profileLink  && data.profileLink != '' ) {
				$('news_title').onclick = function () {
					document.location = data.profileLink ; 
					return false;
				}
			}

			$('news_body').innerHTML = data.body ;

			$('news_body').setStyle('cursor' , 'pointer' ) ;
			if ( data.link  && data.link != '' ) {
				$('news_body').onclick = function () {
					if ( data.type == 1 ) {
						document.location = data.link ; 
					} else if ( data.type == 2 ) {
						lightBoxAjax ( data.link )
					} else {
						document.location = data.link ; 
					}
					return false;
				}
			}

		};
		myChain.wait(DURATION).chain(updateData);
		myChain.callChain();
	} else {
		$('news_prev').fireEvent('click');
	}
}

function cropText ( s , l  , r ) {
	var r = r ? r : '...' ; 
	var l = l ? l : 25 ; 
	if ( s ) {
		s = String(s);
		if ( s.length > l ) {
			s = s.substr( 0 , l - String(r).length ) ;
			s += r ; 
			return s ; 
		} else {
			return s ;
		}
	} else {
		return false ;
	}

}






function showElement(elementId) {
	var e= $(elementId);
	e.set('tween',{
			onComplete: function(){
				display = this.element.getStyle('opacity') > 0 ? 'block' : 'none';
				this.element.setStyle('display' , display);
			}
			,duration : 750
			,link : 'chain'
	});

	if ( e.getStyle('display') != 'block' )
		e.setStyle('opacity' , 0 ).setStyle('display','block').fade('in');
}

function hideElement(elementId) {
	var e= $(elementId);
	if ( e.getStyle('display') != 'none' )
		e.tween('opacity', 0);
}

function getRandomNews ( data ) {
	news_current = Math.round(Math.random() * data.length) 
	return news_current;
}

// ---------------------------------------------------------------------------------------------------------- >

function toggleImageActive (e) {
    e.toggleClass('active');
    imageDoRollOver(e  , e.hasClass('active') ) ;
}
// SCROLL //-------------------------------------------------------------------------------------------------------------------------------
function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
    var axis = horizontal ? 'x' : 'y'  ;
	
	oldC = content.getStyle('display');
   	oldS = scrollbar.getStyle('display');
	oldK = handle.getStyle('display');
	
	content.setStyle('display','block');
	scrollbar.setStyle('display','block');
	handle.setStyle('display','block');
	
	a1 = content.getSize() ;
    a2 = content.getScrollSize() ;
    
	if ( a1[axis] >= a2[axis] ) {
        scrollbar.setStyle('display' , 'none' ) ;
		
	} else {
        var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):( content.getScrollSize().y - content.getSize().y));
        var steps = steps == 0 ? 100 : steps;
        var slider = new Slider( scrollbar , handle , {   
            steps: steps,
            mode: (horizontal?'horizontal':'vertical'),
            onChange: function(step){
                var x = (horizontal?step:0);
                var y = (horizontal?0:step);
                content.scrollTo ( x , y );
            }
        }).set(0);

        if( !(ignoreMouse) ){
            $$(content, scrollbar).addEvent('mousewheel', function(e){ 
                e = new Event(e).stop();
                var step = slider.step - e.wheel * 30; 
                slider.set(step);               
            });
        }
        $(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
		content.setStyle('overflow' , 'hidden') ;
		content.setStyle('display',oldC);
		scrollbar.setStyle('display',oldS);
		handle.setStyle('display',oldK);
    }

}
// SCROLL //-------------------------------------------------------------------------------------------------------------------------------

function overTween ( e , nColor , oColor ) {
    oColor = oColor ? oColor : e.getStyle('color') ; 
    nColor = nColor ? nColor : TEXT_SMOOTH_ROLLOVER_COLOR ;   
    e.set('tween', {duration: TEXT_SMOOTH_ROLLOVER_TIME });
    e.set('oColor', oColor ) ; 
    e.set('nColor', nColor ) ; 
    
    
    e.addEvent('mouseover', function (){
        if (TEXT_SMOOTH_ROLLOVER) {
            this.tween('color', this.get('nColor') );     
        } else {
            this.set('color', this.get('nColor') );   
        }      
    });
    
    e.addEvent('mouseout', function (){
        if ( !this.hasClass('active')) {
            if (TEXT_SMOOTH_ROLLOVER) {
                this.tween('color', this.get('oColor') );
            } else {
                this.set('color', this.get('oColor') );  
            }
        } else {
            if (TEXT_SMOOTH_ROLLOVER) {
                this.tween('color', this.get('nColor') );
            } else {
                this.set('color', this.get('nColor') );    
            }
        }
          
        
    });   
}


function toolTip() {
    myWin.hide();
}
function imageDoRollOver( e , status ) {
	if ( !(status) ){
		if (IMAGE_SMOOTH_ROLLOVER){
			e.tween('opacity' , 1 );
		} else {
			e.set('src' , e.retrieve('originalSRC') ) ;
		}
	} else {
		if (IMAGE_SMOOTH_ROLLOVER){
			e.tween('opacity' , 0.00001 );
		} else {
			e.set('src' , e.retrieve('overSRC') ) ;
		}
	}
}


function imgRollOver (e) {
 if (String(e.retrieve('originalSRC')) == 'null' ){
	var src = e.src ;

	src = src.split('/');
	image = src[src.length-1] ;
	image = image.split('.');
	src = e.src ;
	//console.log(e);
	//console.log(src);
	//console.log(' ')
	if ( src ) {
	src = src.replace( image[0] , image[0] + OVER_KEYWORD  );
	src = src.replace( '_active' , ''  );
	
	e.setStyle('cursor' , 'pointer') ;    
	e.store('overSRC', src );  
	e.store('originalSRC' , e.src ) ;
	
	if (!IMAGE_SMOOTH_ROLLOVER) { 
		e.setStyle('float', 'left') ; 
		e.setStyle('display', 'block') ; 
		if (window.ie){
			e.setStyle('margin-left', -3) ; 
			e.setStyle('margin-right', -3) ; 
		}
	} else { 
		e.set('tween', {duration: IMAGE_SMOOTH_ROLLOVER_TIME });
		w = e.getStyle('width'); 
		e.setStyle('display', 'none') ; 
		var div = new Element('div') ;
		div.setStyle('display', 'none') ; 
		div.setStyle('background' , 'url('+ src +') top left no-repeat'); 
		div.setStyle('float', 'left') ; 
		div.setStyle('width', w) ; 
		div.inject(e , 'before' ) ; 
		div.grab(e);
		div.setStyle('display', 'block') ; 
		e.setStyle('display', 'block') ; 
	}

	if ( !e.hasClass('votes') ) { imageDoRollOver( e  , e.hasClass('active')  ) } ;
	
	e.addEvent('mouseenter', function (){
	  imageDoRollOver( this  ,true ) ;
	});
	
	e.addEvent('mouseleave', function (){
		imageDoRollOver( this ) ;
	});

	a = e.getParent('a');
	if (a && OVER_LINK_ACTION ){
		if ( a.href ) {
		
			// this add over to link if innerText != '' 
			if ( a.innerText != '' ) {
				a.addEvent('mouseenter', function (){
					el = this.getElement('img.rollOver') ;
					if ( el ){
						imageDoRollOver( el , true ) ;
						if (e.alt) {
							var content = e.get('alt').split('::') ;  
							e.store('tip:title', content[0]);  
							e.store('tip:text', content[1]);  
						}
					}
				});
				
				a.addEvent('mouseleave', function (){
					el = this.getElement('img.rollOver') ;
					if ( el ){
						imageDoRollOver( el ) ;
					}
				});
			}
			
			// toolTips . {esto no tendria que estar aca. pero por cuestiones de tiempo ...}
			if ( String(a.get('title')) != 'null' && OVER_LINK_ROLLOVER ) {
				text = String(a.get('title')).split('::') ;
				$(a).store('tip:title' , text[0] );
				$(a).store('tip:text'  , text[1] );
				var SimpleTip = new Tips( a , { showDelay: SIMPLE_TIP_SHOWTIME , hideDelay: 50, offsets: {x: -95, y: 22}, fixed: false });
			}
		 
			if ( a.href != '' &&  !(String(a.href).match("#") == '#' )) {                    
				if (IMAGE_SMOOTH_ROLLOVER) { 
					div.addEvent('click', function (){
						a = this.getParent('a');
						manageLink (a);
						return false ;
					});
				} else {
					e.addEvent('click', function (){
						a = this.getParent('a');
						manageLink (a)
						return false ;
					});
				}
			}
			
			
		}
	}
}	
}
}
	

function manageLink (a) {
 
	if ( a.target && a.target != '_SELF' ) { 	
		window.open(a.href , a.target)
	} else {
		document.location = a.href;
	}
}	


function lightBoxAjax ( URL ) {
	var bkg_opaque = new Element('div' , { id : 'bkg_opaque' });
	div = new Element('div').setStyles({ 
		'margin-left' : 'auto' ,
		'margin-right' : 'auto' ,
		'width' : 960
	});
	if ( $('bkg_opaque') ) $('bkg_opaque').dispose();
	iframe = new Element('iframe' , { 
		id: 'lightBoxCtnr' ,
		scrolling:'no' ,
		allowtransparency:true ,
		src: URL,
		frameborder:'0' ,
		marginheight:'0' ,
		marginwidth:'0' ,
		width:'100%', 
		style:'background-color:transparent' ,
		height: 400
	}).inject(div);
	div.inject(bkg_opaque);
	bkg_opaque.inject('main' , 'before');
	iframe.allowTransparency  = true ;
	iframe.height= 1 ;
	iframe.setStyle('height' , '1px') ;
	var myFx = new Fx.Scroll( window.parent).toTop();
}



function fixIframeSize() {
	try { bkg_opaque = $('bkg_opaque') || window.parent.$('bkg_opaque') } catch (e) {}
	bkg_opaque.setStyle('display' , 'block');
	var size = window.getSize();
	e = $('lightBoxCtnr') || iframe ;
	

	var oDoc =  e.contentWindow || e.contentDocument;
	if (oDoc.document) {
		oDoc = oDoc.document;
	}			
	
	targetHeight = ( oDoc.getScrollSize() ) ? (size.y <= oDoc.getScrollSize().y ? oDoc.getScrollSize().y :size.y  ) : size.y  ; 
	targetHeight = ( window.getScrollSize().y > targetHeight ) ?   window.getScrollSize().y :  targetHeight; 
	
	
	$('bkg_opaque').setStyles({
		'display' : 'block',
		'height': targetHeight  + 'px'
	});
	
	iframe.height= targetHeight + 'px' ; 
	e.setStyle('height', targetHeight + 'px') ; 
	
	bkg_opaque.fade('hide');
	bkg_opaque.fade('in');	
}


function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

	for(var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}


function closePopUp(){
	if ( parent.$('bkg_opaque') ) {
		parent.$('bkg_opaque').dispose();
	}
}

 
function URLEncode( string ) {
	var SAFECHARS = "0123456789" +  
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  
	"abcdefghijklmnopqrstuvwxyz" +
	"-_.!~*'()"; 
	var HEX = "0123456789ABCDEF";

	var plaintext = string;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
		if (ch == " ") {
			encoded += "+"; // x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				alert( "Unicode Character '"  + ch
				+ "' cannot be encoded using standard URL encoding.\n" +
				"(URL encoding only supports 8-bit characters.)\n" +
				"A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} 
 return encoded;
}

function URLDecode( string ) {

	var HEXCHARS = "0123456789ABCDEFabcdef";
	var encoded = string ;
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		} else if (ch == "%") {
			if (
					   i < (encoded.length-2)
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 
				)
			{
					plaintext += unescape( encoded.substr(i,3) );
					i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
			plaintext += ch;
			i++;
		}
	}  
 
	return plaintext ;
}

function loadFlash ( swf , w , h , cntr ) {
	flashInsert ( swf , false , w , h , cntr );
}

function flashInsert ( swf , id , w , h , container , params , vars , callBacks ) { 	
	if ( $(container) ) {
		var obj = new Swiff( swf , {
			id: (id ? id : 'swf_' + container ) ,
			width: w,
			height: h,
			container : (container ? container : false )  ,
			params: (params ? params : { wmode: 'transparent' } ) ,
			vars: (vars  ? vars  : { 'LANG' : SITE_LANG , 'IDIOMA' : SITE_LANG } ) ,
			callBacks : (callBacks? callBacks : false ) 
		});
	}
}	


window.addEvent('domready', function() {

	if ( $$('.bkg_splashpage').length  >= 1 ) {
		if ( Browser.Engine.trident  ) {
			if ( Browser.Engine.version <= 4 ) {
				alert(
						"Boomerang y Boomix requieren Internet Explorer 7 o superior. Si tu equipo utiliza una versión anterior, por favor actualízalo gratuitamente haciendo <a href='link: http://www.microsoft.com/spain/windows/internet-explorer/download-ie.aspx'>click aquí</a><br /><br />" + 
						"O site do Boomerang e o Boomix requerem Internet Explorer 7 ou superior. Se seu computador utiliza uma versão anterior, atualize-o gratuitamente <a href='link: http://www.microsoft.com/latam/windows/internet-explorer/'>clicando aqui</a><br /><br />" + 
						"Boomerang and Boomix require Internet Explorer 7 or newer. If your computer uses an older version than that please update it by  for your free download. <a href='link: http://www.microsoft.com/windows/internet-explorer/default.aspx'>clicking here</a><br /><br />"
					 );
			}
			
		}
	}

    $$('.rollOver').each (function(e){ imgRollOver (e) });
		$$('.footer_links .popUp').each(function (e){
		e.set('href', String(e.get('href')).replace('##LANG##' , SITE_LANG));
		e.addEvent('click' , function (event){ 
			lightBoxAjax(this.get('href'));
			event.stopPropagation();
			event.stop();
			return false;
		});
	});
	
	if ($('news_next') ){
		$('news_next').addEvent ( 'click' , function () {
			news_current = news_current <= data.length -2 ? news_current + 1 : 0 ;  
			updateNews( data[news_current] ) ;
			return false;
		});
		
		$('news_prev').addEvent ( 'click' , function () {
			news_current = news_current >= 1 ? news_current - 1 : data.length - 1 ; 
			updateNews( data[news_current] ) ;
			return false;
		});
		
		updateNews( data[news_current]);
		var annoy = ( function () { updateNews( data[getRandomNews ( data )] ) ;} ).periodical(10000); 
	}
	
	$$('img.ttips').each( function(e) {
		var eTitle = new Array() ; 						 
		eTitle =String( e.get('alt') ).split('::') ;	
		var title = eTitle[0] ? eTitle[0] : '' ;
		var desc  = eTitle[1] ? eTitle[1] : '' ;
		e.store('tip:title', title );
		e.store('tip:text',  desc  );
		e.set('alt' , '' ) ;
	});
	var _Tips = new Tips( 'img.ttips' , { offsets : { x: -35, y : 10 }});
	$$('embed').each(function(e){ e.set("wmode","transparent"); e.getParent('div').setStyle('z-index' , 5) ; });
});
 


