var domainBase='http://www.actidea.com/';
/*
ACORDEÓN
*/
function initAccordions() {
	$('ul.accordion ul').hide();
	function onMouseEnter (mother){
		opening=true;
		var son=mother.children('ul:not(:visible)');
		son.slideDown('slow', finishedOpening);
		//son.css('display','block');
	}
	function onMouseLeave (mother){
		closing=true;
		var son=mother.children('ul:visible');
		son.slideUp('slow', finishedClosing);
		//son.css('display','none');
	}
	function finishedOpening () {
		opening=false;
	}
	function finishedClosing () {
		closing=false;
	}	
	function enableAccordion(){
		opening=false;
		closing=false;
		$('ul.accordion>li').each(function(){
			var current = $(this);
			if(current.hasClass('accordionOpen')){
				current.children('ul').slideDown('slow');
				//current.children('ul').css('display','block');
			} else {
				current.mouseenter(function(){onMouseEnter($(this));});
				current.mouseleave(function(){onMouseLeave($(this));});
				current.children('ul').css('display','none');
			}	
		});
	}
	function disableAccordion(){
		$('ul.accordion>li').unbind('mouseenter mouseleave');
	}
	enableAccordion();
}
$(document).ready(function() {
      if($.browser.msie && (jQuery.browser.version=="6.0" || jQuery.browser.version=="7.0")){
        
      } else {
        initAccordions();
      }
});
/*
FLEXIBLE
*/
//var styleOptions=new array();
/*function layout(){
 
	wW=$(window).width();
    articleW=$('article').width();
    freeWidth=wW-(articleW+16);
    wantedSideW=wW*0.19;
    neededFreeW=wantedSideW*2;
    availableSideW=freeWidth/2;
    minSideW=127;
    window.status=$(window).width()+'x'+$(window).height()+' '+availableSideW;
}*/
function layout() {
		wW=$(window).width();
		articleW=$('article').width();
		freeWidth=wW-(articleW+16);
		wantedSideW=wW*0.19;
		neededFreeW=wantedSideW*2;
		availableSideW=freeWidth/2;
		minSideW=127;
		window.status=$(window).width()+'x'+$(window).height()+' '+availableSideW;
        //var optSideW= wideSideW>sideW ? wideSideW : sideW ;
        //var minwW=minSideW*2+articleW+16;//1010
        //var homeminwW=894;//894
        if(window.location.pathname=="/"){
			if($('nav').width()<$('nav>h1').width()){
				var oFS=$('nav>h1').css('font-size');
				$('nav>h1').css('font-size',oFS-4)
			}       
        
        }

        
        if(neededFreeW>freeWidth){
			availableSideW=freeWidth/2;
			//$('header').css('width',sideW+'px');
			//$('nav').css('display','none');
			if(availableSideW>minSideW){
				$('header').css('width',availableSideW+'px');
				$('nav').css('width',availableSideW+'px');
				$('footer').css('margin-left','auto').css('right','0px').css('left','0px');
				$('article').css('margin-left','auto');
				$('header').css('background','#1B1B1B');
				$('nav').css('display','block');
				$('.mainMenu').show();
				$('header').unbind();				
			} else {
				if(freeWidth>minSideW){
					$('footer').css('left','8px').css('right','auto').css('margin-left','0px');
					$('article').css('left','0px').css('margin-left','0px');
					$('nav').css('display','none');
					$('header').css('width',freeWidth+'px');
					$('header').css('background','#1B1B1B');
					$('header').unbind();
					$('header').css('display','block');	
					$('.mainMenu').show();
				} else {
					$('footer').css('left','8px').css('right','auto').css('margin-left','0px');
					$('article').css('left','0px').css('margin-left','0px');
					$('nav').css('display','none');
					$('header').css('background','transparent');
					$('header').css('width',wantedSideW+'px');
					$('.mainMenu').hide();
					$('header').mouseenter(function(){
						$('.mainMenu').show();
						$('header').css('background','#1B1B1B');
					});
					$('header').mouseleave(function(){
						$('.mainMenu').hide();
						$('header').css('background','transparent');
					});
				}
			}
        } else {
			$('nav').css('display','block');
			$('footer').css('margin-left','auto').css('right','0px').css('left','0px');
			$('article').css('margin-left','auto');
			$('header').css('width',wantedSideW+'px');
			$('nav').css('width',wantedSideW+'px');
			$('header').css('background','#1B1B1B');
			$('header').unbind();
        }
       

}
$(function() {
	if(window.location.pathname!="/"){
		layout();
	}
    //adjustStyle($(window).width());
    //window.status=$(window).width();
    $(window).load(function() {
		if($('body').hasClass('home')){
		
		} else {
			layout();
		}
		
        //adjustStyle();
    });
    $(window).resize(function() {
		layout();
        //adjustStyle();
    });
});

/*
DESIGN MODE
*/
function initDesignMode() {
	$('#designMode').hover(getIn,getOut);
	function getIn(){
		$(".designModeOut").switchClass('designModeOut', 'designModeIn', 500);
	}
	function getOut(){
		$(".designModeIn").switchClass('designModeIn', 'designModeOut', 500);
	}
}
$(document).ready(function() {initDesignMode();});
/*
ANIMACIÓN INICIO
*/
function homeAnimation() {

	wW=$(window).width();
	articleW=$('article').width();
	freeWidth=wW-(articleW+16);
	wantedSideW=wW*0.19;
	neededFreeW=wantedSideW*2;
	availableSideW=freeWidth/2;
	minSideW=127;
	window.status=$(window).width()+'x'+$(window).height()+' '+availableSideW;

	if(neededFreeW>freeWidth){
		availableSideW=freeWidth/2;

		if(availableSideW>minSideW){
			headerW=availableSideW;
			navW=availableSideW;
		} else {
			if(freeWidth>minSideW){
				headerW=freeWidth;
				navW=0;
			} else {
				headerW=0;
				navW=0;
			}
		}
	} else {
		headerW=wantedSideW;
		navW=wantedSideW;
	}


	$("body.home article>h1").detach().prependTo(".nav");
	$("nav").addClass('start');
	$("header").addClass('start');	
	$("article").addClass('start');//ie7
	$("footer").addClass('start');	
	$("nav").delay(2000).animate( { width:headerW+"px" } , 700 , "swing");
	$("header").delay(2000).animate( { width:navW+"px" } , 700 , "swing", function() {
			
			layout();
		});
	
	if($("article").css('opacity')=='0'){
		$("body.home article").delay(2500).animate( { opacity:'1' } , 700 ,"easeInOutExpo");//.addClass('start');//ie7
	}
	//$("body.home article").delay(2500).removeClass('start');//ie7
}
function prepareAnimation(){

}
$(document).ready(function() {
	if(window.location.pathname=="/"){
		homeAnimation();
	}
});
/*$(window).load(function () {
	prepareAnimation();
});*/
/*
TOC (tabla de contenidos lateral para elementos h2 de la página)
*/
function getH2TOC() 
{
	if ( $("article>h2").length>= 1) {
		$(".nav.left").append('<div id="toc"><p>En esta página:</p><br/></div>');
		var selectedTab='';
		$("article>h2").each(function(i) {
			var current = $(this);
			current.attr("id", "title" + i);
			var ic= i>5 ? 0 : i;
			current.nextUntil("h2").andSelf().wrapAll('<div class="tab '+ i +'" />');
			var tab = current.parent();
			if(i==0){
				tab.addClass('selected');
				selectedTab=tab;
			}		
			$("#toc").append(current.clone());
			$("#toc>h2:last").mouseover(function(event){
				event.preventDefault();
				if(tab.hasClass('selected')){
					return false;
				}
				var ntH=tab.outerHeight(true);
				tab.detach();
				tab.insertBefore(selectedTab);
				tab.addClass('selected');
				selectedTab.removeClass('selected');
				selectedTab=tab;
				$(window).scrollTop(0);
			});	
		});
	}
}
function getClickH2TOC() 
{
	if ( $("article>h2").length>= 1) {
		$(".nav.left").append('<div id="toc"><p>En esta página:</p><br/></div>');
		var selectedTab='';
		$("article>h2").each(function(i) {
			var current = $(this);
			current.attr("id", "title" + i);
			var ic= i>5 ? 0 : i;
			current.nextUntil("h2").andSelf().wrapAll('<div class="tab '+ i +'" />');
			var tab = current.parent();
			if(i==0){
				tab.addClass('selected');
				selectedTab=tab;
			}		
			$("#toc").append(current.clone());
			$("#toc>h2:last").click(function(event){
				event.preventDefault();
				if(tab.hasClass('selected')){return false;}
				var ntH=tab.outerHeight(true);
				tab.detach();
				tab.insertBefore(selectedTab);
				tab.addClass('selected');
				selectedTab.removeClass('selected');
				selectedTab=tab;
				$(window).scrollTop(0);
			});	
		});
	}
}
function getImgTOC() 
{
	if ( $("article div>p.over a").length>= 1) {
		$(".nav.left").append('<div id="toc"><p>Desde esta página:</p><br/></div>');
		$("article div>p.over").each(function(i) {
			var current = $(this);
			var tocItem=current.children('a').clone();
			$("#toc").append(tocItem).append('<br />');
			$("#toc>a:last").hover(
				function () {
					current.css('display','block');
				}, 
				function () {
					current.css('display','none');
				}
			);
		});
	}
}
function getImgTOC2() 
{
	if ( $("article div>p.over a").length>= 1) {
		$(".nav.left").append('<div id="toc"><p>Desde esta página:</p><br/></div>');
		var selectedTab='';
		$("article div>p.over").each(function(i) {
			var current = $(this);
			var tocItem=current.children('a').clone();
			var tab = current.parent();
			if(i==0){
				tab.addClass('selected');
				selectedTab=tab;
			}
			$("#toc").append(tocItem).append('<br />');
			/*$("#toc>a:last").hover(
				function () {
					current.css('display','block');
				}, 
				function () {
					current.css('display','none');
				}
			);*/
			$("#toc>a:last").hover(
				function () {
					current.css('display','block');
					//alert(tab);
					if(tab.hasClass('selected')){
						return false;
					}
					tab.detach();
					tab.insertBefore(selectedTab);
					tab.addClass('selected');
					selectedTab.removeClass('selected');
					selectedTab=tab;
					$(window).scrollTop(0);
				}, 
				function () {
					current.css('display','none');
				}
			);
			/*$("#toc>a:last").mouseover(function(event){
				event.preventDefault();
				if(tab.hasClass('selected')){
					return false;
				}
				//var ntH=tab.outerHeight(true);
				tab.detach();
				tab.insertBefore(selectedTab);
				tab.addClass('selected');
				selectedTab.removeClass('selected');
				selectedTab=tab;
				current.css('display','block');
			});
			$("#toc>a:last").mouseout(function(event){
				event.preventDefault();
				current.css('display','none');
			});*/
		});
	}
}
/*
Visor del Portfolio de diseño gráfico
*/
function visorPortfolioClientes(){
	//VISOR de proyecto
	/*if ( $("article h2").length>= 1) {
		$(".nav.left").append('<div id="toc"><p>En esta página:</p><br/></div>');
		$("article>h2:first").nextAll().andSelf().wrapAll('<section />');
		$("article>section>h2").each(function(i) {
			var current = $(this);
			current.nextUntil("h2").andSelf().wrapAll('<div class="client notselected tab tab'+ i +'" />');
			var tab = current.parent();
			var alink=tab.children('h2 a').attr('href');
			var atitle=tab.children('h2 a').attr('title');
			tab.children('p img').wrap('<a href="'+ alink +'" title="'+atitle+'"');
			$("#toc").append(current.clone());
			$("#toc>h2:last").hover(
				function () {
					tab.children('h2').css('display','block');
				}, 
				function () {
					tab.children('h2').css('display','none');
				}
			);
			tab.hover(
				function () {
					tab.children('h2').css('display','block');
				}, 
				function () {
					tab.children('h2').css('display','none');
				}
			);
		});
	}*/
	//VISOR de lista de proyectos
	if( $("article h3").length>= 1){
		$("article>h3:first").add('article>p:first').wrapAll('<div class="visorFotos"><div id="slider">');
		//$("article>h3:first").add('article>p:first').wrapAll('<div id="slider">');
		//$("article>h3").add('article>p:first').wrapAll('<section class="visorFotos">');
		//$("article h3").detach();
		//$("#slider").wrapAll('<div class="visorFotos">');
		//$("article>p:first").wrapAll('<div id="slider">');
		$("article h3").css('display','none');
		$("div.visorFotos").css('display','block').css('position','relative').css('width','740px').css('height','340px').css('overflow','hidden');
		$(window).load(function () {
			setUpSlider();
		});
	}
	function setUpSlider(){
		var slider=$("div#slider");
		var npics=$("img", slider).length;
		if(npics<=1) return false;
		var sliderWidth=0;
		var positions=new Array();
		var currentPosition=0;
		//$("section.visorFotos>div#slider img")
		$("img", slider).each(function(i){
			positions[i]=0-sliderWidth;
			sliderWidth+=$(this).width();
			$(this).css('display','inline');
		});
		slider.width(sliderWidth);
		//Prev & Next Btns
		//$("div.visorFotos").prepend('<div id="sliderControls"><div id="sliderPrev"></div><div id="sliderNext"></div></div>');
		$("div.visorFotos").prepend('<div id="sliderControls"><div id="sliderPrev" class="sliderPrev"></div><div id="sliderNext" class="sliderNext"></div></div>');
		//$("#sliderControls").html('<div id="sliderPrev"> </div><div id="sliderNext"> </div>');
		$("#sliderNext").click(function(){
			currentPosition=currentPosition+1;
			var isLast = currentPosition>=positions.length ? true : false;
			currentPosition= isLast ? 0 : currentPosition;
			moveSlider(positions[currentPosition]);
		});
		$("#sliderPrev").click(function(){
			currentPosition=currentPosition-1;
			var isFirst = currentPosition<=-1 ? true : false;
			currentPosition= isFirst ? positions.length-1 : currentPosition;
			moveSlider(positions[currentPosition]);
		});
		if($.browser.msie){
			sliderControlsHover();
		}
		/*window.console.log("%s ::: %o",positions,this);*/
	}
	function moveSlider(destX){
		$("#slider").animate({left:destX},500,'easeInOutExpo');
	}
	function sliderControlsHover(){
		$("div.sliderNext").hover(function () {
			$(this).addClass('sliderNextArrow');
		}, function () {
			$(this).removeClass('sliderNextArrow');
		});
		$("div.sliderPrev").hover(function () {
			//alert('hey there ! '+$(this).css('background-url'));
			$(this).addClass('sliderPrevArrow');
		}, function () {
			$(this).removeClass('sliderPrevArrow');
		});
		/*$("div.sliderNext").css('background', 'url(http://www.actidea.com.es/imagenes/arrow-R.png) no-repeat right center');
		$("div.sliderPrev").mouseover(function() {
			alert('hey there ! '+$(this).css('background'));
			$(this).css('background', 'url(http://www.actidea.com.es/imagenes/arrow-L.png) no-repeat left center');
		});
		$("div.sliderNext").mouseover(function() {
			$(this).css('background', 'url(http://www.actidea.com.es/imagenes/arrow-R.png) no-repeat right center');
		});*/		
	}
}



$(document).ready(function() {
	//TODO it's a mess and not clean should be automatic from content maybe make tocs in php on server
	//plus not good -easier to edit wysiwyg, but- for non image displays, and not accesible...
	// || window.location.pathname.indexOf('gestion-cultural/distribucion')!=-1
	if(window.location=="http://www.actidea.com/diseno-grafico-y-comunicacion/portfolio" || window.location=="http://www.actidea.com/diseno-grafico-y-comunicacion/portfolio/"){
		getImgTOC();
	} else {
		if(window.location.pathname.indexOf('diseno-grafico-y-comunicacion/portfolio/')!=-1){
			visorPortfolioClientes();
		}	
	}

		//alert(window.location);
		if(window.location.pathname.indexOf('/actividades')!=-1){
			//
			//alert(window.location);
			if(window.location=="http://www.actidea.com/actividades" || window.location=="http://www.actidea.com/actividades/"){
				//alert('ooo');
				getImgTOC2();
			} else {
				//alert(window.location);
				//getH2TOC();
				getClickH2TOC();
			}
		} else {
			var pip=domainBase+'gestion-cultural/distribucion';
			var pip2=pip+'/';
			var pip3=domainBase+'gestion-cultural/trayectoria';
			var pip4=pip3+'/';
			//var pep='gestion-cultural/distribucion';
			//alert(window.location.pathname);
			if(window.location==pip || window.location==pip2 ||window.location==pip3 || window.location==pip4){
				//alert(pip);
				getImgTOC();
			} else {
				getH2TOC();
			}
		}
});




/*
CALENDAR
*/
$(function () {
	$('.date_has_event').each(function () {
		var distance = 10;
		var time = 0;
		var timeout = 0;
		var hideDelay = 0;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $(this);
		//var popup = $('.events ul', this).css('opacity', 0);
		var popup = $('.events ul', this).css('display', 'none');
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				popup.css({
					top: 20,
					left: 26,
					"z-index":10000,
					display: 'block'
				})
				/*.animate({
					bottom: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					
				});*/
				
				beingShown = false;
				shown = true;
			}
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				/*popup.animate({
					bottom: '-=' + distance + 'px',
					opacity: 0
				}, timeout, 'swing', function () {
					
					popup.css('display', 'none');
				});*/
				popup.css({
					top: 0,
					left: 0,
					"z-index":10000,
					display: 'none'
				});
				shown = false;
			}, hideDelay);
		});
	});
});
/*
	xBreadcrumbs (Extended Breadcrums) jQuery Plugin
	2009 ajaxBlender.com
	For any questions please visit www.ajaxblender.com 
	or email us at support@ajaxblender.com
*/

;(function($){
	/*  Variables  */
	$.fn.xBreadcrumbs = function(settings){
		var element = $(this);
		var 
		
		settings = $.extend({}, $.fn.xBreadcrumbs.defaults, settings);

		function _build(){
			if(settings.collapsible){
				var sz = element.children('LI').length;
				element.children('LI').children('A').css('white-space', 'nowrap').css('float', 'left');
				element.children('LI').children('A').each(function(i, el){
					if(i != sz - 1){
						$(this).css('overflow', 'hidden');
						$(this).attr('init-width', $(this).width());
						$(this).width(settings.collapsedWidth);
					}
				});
			}
			
            element.children('LI').mouseenter(function(){
                if($(this).hasClass('hover')){ return; }
                
            	_hideAllSubLevels();
            	if(!_subLevelExists($(this))){ return; }

            	// Show sub-level
            	var subLevel = $(this).children('UL');
            	_showHideSubLevel(subLevel, true);
            	
            	if(settings.collapsible && !$(this).hasClass('current')){
            		var initWidth = $(this).children('A').attr('init-width');
            		$(this).children('A').animate({width: initWidth}, 'normal');
            	}
            });
            
            element.children('LI').mouseleave(function(){
                var subLevel = $(this).children('UL');
                _showHideSubLevel(subLevel, false);
                
                if(settings.collapsible && !$(this).hasClass('current')){
                	$(this).children('A').animate({width: settings.collapsedWidth}, 'fast');
                }
            });
		};
		
		function _hideAllSubLevels(){
			element.children('LI').children('UL').each(function(){
                $(this).hide();
                $(this).parent().removeClass('hover');
			});
		};
		
		function _showHideSubLevel(subLevel, isShow){
			if(isShow){
                subLevel.parent().addClass('hover');
                if($.browser.msie){
                	var pos = subLevel.parent().position();
                	subLevel.css('left', parseInt(pos['left']));
                }
				if(settings.showSpeed != ''){ subLevel.fadeIn( settings.showSpeed ); } 
				else { subLevel.show(); }
			} else {
                subLevel.parent().removeClass('hover');
                if(settings.hideSpeed != ''){ subLevel.fadeOut( settings.hideSpeed ); } 
                else { subLevel.hide(); }
			}
		};
		
		function _subLevelExists(obj){
			return obj.children('UL').length > 0;
		};
		
		//    Entry point
		_build();
	};
	
	/*  Default Settings  */
	$.fn.xBreadcrumbs.defaults = {
		showSpeed:        'fast',
		hideSpeed:        '',
		collapsible:      false,
		collapsedWidth:   10
	};
})(jQuery);
$(document).ready(function(){
	//TODO detach and position elsewhere according to settings (if not done with php template parts how to know where? =>additional class on body?)
	$('#xbreadcrumbs').detach().insertAfter("article>h1:first").xBreadcrumbs();
});
/*-------------------------------------------------------------------- 
 * jQuery plugin: customInput()
 * by Maggie Wachs and Scott Jehl, http://www.filamentgroup.com
 * Copyright (c) 2009 Filament Group
 * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses.
 * Article: http://www.filamentgroup.com/lab/accessible_custom_designed_checkbox_radio_button_inputs_styled_css_jquery/  
 * Usage example below (see comment "Run the script...").
--------------------------------------------------------------------*/

/*
jQuery.fn.customInput = function(){
//alert('hola');
	$(this).each(function(i){	
		if($(this).is('[type=checkbox],[type=radio]')){
			var input = $(this);
			//alert(input);
			// get the associated label using the input's id
			var label = $('label[for='+input.attr('id')+']');
			
			//get type, for classname suffix 
			var inputType = (input.is('[type=checkbox]')) ? 'checkbox' : 'radio';
			
			// wrap the input + label in a div 
			$('<div class="custom-'+ inputType +'"></div>').insertBefore(input).append(input, label);
			
			// find all inputs in this set using the shared name attribute
			var allInputs = $('input[name='+input.attr('name')+']');
			
			// necessary for browsers that don't support the :hover pseudo class on labels
			label.hover(
				function(){ 
					$(this).addClass('hover'); 
					if(inputType == 'checkbox' && input.is(':checked')){ 
						$(this).addClass('checkedHover'); 
					} 
				},
				function(){ $(this).removeClass('hover checkedHover'); }
			);
			
			//bind custom event, trigger it, bind click,focus,blur events					
			input.bind('updateState', function(){	
				if (input.is(':checked')) {
					if (input.is(':radio')) {				
						allInputs.each(function(){
							$('label[for='+$(this).attr('id')+']').removeClass('checked');
						});		
					};
					label.addClass('checked');
				}
				else { label.removeClass('checked checkedHover checkedFocus'); }
										
			})
			.trigger('updateState')
			.click(function(){ 
				$(this).trigger('updateState'); 
			})
			.focus(function(){ 
				label.addClass('focus'); 
				if(inputType == 'checkbox' && input.is(':checked')){ 
					$(this).addClass('checkedFocus'); 
				} 
			})
			.blur(function(){ label.removeClass('focus checkedFocus'); });
		}
	});
};*/
/*
CUSTOM INPUT
*/
/*$(document).ready(function() {
	$('input.cf-box-b').customInput();
});*/

