var flash = null;
var $m = jQuery.noConflict();
var curvyCornersVerbose = false;

function lyteclose(refresh) {
	myLytebox.end();
	myLytebox = new LyteBox();
	if (refresh) {
		new PeriodicalExecuter(function(pe) {
			pe.stop();
			location.reload(true);
		}, 1);
	}
}

function block(msg, unblock) {
	$m.blockUI({
		message: '<h1>'+msg+'</h1>',
		css: {border:'none',padding:'15px',backgroundColor:'#000000','-webkit-border-radius':'10px','-moz-border-radius':'10px',opacity:.5,color:'#ffffff'}
	});
	if (unblock != 'undefined' && unblock) setTimeout($m.unblockUI, (typeof unblock == 'boolean' ? 2000 : unblock));
}

function unblock() {$m.unblockUI();}

function init() {
	humanMsg.setup();
	rounded();
	titles();
	quotes();
	hinters();
	counters();
	if (flash != 'undefined' && flash) {
		humanMsg.displayMsg(flash);
		flash = null;
	}
}


function rounded() {
	var settings = $H({
		'.round, .btn, .menu': {tl: { radius: 10 }, tr: { radius: 10 }, bl: { radius: 10 }, br: { radius: 10 }, antiAlias: true},
		'.humanMsg': {tl: { radius: 10 }, tr: { radius: 10 }, bl: { radius: 10 }, br: { radius: 10 }, antiAlias: true},
		//'ul#navigation a': {tl: { radius: 10 }, tr: { radius: 10 }, bl: { radius: 10 }, br: { radius: 10 }, antiAlias: true},
		'#toolBar': {bl: { radius: 10 }, br: { radius: 10 }, antiAlias: true}
	});
	settings.each(function (pair) {
	    curvyCorners(pair.value, pair.key);
	});
}

function hinters() {
	$$('.hinter').each(function(item) {
		if (item.type == 'password') {
			var styles = ['font-family','font-size','line-height'];
			hinter = new Element('div',{'class': 'typin-password'}).update(item.readAttribute('hinter'));
			$(item).insert({before: hinter});
			hinter.id = $(item).identify() + 'Hinter';
			hinter.writeAttribute('itemid',$(item).identify());
			$(item).writeAttribute('hinterid',hinter.id);
			hinter.clonePosition($(item));
			hinter.observe('click',function() {e = $(this);$(e.readAttribute('itemid')).focus();});
			for (var i=0; i<styles.length; i++) {
				hinter.setStyle(styles[i],$(item).getStyle(styles[i]));
			}
		} else item.value = item.readAttribute('hinter');
			
		item.observe('focus',function(event) {
			e = $(this);
			if (e.type == 'password') {
				if (e.readAttribute('hinterid')) $(e.readAttribute('hinterid')).hide();
			} else if ((e.tagName.toLowerCase() == 'input' || e.tagName.toLowerCase() == 'textarea') && e.value == e.readAttribute('hinter')) e.value = '';
			e.addClassName('typin');
		});			
		item.observe('blur',function(event) {
			e = $(this);
			if (new String(e.value).blank()) {				
				if (e.type == 'password') {
					if (e.readAttribute('hinterid')) $(e.readAttribute('hinterid')).show();
				} else  {
					e.value = e.readAttribute('hinter'); 
				}
				e.removeClassName('typin');
			}
		});
	});
}

function titles() {
	$m('.sifrWhite').sifr({font: '/img/AniversRegular', textAlign: 'left',	color: '#ffffff', save: true});
	$m('.sifr').sifr({font: '/img/AniversRegular', textAlign: 'left', color: '#4f7356', save: true});
}

function quotes() {
	$m('span.quote').each(function() {
		text = $m(this).text();	// Get the text of the span
		text = text.replace( /\((.*)\)/gi, " " );	// Get rid of unwanted charactors
		// Check if this is to be a right or left pull quote and output it
		if ($m(this).is('.right')) $m(this).parent().before('<blockquote class="quote right"><p><span class="lquote">&ldquo;</span>'+ text +'<span class="rquote">&rdquo;</span></p></blockquote>');
		else $m(this).parent().before('<blockquote class="quote"><p><span class="lquote">&ldquo;</span>'+ text +'<span class="rquote">&rdquo;</span></p></blockquote>');
	});
        // End pull quote code
}

function counters() {
	$m('.counter').each(function() {
		limiter(this);
	});
}

function limiter(textarea) {
	textarea = $(textarea);
	info = $(textarea.readAttribute('counter'));
	var limit = textarea.readAttribute('limit');
	var limittype = textarea.readAttribute('limittype'); // negative
	var text = textarea.value; 
	var textlength = text.length;
	if (textlength > limit) {
		if (limittype == 'negative') {
			info.addClassName('negative');
			info.innerHTML = limit - textlength;
		} else {
			textarea.value = text.substr(0,limit);
			info.innerHTML = limit;
		}
		return(false);
	} else {
		info.removeClassName('negative');
		info.innerHTML = limit - textlength;
		return(true);
	}
}

function addLink(input, link) {
	if (!link) return(false);
	input = $(input);
	input.value = input.value.substring(0,input.selectionStart) + link + input.value.substring(input.selectionEnd);
	return(true);
}

$m(document).ready(init);

$m(document).ready(function() {
	/* Users module */
	
	// Get CityList throught ajax
	$m("#UserCountryId").change(function() {
		var countryId = $m(this).val();
		$m.ajax({
			url: "/cities/getCities/countryId:"+countryId+"/required:0/useModel:User",
			success: function(responseCity) { $m("#cityList").html(responseCity); }
		});
	});
	
	$m("#ClientCountryId").change(function() {
		var countryId = $m(this).val();
		$m.ajax({
			url: "/cities/getCities/countryId:"+countryId+"/required:0/useModel:Client",
			success: function(responseCity) { $m("#cityList").html(responseCity); }
		});
	});
	
	/*$m("#getState").click(function() {
		var postalCode = $m("#UserPostalCode").val();
		if(postalCode.length < 5 || postalCode.length > 5) {
			alert("Postal Code must have 5 numbers.");
		} else {
			$m.ajax({
				url: "/states/getStates/postalCode:"+postalCode+"/useModel:User",
				success: function(respGetStates) { $m("#stateList").html(respGetStates); }
			});
		}
	});*/
	
	$m("#getState").click(function() {
		var postalCode = $m("#ClientPostalCode, #UserPostalCode").val();
		if(postalCode.length < 5 || postalCode.length > 5) {
			alert("Postal Code must have 5 numbers.");
		} else {
			$m.ajax({
				url: "/states/getStates/postalCode:"+postalCode+"/useModel:Client",
				success: function(respGetStates) { $m("#stateList").html(respGetStates); }
			});
		}
	});
	
	
	// get HTML fields for register form when user type is corporate
	$m("#UserCorporate").click(function() {
		$m.ajax({
			url: "/clients/register/",
			success: function(resCorp) { $m("#corporate").html(resCorp); }
		});
		//$("#corporate").toggle();
		$m("label[for='UserWebpage']").html("Pagina Web");
	});
	
	// Show-hide user-blogger fields in register form - register
	$m("#UserPersonal, #UserBlogger").click(function() {
		$m("#corporate").empty();
	});
	
	$m("#UserPersonal").click(function() {
		$m("#webreq").remove();
	});
	
	$m("#UserBlogger").click(function() {
		$m("label[for='UserWebpage']").append("<span id=\"webreq\" class=\"requiredField\">*</span>");
	});
	
	/*$("#UserBlogger").click(function() {
		$.ajax({
			url: "/users/bloggerurl/",
			success: function(blgrres) { $("#bloggerurl").html(blgrres); } // blgrres: blogger response
		});
		$("#corporate").empty();
	});*/
	
	
	// Show/hide cupon filed when account_type is User/Blogger
	/*$("a .cupon").click(function() {
		$("#cuponField").toggle();
	});*/
	
	// Get CityList throught ajax - users
	/*$("#ClientCountryId").change(function() {
		var countryId = $(this).val();
		$.ajax({
			url: "/schiclick/cities/getCities/"+countryId,
			success: function(responseCity) { $("#cityList").html(responseCity); }
		});
	});*/
	
});

/* HTML <select> with list of countries */
function getCities() {
	var countryId = $m("#ClientCountryId").val();
	$m.ajax({
		url: "/cities/getCities/"+countryId,
		success: function(responseCity) { $m("#cityList").html(responseCity); }
	});
}
