var allowCookies = (getCookie('SID') != '');

function clearText(field, def) {
	if ($(field).val() == def) {
		$(field).val('').css('color', '#333333');
	}
}

function resetText(field, def) {
	if ($.trim($(field).val()) == '') {
		$(field).val(def).css('color', '#999999');
	}
}

function getExpDate(days, hours, minutes) {
	var expDate = new Date();	
	expDate.setDate(expDate.getDate() + parseInt(days));
	expDate.setHours(expDate.getHours() + parseInt(hours));
	expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
	return expDate.toGMTString();
}

function getCookieVal(offset) {	
	var endstr = document.cookie.indexOf(';', offset);	
	if (endstr == -1) {
		endstr = document.cookie.length;
	}	
	return unescape(document.cookie.substring(offset, endstr));	
}

function getCookie(name) {	
	var arg = name + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;	
	while (i < clen) {		
		var j = i + alen;		
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal(j);	
		}	
		i = document.cookie.indexOf(' ', i) + 1;		
		if (i == 0) {
			break;	
		}
	}	
	return '';	
}

function setCookie(name, value, expires) {	
	document.cookie = name + '=' + escape(value) + '; expires=' + expires + ';path=/';
}

function deleteCookie(name) {
	if (getCookie(name)) {
		document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
	}
}

// jquery highlight extension
(function($) {
		  
	$.fn.myHighlight = function (options) {
			
		var opts = $.extend({}, $.fn.myHighlight.defaults, options);
		
		return this.each(function () {

			$(this).highlightFade({
				start: opts.startColor,
				speed: 600,
				complete: function () {
					$(this).highlightFade({start: opts.startColor, speed: 2000});
				}
			});
			
		});
		  
	};
	
	$.fn.myHighlight.defaults = {startColor: '#ffff99'};
	
})(jQuery);

function anchorHilite() {		

	if (arguments.length) {
		var a = arguments[0];
	} else {
		var a = window.location.hash;
	}

	if (a.indexOf('#') != -1) {		
	
		var id = a.slice(1, a.length);			
			
		if (id != '') {
			
			if ($('#' + id).length == 0) {
				return;
			}
				
			$('#' + id).myHighlight();
				
		}
	}
}

$(function() {
		   
	$('#naflags').hover(function () {
		$('#location').html('Go to 8th & Walton U.S and Canada &rarr;');
		$(this).find('img').attr('src', '/img/usa-canada-on.png');
	}, function () {
		$('#location').html('');
		$(this).find('img').attr('src', '/img/usa-canada-off.png');
	});
	
	$('#newsletterEmail').click(function () {
		clearText(this, 'Su direccion de correo electronico');					
	}).blur(function () {
		resetText(this, 'Su direccion de correo electronico');	
	});
	
	anchorHilite();
	
	$('.top-links > li > a').click(function () {
		anchorHilite($(this).attr('href'));
	});
	
	if (!allowCookies) {
		
		$('a.cookie-prompt,:button').click(function () {
		
			$("#noCookieMsg").jqm({
				modal: true,
				overlay: 70,
				onHide: function (hash) {
					hash.w.hide();
					hash.o.remove();
				}
			}).jqmShow();
			
			return false;
		
		});
		
		$('form.cookie-prompt').submit(function () {
		
			$("#noCookieMsg").jqm({
				modal: true,
				overlay: 70,
				onHide: function (hash) {
					hash.w.hide();
					hash.o.remove();
				}
			}).jqmShow();
			
			return false;
		
		});
			
	}
	
});