jQuery(document).ready(function(){
	contain_pos = jQuery('.menu_contain ul li:first').position();
	item_height = jQuery('.fmenu ul li').height();
	medium_line = (jQuery('.menu_contain').height() / 2) + contain_pos.top;
	medium_top = medium_line - (item_height);
	medium_bot = medium_line + (item_height);
	
	amenu_down		= jQuery('a#menu_down').position();
	
	if( jQuery('.menu_contain').height() <  jQuery('.menu_contain ul').height() ){
	jQuery('a#menu_down').removeClass('nolink');
	} else {
		jQuery('a.nolink').attr('title','');
	}
	
//	SCROLL DESACTIVADO BLOQUEADO
//	jQuery('.fmenu').children('a').click(function(){
//		var direction = 'up';
//		if( jQuery(this).attr('id') == 'menu_down' ) {
//			direction = 'down';
//		}
//		rotateFeatueMenu(direction)
//		return false;	
//	})
	
	var display_position 	= jQuery('#feature .fmain').position();
	jQuery('#feature .fmenu li h2 a').click(function(){
		do_rotate_feature(this);
		return false;
	});

	jQuery('#feature').bind("mouseenter",setMenuOver);
	jQuery('#feature').bind("mouseleave",setMenuOut);
	
	jQuery('.gphome').bind("mouseenter",setGpOver);
	jQuery('.gphome').bind("mouseleave",setGpOut);	
	
	rotate_feature();
	rotate_gplayer();
		
	//g-player
	jQuery(".gphome .contain ul:first").addClass('current');
	jQuery(".gphome .contain").css('overflow','hidden');
	
	jQuery(".gphome .contentheading a").click(function(){
		do_rotate_gplayer(this);
		return false;
	});
	
	jQuery('.homeBottomLeft .item_list.canexpand').click(function() {
		if( jQuery(this).hasClass('expanded') ) return true;
		jQuery(this).addClass('expanded');
		jQuery(this).animate({
		    height: '270px'
		  }, {
		    duration: 1000
		  });
		return false;
	});
	
//	jQuery('.homeBottomLeft .item_list.expanded.canexpand').click(function(){
//	return true;
//	});
	
	
	jQuery('.homeBottomLeft .item_list.canexpand').mouseleave(function() {
		if( jQuery(this).hasClass('expanded') ) {
		} else {
			return;
		}
		jQuery(this).removeClass('expanded');
		jQuery(this).animate({
		    height: '61px'
		  }, {
		    duration: 1000
		  });					
	});
	
});

function setMenuOver(){
	if( typeof(tm_feature) != 'undefined' ) {
		clearTimeout(tm_feature);
	} 
}
// CONTROL DE VELOCIDAD DE SLIDE  ::  1000 = 1seg //
function setMenuOut(){
	setMenuOver();
	tm_feature = setTimeout(rotate_feature, 60000);
}
function setGpOver(){
	if( typeof(tm_gplayer) != 'undefined' ) {
		clearTimeout(tm_gplayer);
	} 
}
function setGpOut(){
	setGpOver();
	tm_gplayer = setTimeout(rotate_gplayer, 6000);
}
function animmateFeatureMenu(offset){	
	jQuery('.fmenu ul').animate({
	    marginTop: offset+'px'
	}, 
	  300
	, function(){
		
		var first_pos = jQuery( '.menu_contain li:first').position();
		var last_pos = jQuery( '.menu_contain li:last').position();
		
		if( first_pos.top < contain_pos.top ) {
			jQuery('a#menu_up').removeClass('nolink');
		} else {
			jQuery('a#menu_up').addClass('nolink');
		}
		
		if ( last_pos.top < amenu_down.top) {
			jQuery('a#menu_down').addClass('nolink');
		} else {
			jQuery('a#menu_down').removeClass('nolink');
		}

	});	
}

function rotateFeatueMenu(direction){
	
	current_m 	= parseInt(jQuery('.fmenu ul').css('marginTop'));
	margin		= parseInt(jQuery('.fmenu ul li').css('marginBottom'));
	
	
	menu_position = jQuery('.fmenu ul').position();
	
	if( direction == 'down' ) {
		offset	 	=  current_m - (item_height + margin);	
		if( ((menu_position.top - offset) + item_height) > jQuery('.fmenu ul').height() ){
			return false;
		}			
	} else {
		offset 		= current_m + (item_height +  margin);
		if( (menu_position.top + offset) >  menu_position.top ){
			return false;
		}
	}
  var numero2 = 79;
	var resto = parseInt(offset % numero2);
	if (resto==0)
		animmateFeatureMenu(offset);
	return false;
}

function rotate_feature(){
	if( typeof(tm_feature) == 'undefined' ) {
		setMenuOut();
		return;
	} 
	var totalChildren = jQuery('.fmenu li').size();
	jQuery('.fmenu li.current').each(function(){
		var currentItem = jQuery('.fmenu li').index(this) + 1;
		if( currentItem == totalChildren ){
			currentItem = 1;
			//also reset the meny to the top:
			animmateFeatureMenu('0');
		} else {
			var position_now = jQuery(this).position();
			if(  position_now.top > medium_top && position_now.top < medium_bot ) {
				rotateFeatueMenu('down');
			}
			currentItem++;
		}
		do_rotate_feature(jQuery('.fmenu li:nth-child('+currentItem+')').children('h2').children('a'));
		setMenuOut();
		return false;			
	});
}

function rotate_gplayer(){
	if( typeof(tm_gplayer) == 'undefined' ) {
		setGpOut();
		return;
	} 
	var totalChildren = jQuery('.gphome .contentheading ul li').size();
	jQuery('.gphome .contentheading ul li.current').each(function(){
		var currentItem = jQuery('.gphome .contentheading ul li').index(this) + 1;
		if( currentItem == totalChildren ){
			currentItem = 1;
		} else {
			currentItem++;
		}
		do_rotate_gplayer(jQuery('.gphome .contentheading ul li:nth-child('+currentItem+')').children().children('a'));
		setGpOut();
		return false;	
	});
}

function do_rotate_gplayer(obj){
	jQuery('.gphome .contentheading li, .gphome .contentheading li a').removeClass('current');
	jQuery(obj).parent().parent().addClass('current');
	newItem_id = String(jQuery(obj).parent().parent().attr('id'));
	jQuery(".gphome .contain ul.current").fadeOut('fast',function(){
		jQuery(".gphome .contain ul").removeClass('current');
		jQuery('ul#i'+newItem_id).fadeIn('fast',function(){
			jQuery('ul#i'+newItem_id+', li#'+newItem_id+' a').addClass('current');
			color 	= jQuery(obj).css('color');
			jQuery(".gphome .contentheading").css('cssText','border-bottom-color:'+color+'!important'); 
		});
	});
	return false;
}

function do_rotate_feature(obj){
	
		if( jQuery(obj).parent().parent().hasClass('current') ){
			return false;
		}
		offset = '';
		menu_position 		= jQuery('#feature .fmain ul.contain').position();
		this_id 			= jQuery(obj).parent().parent().attr('id');
		

		feature_id 			= this_id.replace('fml_','');
		feature_pos 		= jQuery('#feature .fmain #'+feature_id).position();
		
		
		
		jQuery('#feature .fmenu li').removeClass('current');
		jQuery(obj).parent('h2').parent('li').addClass('current');
		
		index = jQuery(obj).parent().parent().parent().children().children().children().index(obj);
		height = parseInt(jQuery('#feature .fmain #'+feature_id).css('height'));
	
		if( feature_pos.top  > menu_position.top ){
			offset = (height * index)*-1;
		} else if( feature_pos.top  < menu_position.top ) {
			offset = (height * index)*-1;	
		}
		
		jQuery('#feature .fmain ul.contain').animate({
		    marginTop: offset+'px'
		  }, {
		    duration: 1000
		  });	
}
