var TRANS_VIEWALLRESULTS = "viewallresults";
var TRANS_CONFIRMDELETEROLE = "confirmdeleterole";
var TRANS_CUSTOMIZE = "customize";

jQuery(function()
{
	window.educared = new EducaRed();
	window.educared.init();
});

/**
 * EducaRed Object Constructor
 */
function EducaRed()
{
	this.portlets = new Object();
	this.translations = new TranslationCollection();
	this.registerTranslations();
}

/**
 * Initializes EducaRed Settings
 */
EducaRed.prototype.init = function()
{	
	jQuery("#header .localization a").click(this.toggleLanguage);
	jQuery("#div-seleccion-pais .buttons input[type=button]").click(this.toggleLanguage);
	
	jQuery(document).ready(this.hideFooter);

	jQuery("#footer li.browse").click(this.toggleFooter);
	
	this.setFormClasses();
	
	// Detects whether we are in IE6/7 using the .ie6 class that Liferay
	// adds to the HTML element
	if (jQuery(".ie6").length > 0)
	{
		this.setIE6Compatibility();
	}
	
	
	
	jQuery("div.home-highlight").homePortlet();
	
	//treeView
	jQuery("div.content-administration li>ul").addClass("submenu");

	   
	jQuery("div.content-administration ul").parents("li").append(
		jQuery("<a href=\"#\" alt=\"expand\" class=\"expand display-block seeVariation\"></a>")
	 );
	
	//jQuery("div.content-administration").treeViewPortlet("submenu");
	
	jQuery("a.expand").click(function() {
		jQuery(this).toggleClass("expand");
		jQuery(this).toggleClass("collapse");
	
		var scope = jQuery(this).parent();
		jQuery("ul.submenu", scope).slideToggle(500);
	    return false;
	    
	  });
	
	jQuery("#userFilter").click(function(){
		var micontrol = jQuery("#localeIdentify"); 
			if (this.checked) 
			{ 
				micontrol.removeAttr("disabled"); 
			} 
			else 
			{ 
				
				micontrol.attr("disabled", "true"); 
			} 
			
		});
		
	this.toggleStatus();
	
	
	jQuery(".administration-portlet a.btn.delete").click(function()
	{
		return confirm(educared.translations.get(TRANS_CONFIRMDELETEROLE));
	});
	
	
		
		
	this.preview();
	this.showpreview(); 
	
	this.curricularTopicFunction();
	this.showCategory();
	
	jQuery("ul.dock").pluginDock();
	
	jQuery("div.vote-list").valoration();
	
};

/**
 * Toggles the appearance of the language portlet
 */
EducaRed.prototype.toggleLanguage = function()
{
	jQuery("#div-seleccion-pais").toggle(500);
	return false;
};

/**
 * Toggles the appearance of the footer
 */
EducaRed.prototype.toggleFooter = function()
{
	jQuery("#footer div.browse").slideToggle(500);
};

/**
 * Hides the footer
 */
EducaRed.prototype.hideFooter = function()
{
	jQuery("#footer div.browse").hide();
};

/**
 * Adds form classes to every form input element if they are not already
 * registered
 */
EducaRed.prototype.setFormClasses = function()
{
	jQuery("input").each(function()
	{
	    var className = null;
	    var jThis = jQuery(this);
	
	    switch (jThis.attr("type"))
	    {
	        case "text":
	        case "password":
	            className = "txt";
	            break;
	
	        case "button":
	            className = "btn";
	            break;
	
	        case "submit":
	            className = "submit";
	            break;
	        
	        default:
	            break;
	    }
	
	    if (!jThis.hasClass(className))
	    {
	        jThis.addClass(className);
	    }
	});
};

/**
 * Sets some properties and events so the UX with IE6/7 is the same as other
 * more standard-compliant browsers 
 */
EducaRed.prototype.setIE6Compatibility = function()
{
	// Registers the btn-hover class for all the input type submit and button
	// so they are mouse-relative-position-aware
	jQuery(".btn").add(".submit")
		.mouseover(function()
		{
			jQuery(this).addClass("btn-hover");
		})
		.mouseout(function()
		{
			jQuery(this).removeClass("btn-hover");
		});
};

EducaRed.prototype.registerTranslations = function()
{
	this.translations.set(TRANS_VIEWALLRESULTS,"Ver todos los resultados","es");
	this.translations.set(TRANS_CONFIRMDELETEROLE,"¿Desea eliminar el rol seleccionado?","es");
	this.translations.set(TRANS_CUSTOMIZE,"Personalizar","es");
};

EducaRed.prototype.toggleStatus = function ()
{

	if (jQuery("#userFilter").is(":checked")) 
	{
		jQuery("#localeIdentify").removeAttr("disabled");
	} 
	else 
	{
		jQuery("#localeIdentify").attr("disabled", "true");
	}   
	
}







EducaRed.prototype.preview = function ()
{
	var checked = ".showIcon";
	var valueHide = ".content-icon";
	checkChecked(checked,valueHide);
	
	checked=".showLogo";
	valueHide=".logo-content";
	checkChecked(checked,valueHide);
	
	checked=".showSubtitle";
	valueHide=".subtitle";
	checkChecked(checked,valueHide);
	
	checked=".showImage";
	valueHide=".img-content";
	checkChecked(checked,valueHide);
	
	checked=".showSeeMore";
	valueHide=".see-more";
	checkChecked(checked,valueHide);
	
	checked=".showSeeMore";
	valueHide="h5";
	checkChecked(checked,valueHide);
	
	checked=".showDescription";
	valueHide="p.description";
	checkChecked(checked,valueHide);
	
	checked=".showInitDate";
	valueHide="p.date";
	checkChecked(checked,valueHide);
	
	
	
	if (jQuery(".outstanding-view").is(":checked")) 
		{
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .configure-list li").addClass("visual-content2");
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .configure-list li").removeClass("visual-content");
			
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .dynamic-list li").addClass("visual-content2");
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .dynamic-list li").removeClass("visual-content");
			
		} 
		else 
		{
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .configure-list li").addClass("visual-content");
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .configure-list li").removeClass("visual-content2");
			
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .dynamic-list li").addClass("visual-content");
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .dynamic-list li").removeClass("visual-content2");
		}
		
		if (jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .complete-view").is(":checked")) 
		{
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .configure-list li").attr("id","complete");

			
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .dynamic-list li").attr("id","complete");
			

		} 
		else 
		{
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .configure-list li").removeAttr("id","complete");
			
			
			jQuery(".community-mydata-portlet .configure-view-dinamic-portlet .dynamic-list li").removeAttr("id","complete");
			
		}

	
}

function checkChecked(checked, valueHide){
	if (jQuery(checked).is(":checked")) 
	{
		jQuery(".dynamic-configure-list " + valueHide).show();
		jQuery(".configure-list " + valueHide).show();
	}
	else
	{
		jQuery(".dynamic-configure-list " + valueHide).hide();
		jQuery(".configure-list " + valueHide).hide();
	}
}


EducaRed.prototype.showpreview = function ()
{
	var varClick=".showIcon";
	var varToggle=".content-icon";
	checkPreview(varClick,varToggle);
	
	varClick=".showSubtitle";
 	varToggle=".subtitle";
	checkPreview(varClick,varToggle);
	
	varClick=".showLogo";
 	varToggle=".logo-content";
	checkPreview(varClick,varToggle);
	   
	varClick=".showImage";
 	varToggle=".img-content";
	checkPreview(varClick,varToggle);
	 
	varClick=".showNumberEdition";
 	varToggle=".numerMessages";
	checkPreview(varClick,varToggle);
	 
	varClick=".showDescription";
 	varToggle="p.description";
	checkPreview(varClick,varToggle);  
	varToggle="p.numerMessages";
	checkPreview(varClick,varToggle);     
	   
	varClick=".showSeeMore";
 	varToggle="p.see-more";
	checkPreview(varClick,varToggle);  
	   
	varClick=".showInitDate";
 	varToggle="p.date";
	checkPreview(varClick,varToggle);  
	  

	   jQuery(".outstanding-view").click(function(){
		if (jQuery(".outstanding-view").is(":checked")) 
		{
			jQuery(".configure-view-dinamic-portlet .configure-list li").addClass("visual-content2");
			jQuery(".configure-view-dinamic-portlet .configure-list li").removeClass("visual-content");
			
		} 
		else 
		{
			jQuery(".configure-view-dinamic-portlet .configure-list li").addClass("visual-content");
			jQuery(".configure-view-dinamic-portlet .configure-list li").removeClass("visual-content2");
		}
		
		});
	
	
		jQuery(".complete-view").click(function(){
		if (jQuery(".complete-view").is(":checked")) 
		{
			jQuery(".configure-view-dinamic-portlet .configure-list li").attr("id","complete");
			jQuery(".configure-view-dinamic-portlet .configure-list li").attr("id","complete");
			

		} 
		else 
		{
			jQuery(".configure-view-dinamic-portlet .configure-list li").removeAttr("id","complete");
			jQuery(".configure-view-dinamic-portlet .configure-list li").removeAttr("id","complete");
		}
		
		});
}

function checkPreview(varClick, varToggle){
	jQuery(varClick).click(function(){
		jQuery(varToggle).slideToggle(500);
							  });
}	

	

	
EducaRed.prototype.curricularTopicFunction = function(){
	jQuery(".select-country").change(function(){
		var linked = jQuery(this).attr("linked");
		var hijo = jQuery(this).attr("hijo");
		var firstvalue = jQuery(this).attr("firstvalue");
		var url = jQuery(this).attr("url");
		
		var _language = jQuery(this).attr("language");
		var _country = jQuery(this).attr("country");
		
		if(linked == "true"){	
			jQuery.getJSON(url,{countryId: jQuery(this).val(), language:_language, country:_country}, function(j){
				var options = '';
				options += '<option value="0">' + firstvalue + '</option>';
				
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].id + '">' + j[i].value + '</option>';
				}
				
				jQuery('#' + hijo).html(options);
				jQuery('#' + hijo + ' option:first').attr('selected', 'selected');
				jQuery('#' + hijo).change();
			})
		}
	});
}
	
	EducaRed.prototype.showCategory = function() {
		jQuery("p.expand a").click(function() {
			jQuery(this).focus();
			jQuery(this).toggleClass("expand");
			jQuery(this).toggleClass("collapse");					 
			jQuery(".category-list").slideToggle(500);
			
			var urlServicioJson = jQuery(this).attr("urlServicioJson");
			var _language = jQuery(this).attr("language");
			var _country= jQuery(this).attr("country");
			var onClickFunctionName= jQuery(this).attr("onClickFunctionName");

			jQuery.getJSON(urlServicioJson,{language:_language, country:_country}, function(j){
				var options = '';
				options += '<ul>';
				
				for (var i = 0; i < j.length; i++) {
					options += '<li>';
					if (j[i].childrenCount > 0){
						options += '<span  class="expand" parentId="' + j[i].code + '" myId="' + j[i].id + '" text="' + j[i].value + '" language="' + _language + '" country="' +_country + '" urlServicioJson="' + urlServicioJson + '"  onClickFunctionName="' + onClickFunctionName + '" ></span>';
					}
					options +='<p><a id="' + j[i].id + '" onclick="javascript: ' + onClickFunctionName + '(\'' + j[i].value + '\',\'' + j[i].id + '\');">' + j[i].value + '</a><p></li>';					
				}
				options += '</ul>';
						
				jQuery(".category-list").html(options).find("span.expand").click(click_category);
			})
									 
		});
	}
	
	function click_category (){
	
		jQuery(this).toggleClass("expand");
			
		jQuery(this).toggleClass("collapse");
		var scope = jQuery(this).parent();
		
		var parentId = jQuery(this).attr("parentId");
		var myId = jQuery(this).attr("myId");
		var text = jQuery(this).attr("text");
		
		var urlServicioJson = jQuery(this).attr("urlServicioJson");
		var _language = jQuery(this).attr("language");
		var _country= jQuery(this).attr("country");				
		var onClickFunctionName= jQuery(this).attr("onClickFunctionName");
		
		
		jQuery.getJSON(urlServicioJson,{idCatParent:parentId, language:_language, country:_country}, function(j){
			var options = '<span  class="collapse" parentId="' + parentId + '" myId="' +myId + '" text="' + text+ '" language="' + _language + '" country="' + _country + '" urlServicioJson="' + urlServicioJson + '" onClickFunctionName="' + onClickFunctionName + '" ></span><p><a id="' + myId + '" onclick="javascript: ' + onClickFunctionName + '(\'' + text + '\',\'' + myId + '\');">' + text + '</a></p>';
			
			options += '<ul>';
			
			for (var i = 0; i < j.length; i++) {
				options += '<li>';
				if (j[i].childrenCount > 0){
					options += '<span  class="expand" parentId="' + j[i].code + '" myId="' + j[i].id + '" text="' + j[i].value + '" language="' + _language + '" country="' + _country + '" urlServicioJson="' + urlServicioJson + '" onClickFunctionName="' + onClickFunctionName + '" ></span>';
				}
				options +='<p><a id="' + j[i].id + '" onclick="javascript: ' + onClickFunctionName + '(\'' + j[i].value + '\',\'' + j[i].id + '\');">' + j[i].value + '</a></p></li>';
			}
			options += '</ul>';
			jQuery(scope).html(options).find("span.expand").click(click_category);
			jQuery(scope).find("span.collapse").click(click_collapse);
		})
	    return false;
	}
		
	function click_collapse (){	
		jQuery(this).toggleClass("collapse");
		jQuery(this).toggleClass("expand");	
		var scope = jQuery(this).parent();
		jQuery("ul", scope).slideToggle(500);
		
		return false;
	}