/**
 * Deteccion de Internet Explorer
 * Se guarda en una variable para consultar una sola vez
 */
var msie = ( jQuery.browser.msie ) ? true : false;
var msie = {
	msie6 : ( jQuery.browser.version == '6.0' ) ? true: false,
	msie7 : ( jQuery.browser.version == '7.0' ) ? true: false
}
function cleanArray(actual){
  var newArray = new Array();
  for(var i = 0; i<actual.length; i++){
      if (actual[i]){
        newArray.push(actual[i]);
    }
  }
  return newArray;
}
var js = {
	value_desc_remove : function(){
		$('#values-descriptions').hide(0, function(){
			$(target).hide();
		}).removeAttr('class');
	},
	value_desc_show : function(e){
		target = $(e).attr('href');
		index = $('#values-names li').index( $(e).parent() );
		$values_descriptions = $('#values-descriptions');
		bg_pos = ( parseInt( index ) * 115 ) + 8;
		$values_descriptions.css('background-position', '-'+bg_pos+'px 0');
		$(e).addClass('hover');
		if ( $values_descriptions.hasClass('active') ) {
			if ( typeof(target) != 'undefined' && typeof(index) != 'undefined' ) {
				$('#values-descriptions div.value-description').hide();
				$(target).show();
				$values_descriptions.
					removeAttr('class').
					addClass('active').
					hover( function(){ $(this).addClass('hover').removeClass('active') }, function(){ $(this).removeClass('hover'); js.value_state_check() } );
			}
		} else {
			$(target).show();
			$values_descriptions.
				show().
				addClass('active').
				hover( function(){ $(this).addClass('hover').removeClass('active') }, function(){ $(this).removeClass('hover'); js.value_state_check() } );
		}
	},
	value_state_check : function(){
		if ( !$('#values-names a').hasClass('hover') && $('#values-descriptions').hasClass('active') ) {
			$('#values-descriptions').removeClass('active');
		} else if ( $('#values-names a').hasClass('hover') || $('#values-descriptions').hasClass('active') || $('#values-descriptions').hasClass('hover') ) {
			if ( typeof(values_check) == 'undefined' ) {
				values_check = window.setInterval( js.value_state_check, 500);
			}
		} else {
			if ( typeof(values_check) != 'undefined' ) {
				window.clearInterval( values_check );
				delete values_check;
			}
			js.value_desc_remove();
		}
	},
	adjust_bg : function(){
		var bg_pos = 1100 - parseInt( jQuery(document).height() ) + parseInt( $('#footer').outerHeight() );
		$('#global').css('background-position', 'center -'+bg_pos+'px');
	},
	ajax_get_post : function(e){
		if ( $(e).parents('li').hasClass('active') ) {
			return false;
		} else {
			e_parent_id = $(e).attr('rel');
			$e_parent = $(e).parents('li');
			get_post = e_parent_id.split('post-')[1];
			$content_wrap = $('#content-wrap');
			$('#ajax-posts .active').removeClass('active');
			$e_parent.addClass('active');
			content_height = $content_wrap.height();
			$('.post', $content_wrap).fadeTo('fast', 0.35, function(){
				$content_wrap.height(content_height).addClass('loading');
			});
			var old_title = document.title.split(' | ');
			var new_title = $(e).text();
			if ( old_title.length > 1 ) {
				document.title = new_title +' | '+ old_title[1];
			} else {
				document.title = new_title +' | '+ old_title[0].replace('| ', '');
			}
			js.ajax_get_post_content(get_post);
		}
	},
	ajax_get_post_content : function(get_post, post_id){
		post_id = ( post_id ) ? post_id : get_post;
		$.post('index.php', { action : 'asset_ajax_get_post', post_id : post_id, referer: location.pathname.toString() }, function(data){
			if ( data ) {
				$content_wrap = $('#content-wrap');
				$('.post', $content_wrap).remove();
				$content_wrap.removeClass('loading').append(data);
				new_content_height = $('.post', $content_wrap).height();
				$content_wrap.animate({ height: new_content_height }, 'normal', 'linear');
				$('a[href$=.jpg], a[href$=.jpg], a[href$=.jpeg], a[href$=.JPEG], a[href$=.png], a[href$=.PNG], a[href$=.gif], a[href$=.GIF]', '#content').colorbox({
					maxWidth: '90%',
					maxHeight : '90%',
					scalePhotos : true
				});
			}
		});
	},
	ajax_get_posts : function(e){
		offset = $(e).attr('rel').split('-')[1];
		$ajax_posts = $('#ajax-posts');
		$('#ajax-posts-pagination').fadeOut('fast', function(){ $(this).remove(); });
		$ajax_posts.fadeTo('fast', 0.35, function(){
			$('#sidebar').addClass('loading');
		});
		$.post('index.php', { action: 'asset_ajax_get_posts', offset: offset, referer: location.pathname.toString() }, function(data){
			if ( data ) {
				$ajax_posts.animate( { height: 0 }, 'normal', function(){
					$(this).remove();
				}).parent().removeClass('loading');
				$('#sidebar').append(data);
				first_post_id = $('li:first-child', data).attr('id');
				first_post_id_number = first_post_id.split('-')[1];
				displayed_post_id = $('#content-wrap .post').attr('id').split('-')[1];
				if ( first_post_id_number == displayed_post_id ) {
					$('#'+first_post_id).addClass('active');
				}
				js.replace_url( $('#ajax-posts a') );
				$('a[href='+location.href+']').parents('li').addClass('active');
			}
		});
	},
	home_img_load : function(e){
		$global = $('#global');
		rand_img = $('meta[name=background-image-url]').attr('content');
		$('<div />', {
			id: 'loading'
		}).prependTo($global);
		home_bg_img = new Image();
		home_bg_img.id = 'home-bg-img';
		home_bg_img.onload = function(){
			$('#loading').fadeOut(600, function(){ $(this).remove() });
		}
		home_bg_img.src = rand_img;
	},
	about_asset_menu : function(e) {
		if ( $(e).parent().attr('id') === 'carrera-en-asset-menu' ) {
			return true;
		} else if ( $(e).parent().hasClass('active') ) {
			return false;
		} else {
			$about_menu = $('#about-asset-menu');
			$('dd.active.has-children .page-children, dd.active.has-children div.page-summary', $about_menu).slideToggle('normal');
			if ( $('dt.active').next().attr('id') === 'equipo' ) {
				$('#equipo .page-children, #equipo .page-summary').slideToggle('normal');
			}
			$this_subinfo = $(e).parent().next();
			if ( $(e).parent().hasClass('has-children') || $(e).parent().next().attr('id') == 'equipo' ) {
				$('.page-children, .page-summary', $this_subinfo).slideToggle('normal');
			}
			$('.active', $about_menu).removeClass('active');
			$('.current_page_item', $about_menu).removeClass('current_page_item');
			$(e).parent().addClass('active').next().addClass('active');
			if ( $('#equipo').hasClass('active') ) {
				$first_element = $('#equipo a:first');
				var page_id = $first_element.attr('rel').split('-')[2];
				var referer = $first_element.attr('href');
				var new_title = $first_element.attr('title');
				var original_url = $first_element.data('original_url');
				$first_element.addClass('active');
			} else if ( !$(e).parent().hasClass('has-children') ) {
				var page_id = $(e).attr('rel').split('-')[2];
				var referer = $(e).attr('href');
				var new_title = $(e).text();
				var original_url = $(e).data('original_url');
			} else {
				$first_element = $('dd.active ul li:first-child', $about_menu);
				var page_id = $first_element.attr('class').split(' ')[1].split('-')[2];
				var referer = $('a', $first_element).attr('href');
				var new_title = $('a', $first_element).attr('title');
				var original_url = $('a', $first_element).data('original_url');
				$first_element.addClass('current_page_item');
			}
			js.about_asset_get_page(page_id, referer, new_title);
			return false;
		}
	},
	about_asset_page : function(e) {
		if ( $(e).parent().hasClass('current_page_item') ) {
			return false;
		} else if ( $(e).parents('div').hasClass('asset-people') ) {
			$e = ( $(e)[0].tagName == 'IMG' ) ? $(e).parent() : $(e);
			$('#about-asset-menu .current_page_item').removeClass('current_page_item');
			if ( !$('#equipo').hasClass('active') ) {
				$('#about-asset-menu .active').removeClass('active');
				$('#equipo').addClass('active');
			} else {
				$('#equipo a.active').removeClass('active');
			}
			var page_id = $e.attr('rel').split('-')[2];
			var referer = $e.attr('href');
			var new_title = $e.attr('title');
			$e.addClass('active');
			js.about_asset_get_page(page_id, referer, new_title);
		} else {
			$('#about-asset-menu .current_page_item').removeClass('current_page_item');
			var page_id = $(e).parent().attr('class').split(' ')[1].split('-')[2];
			var referer = $(e).attr('href');
			var new_title = $(e).attr('title');
			$(e).parent().addClass('current_page_item');
			js.about_asset_get_page(page_id, referer, new_title);
		}
	},
	about_asset_get_page : function(page_id, referer, new_title, original_url){
		$content_wrap = $('#content-wrap');
		content_height = $content_wrap.height();
		$('.hentry', $content_wrap).fadeTo('fast', 0.35, function(){
			$content_wrap.height(content_height).addClass('loading');
		}).css('background', 'none');
		if ( new_title ) {
			var old_title = document.title.split(' | ');
			if ( old_title.length > 1 ) {
				document.title = new_title +' | '+ old_title[1];
			} else {
				document.title = new_title +' | '+ old_title[0].replace('| ', '');
			}
		}
		if ( !referer ) referer = location.pathname.toString();
		$.post('index.php', {action: 'asset_ajax_get_content', post_id : page_id, referer: referer, original_url: original_url }, function(data){
			if (data) {
				$('.hentry', $content_wrap).remove();
				$content_wrap.append(data).removeClass('loading');
				if ( $(data).hasClass('ejecutivos') ) {
					bg_pos = $('.exec-indicator').attr('id').split('exec-')[1];
					$('.exec-indicator')
						.css('background-position', '5% 0')
						.animate({
							'background-position': '-'+bg_pos+'% 0'
						}, 'slow');
				}
				new_content_height = $('.hentry', $content_wrap).height();
				$content_wrap.animate({ height: new_content_height }, 'normal', 'linear');
				$('a[href$=.jpg], a[href$=.jpg], a[href$=.jpeg], a[href$=.JPEG], a[href$=.png], a[href$=.PNG], a[href$=.gif], a[href$=.GIF]', '#content').colorbox({
					maxWidth: '90%',
					maxHeight : '90%',
					scalePhotos : true
				});
			}
		});
	},
	replace_url : function(selector){
		selector_length = $(selector).length;
		for ( i = 0; i < selector_length; i++ ){
			url = $(selector[i]).attr('href');
			if ( location.href.match('/novedades/') ) {
				url_path = url.replace( '/novedades/', '/novedades/#/' )
			} else if ( location.href.match('/#/') ) {
				url_path = url.replace( location.hostname, location.hostname+location.pathname+'#' );
			} else if ( url.match('/carrera-en-asset/') && !location.href.match('/carrera-en-asset/') ) {
				continue;
			} else {
				url_path = url.replace( 'http://'+location.hostname, location.href+'#' );
			}
			$(selector[i]).attr('href', url_path );
		};
	},
	about_menu_detect_children : function(){
		$('#about-asset-menu dd').each(function(){
			if ( $('ul', $(this) ).length > 0 && $('li', $(this)).length > 0 )
				$(this).addClass('has-children').prev().addClass('has-children');
			else
				$(this).addClass('dont-have-children').prev().addClass('dont-have-children');
		});
	},
	ajax_bookmarks : function(){
		if ( location.href.match('/#/') ) {
			ajax_path = location.href.split('#');
			if ( ajax_path[0].match('/es/') || ajax_path[0].match('/en/') ) {
				lang_url = ( location.href.match('/es/') ) ? '/es' : '/en';
				original_url = location.href.replace(location.pathname, lang_url + ajax_path[1]).split('#')[0];
			} else if ( ajax_path[1].match('/es/') || ajax_path[1].match('/en/') ) {
				lang_url = ( location.href.match('/es/') ) ? '/es' : '/en';
				ajax_path[1] = ajax_path[1].replace(lang_url, '');
				original_url = location.href.replace(location.pathname, ajax_path[1]).split('#')[0];
			} else {
				original_url = location.href.replace(location.pathname, ajax_path[1]).split('#')[0];
			}
			$original_link = $('a[href='+original_url+']');
			$original_link_parent = $original_link.parent();
			if ( $original_link_parent[0].tagName == 'DT' && $original_link_parent.hasClass('has-children') ) {
				$children_container = $original_link_parent.next();
				$first_children = $('a:first', $children_container);
				page_id_get = $first_children.parent().attr('class').split(' ')[1].split('-')[2];
				js.about_asset_get_page(page_id_get, $first_children.attr('href'), '');
				$('#about-asset-menu .active').removeClass('active');
				$('#about-asset-menu .current_page_item').removeClass('current_page_item');
				$first_children.parent().addClass('current_page_item').parents('dd').addClass('active').prev().addClass('active');
			} else if ( $original_link_parent.next().attr('id') == 'equipo' ) {
				$first_children = $('#equipo a:first');
				page_id_get = $first_children.attr('rel').split('-')[2];
				if ( $first_children.attr('href').match('/#/') ) {
					js.about_asset_get_page(page_id_get, $first_children.attr('href'), $first_children.attr('title') );
				} else {
					js.about_asset_get_page(page_id_get, $first_children.attr('href').replace(location.pathname, location.pathname+'/#/'), $first_children.attr('title') );
				}
				$('#about-asset-menu .active').removeClass('active');
				$('#about-asset-menu .current_page_item').removeClass('current_page_item');
				$first_children.addClass('active').parents('dd').addClass('active').prev().addClass('active');
			} else if ( $.id('#about-asset-menu') ) {
				js.about_asset_get_page('', location.href, '');
				$('#about-asset-menu .active').removeClass('active');
				$('#about-asset-menu .current_page_item').removeClass('current_page_item');
				$original_link.parent().addClass('current_page_item').parents('dd').addClass('active').prev().addClass('active');
			}
			var old_title = document.title.split(' | ');
			var new_title = $original_link.text();
			if ( old_title.length > 1 ) {
				document.title = new_title +' | '+ old_title[1];
			} else {
				document.title = new_title +' | '+ old_title[0].replace('| ', '');
			}
		}
	},
	asset_news_ajax_bookmarks : function(){
		if ( location.href.match('/#/') ) {
			ajax_path = location.href.split('#');
			if ( ajax_path[0].match('/es/') || ajax_path[0].match('/en/') ) {
				lang_url = ( location.href.match('/es/') ) ? '/es' : '/en';
				original_url = location.href.replace(location.pathname, lang_url + ajax_path[1]).split('#')[0];
			} else if ( ajax_path[1].match('/es/') || ajax_path[1].match('/en/') ) {
				lang_url = ( location.href.match('/es/') ) ? '/es' : '/en';
				ajax_path[1] = ajax_path[1].replace(lang_url, '');
				original_url = location.href.replace(location.pathname, ajax_path[1]).split('#')[0];
			} else {
				original_url = location.href.replace(location.pathname, ajax_path[1]).split('#')[0];
			}
			$original_link = $('a[href='+original_url+']');
			ajax_post_id = $original_link.attr('rel').split('post-')[1];
			$ajax_posts = $('#ajax-posts');
			$('li.active', $ajax_posts).removeClass('active');
			if ( $original_link.length > 0 ) {
				$original_link.parents('li').addClass('active');
			}
			$content_wrap = $('#content-wrap');
			content_height = $content_wrap.height();
			$('.post', $content_wrap).fadeTo('fast', 0.35, function(){
				$content_wrap.height(content_height).addClass('loading');
			});
			var old_title = document.title.split(' | ');
			var new_title = $original_link.text();
			if ( old_title.length > 1 ) {
				document.title = new_title +' | '+ old_title[1];
			} else {
				document.title = new_title +' | '+ old_title[0].replace('| ', '');
			}
			js.ajax_get_post_content( location.href, ajax_post_id  );
		}
	},
	tombstones_refresh : function() {
		$tombstones = $('#sidebar-tombstones');
		$tombstone_links = $('a', $tombstones);
		tombstone_link_count = $tombstone_links.length; present_tombstones = Array();
		for ( i=0; i<tombstone_link_count; i++ ){
			present_tombstones[i] = $($tombstone_links[i]).attr('rel').split('tombstone-')[1];
		}
		$.post('index.php', { action : 'tombstones_refresh', not_these : present_tombstones}, function(data){
			if ( data ) {
				$tombstones.fadeOut('slow', function(){
					$(this).remove();
					$('#sub-sidebar').append(data);
					$('a', '#sub-sidebar').colorbox();
				});
				tombstones_refresh = window.setTimeout('js.tombstones_refresh()', 10000);
			}
		});
	}
}
jQuery(document).ready(function($){
	if ( $('#global').hasClass('bottom') ) {
		js.adjust_bg();
		$(window).bind('resize', function(){ js.adjust_bg(); });
	}
	js.home_img_load();
	if ( $.id('#sidebar-tombstones') ) {
		tombstones_refresh = window.setTimeout('js.tombstones_refresh()', 10000);
	};
	$('#values-names a').mouseover( function(e){ js.value_desc_show(e.target); } );
	$('#values-names a').mouseout( function(){ js.value_state_check(); $('#values-names a').removeAttr('class'); } );
	$('#enviar-contacto').live('click', function(){
		$contactform = $('#contactform');
		$contactform
			.append('<input type="hidden" name="action2" value="contact_ajax-send" />')
			.fadeTo('normal', 0.35)
			.parent()
			.addClass('loading');
		var form_data = $contactform.serialize();
		$.post('index.php', form_data, function(data){
			$('input, textarea', $contactform).attr('disabled', 'disabled');
			if ( data ) {
				$('#contact').replaceWith(data);
				if ( $.id('#content-wrap') ) {
					new_content_height = $('#content-wrap .hentry').height();
					$('#content-wrap').animate({ height: new_content_height }, 'normal', 'linear');
					if ( msie.msie6 || msie.msie7 ) {
						$('#enviar-contacto').attr('value', '');
					}
				}
			}
		});
		return false;
	});
	$('a.colorbox').colorbox({
		opacity: 0.5,
		current: '{current} de {total}',
		previous: 'anterior',
		next: 'siguiente'
	});
	$('#project-link').colorbox({
		opacity: 0.5,
		inline: true,
		href: '#project-desc',
		width: 515,
		title: false
	});
	$('a[href$=.jpg], a[href$=.jpg], a[href$=.jpeg], a[href$=.JPEG], a[href$=.png], a[href$=.PNG], a[href$=.gif], a[href$=.GIF]', '#content').colorbox({
		maxWidth: '90%',
		maxHeight : '90%',
		scalePhotos : true
	});
	$('#ajax-posts h4 a').live('click', function(e){ js.ajax_get_post(e.target); } );
	$('#ajax-posts-pagination a').live('click', function(e){ js.ajax_get_posts(e.target); return false; });
	if ( $.id('#about-asset-menu') ) {
		js.about_menu_detect_children();
		js.ajax_bookmarks();
		js.replace_url( $('#about-asset-menu a') );
		$('#about-asset-menu dt a').click(function(e){ js.about_asset_menu(e.target); });
		$('#about-asset-menu .page-children a').click(function(e){ js.about_asset_page(e.target); });
	}
	if ( $.id('#ajax-posts') ) {
		js.asset_news_ajax_bookmarks();
		js.replace_url( $('#ajax-posts a') );
	}
	if ( $.id('#home-features') ) {
		$home_features = $('#home-features .yui-u');
		home_features_max_height = '0';
		$home_features.each(function(){
			if ( $(this).outerHeight(true) > home_features_max_height ) home_features_max_height = $(this).outerHeight(true);
		}).css({
			'height': home_features_max_height
		}).hover(
			function(){$(this).addClass('hover')},
			function(){$(this).removeClass('hover')}
		);
	}
	if ( $.id('#equipo') ) {
		$('#equipo div.asset-people').each(function(){
			var $people = $(this).find('a'), peopleLength = $people.length;
			for ( i = 0; i < peopleLength; i ++ ) {
				$($people[i]).addClass('indexof-'+i%5);
			}
		});
	}
	if ( msie.msie6 || msie.msie7 ) {
		$('#enviar-contacto').attr('value', '');
	}
	$('#spanish-version-link a, #english-version-link a').click(function(){
		if ( location.pathname.length > 4 ) { // we're not seeing the home
			alt_lang_url = ( $(this).parent().attr('id') === 'spanish-version-link' ) ? location.href.replace(location.pathname, '/es'+location.pathname) : location.href.replace(location.pathname, '/en'+location.pathname);
			$(this).attr('href', alt_lang_url);
		}
	});
	$('#photo-caption a').attr('target', '_blank');
});
jQuery.extend({
	id : function(id) {
		var id = id.split("#");
		return document.getElementById(id[1])?true:false;
	}
});
