/**
 * FullInteractive
 * -----------------------------------------------------------------
 *
 * author:           Alcaraz & Estevez Consultores
 * projectId:        
 * version:          0.1
 * creation date:    26-08-2009
 * description:      
 * notes:            
 *
 */



$(document).ready(function()
{
	/**
	 * generic lib initialisation
	 */
	ALCARAZ.htmlUtils.initExternalLinks();
	ALCARAZ.forms.initDefaultValues();
	ALCARAZ.misc.initFullLinks();
	ALCARAZ.misc.initWidgets();

	if( $.browser.msie )
	{
		ALCARAZ.cssFixes.fixIESelectors();
		ALCARAZ.cssFixes.fixLegends();
	}

	if( $.browser.mozilla )
	{
		ALCARAZ.cssFixes.fixLegends();
	}
	
	
	
	
	// load fancybox css
	$("head").append('<link href="/assets/css/jquery.fancybox-1.3.0.css" rel="stylesheet" type="text/css" />');
	
	
	
	// home banners link expansion
	$("body.home .banner").each(function(i)
	{
		var href = $(this).find("h2 a").eq(0).attr("href");
		
		$(this).css("cursor", "pointer").bind("click", function()
		{
			window.location = href;
		});
	});
	
	// link video
	$("body.home .banner a.video").fancybox({
		'hideOnContentClick': true
	});
	
	
	
	
	/**
	 *  FULL creation form
 	 *
 	 ***********************************************************************************************************************************/
	$("input[name=configurationType]")
		.bind("change", function()
		{
			if( $("#customType").attr("checked") )
			{
				$("#fullCreation-step3").stop().slideDown();
			}
			else
			{
				$("#fullCreation-step3").stop().slideUp();
			}
		})
		.triggerHandler("change");
	
	
	// help icons
	$("#createFullForm")
		.addClass("jsfied")
		.find("dl").each(function(i)
		{
			$(this).append('<img src="/assets/images/ui/icons/help.png" class="helpIcon" />');
		});
	
	$("#createFullForm dd")
		.bind("click", function()
		{
			if( $(this).css("display") == "none" )
			{
				$(this).fadeIn("slow");
			}
			else
			{
				$(this).fadeOut("slow");
			}
		})
		.each(function()
		{
			$(this).prepend('<div class="title">' + $(this).prev("dt").find("label:eq(0)").text() + '</div>');
			$(this).append('<span class="tip-arrow tip-lArrow" />');
		});
	
	$(".helpIcon").bind("click", function()
	{
		var helpIcon = this;
		
		if( $("#createFullForm dd:visible").get().length > 0 )
		{
			if( $(helpIcon).prev("dd").css("display") == "block" )
			{
				// info popup is allready visible, so hide it
				$(helpIcon).prev("dd").triggerHandler("click");
			}
			else
			{
				// hide other visible popups
				$("#createFullForm dd:visible").triggerHandler("click");
				
				// show popup
				$(helpIcon).prev("dd").triggerHandler("click");
			}
		}
		else
		{
			// no info popups visible
			$(helpIcon).prev("dd").triggerHandler("click");
		}
	});
	
	
	
	
	/**
 	 *  Effects:
 	 * ------------------------------------
 	 *  rounded corners, dropshadows, ...
 	 *
 	 ***********************************************************************************************************************************/	
	
	/**
	 *  rounded corners
	 * ------------------------------------
	 */
	ALCARAZ.misc.setCssCorners("#siteNav", "top", "6px");
	ALCARAZ.misc.setCssCorners("#siteNav", "bottom", "5px");
	
	ALCARAZ.misc.setCssCorners("#loginBox", "bottom", "15px");
	
	ALCARAZ.misc.setCssCorners("#loggedUserBox", "bottom", "8px");
	
	ALCARAZ.misc.setCssCorners("#contents", "top", "8px");
	ALCARAZ.misc.setCssCorners("#contents > #contentsHeader", "top", "8px");
	ALCARAZ.misc.setCssCorners("#mainContents > #contentsHeader", "right", "8px");
	
	ALCARAZ.misc.setCssCorners(".sectionNav:eq(0) li:first-child", "tl", "8px");
	ALCARAZ.misc.setCssCorners(".sectionNav:eq(0) li:first-child a", "tl", "8px");
	
	ALCARAZ.misc.setCssCorners(".formContainer", "top", "6px");
	ALCARAZ.misc.setCssCorners(".formContainer", "bottom", "6px");
	
	ALCARAZ.misc.setCssCorners(".innerContentsBox", "top", "5px");
	ALCARAZ.misc.setCssCorners(".innerContentsBox", "bottom", "5px");
	
	ALCARAZ.misc.setCssCorners("#siteSearchSubmit", "top", "8px");
	ALCARAZ.misc.setCssCorners("#siteSearchSubmit", "bottom", "8px");
	
	ALCARAZ.misc.setCssCorners("#pdfSpecs", "top", "5px");
	ALCARAZ.misc.setCssCorners("#pdfSpecs", "bottom", "5px");
	
	ALCARAZ.misc.setCssCorners("#home-promos .banner", "top", "5px");
	ALCARAZ.misc.setCssCorners("#home-promos .banner", "bottom", "5px");
	
	ALCARAZ.misc.setCssCorners("#home-intro", "top", "8px");
	ALCARAZ.misc.setCssCorners("#home-intro", "bottom", "8px");
	
	
	
	
	// sliding doors
	$("body.admin #contents .btn").wrap('<div class="btnWrapper"></div>');
	$("body.admin #contents input[type=submit]").wrap('<div class="submitBtnWrapper"></div>');
	$(".btnWrapper, .submitBtnWrapper").append("<span />");
	
	
	/**
	 *  dropshadows
	 * ------------------------------------
	 *
	 */
	ALCARAZ.misc.addPureCssShadow("body.library-users #magazinesList img", "4px", "-4px", "8px", "#333");
	
	
	
	
	/**
	 *  other
	 * ------------------------------------
	 *
	 */
	
	/* pretty file inputs */
	$("input[type=file].pretty, body.admin input[type=file]").each(function(i)
	{
		$(this)
			.wrap('<div class="fileWrapper"></div>')
			.parent(".fileWrapper")
			.append('<input type="text" class="fakeFile" value="' + $("label[for=" + $(this).attr("id") + "]").text() + '" />')
			.append('<div><em>examinar</em><span /></div>')
			.end()
			.css("opacity", "0")
			.bind("change", function()
			{
				$(this).next("input.fakeFile").val( $(this).val() );
			});
	});
	
	/**
	 * grayscale conversion
	 * deppends on grayscale.js
	 * if there are too many imgs it's a resource hog,
	 * so better find another way to achieve this effect
	 */
	/*
	$("#magazinesList .magazineStatus-expired").prev("a").find("img").add("img.expired").each(function()
	{
		grayscale(this);
	});
	*/
	
	$("#blog-archives ul").hide();
	$("#blog-archives h2").css("cursor", "pointer").bind("click", function()
	{
		$("#blog-archives ul").slideToggle();
	});
	
	
	
	
	if( $('body').hasClass('blog') || $('body').hasClass('forum') )
	{
		(function()
		{
			var isBlog     = $('body').hasClass('blog');
			var formEl     = $('#contentsHeader #searchform');
			var inputEl    = isBlog ? $('#s') : $('#q');
			var defaultVal = 'Buscar en el ' + (isBlog ? 'blog' : 'foro');
			
			if( inputEl.val() == '' )
			{
				inputEl.val(defaultVal);
			}
			
			inputEl.focus( function()
			{
				if($(this).val() == defaultVal){ $(this).val(""); }
			});
			
			inputEl.blur( function()
			{
				if($(this).val() === ""){ $(this).val(defaultVal); }
			});

			formEl.submit( function()
			{
				if((inputEl.val() === "") || (inputEl.val() == defaultVal))
				{
					return false;
				}
			});
		})();
	}
	
	
	
	/*
	 * blog: heighlight the category on the sectionNav on the single post pages
	 */
	if( $('body').hasClass('single') )
	{
		(function()
		{
			var postClasses = String( $("*[id^='post-'][class*='category-']").attr('class') ).split(' ');
			var regExp      = /^category-(.*)$/;
			
			for(var i = 0; i < postClasses.length; i++)
			{
				if( regExp.test(postClasses[i]) )
				{						
					var className = String(regExp.exec(postClasses[i])[0]).toLowerCase();
					var titleStr  = className.replace("category-", "").replace("-", " ");
					
					$(".sectionNav a").filter(function(i)
					{
						return String( $(this).text() ).toLowerCase() == titleStr.toLowerCase();
					}).parent("li").addClass("current");
					
					// uncomment next line to highlight only one category
					//break;
				}
			}
			
		})();
	}
	
	
	
	
	/**
	 * CSS fixes:
	 * ------------------------------------
	 * workarounds for some css related problems, especially on IE
	 * the most of this problems with Internet Exploder are fixed
	 * via additional css files linked with conditional comments,
	 * but some other problems (like some selectors) can not be fixed
	 * only with CSS or would be a PITA to fix
	 * (if you don't know what PITA means, look here... http://www.urbandictionary.com/define.php?term=PITA)
	 *
	 ***********************************************************************************************************************************/
	
	if($.browser.mozilla)
	{
		$(".submitBtnWrapper").css("display", "-moz-inline-block");
		$("#productFunctionalities li.reset").css("margin-top", "-22.2em");
	}
	
	
	if($.browser.msie)
	{
		$("#productFunctionalities li.reset").css("margin-top", "-22.2em");
		
		if($.browser.version < 8)
		{
			$('hr.clearFooter').after('<span class="clearFooter">&nbsp;</span>');
			$('hr.clearFooter').remove();
		}
		
		if($.browser.version < 7)
		{
		}
	}
	
	
	
	
});
