// Redireccion
if (readCookie('visited') == null){	
	createCookie('visited','yes',1);
	createCookie('referer_np',location.href,1);
	location.href = 'index.php';
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// 
$().ready(function(){
	
		if( $("select.nice").length > 0){
			

			//fix para que jnice funcione con ajax
			// Combo Region
			$("body .virtual_select:eq(0) li").live('click', function(e){
				var $this = $(this);
				$this.parent().slideUp();
				$this.parent().prev().val($this.html());
				$this.parent().next().val($this.attr("rel"));
				
				$this.siblings("li").removeClass('selected');
				$this.addClass('selected');
			
								
				loadData('ciudades','#select2', $(this).attr('rel'));
				
				return false;
					
			});
			
			// Combo Ciudad 				
			
			$("body .virtual_select:eq(1) li").live('click', function(e){
				var $this = $(this);
				$this.parent().slideUp();
				$this.parent().prev().val($this.html());
				$this.parent().next().val($this.attr("rel"));
				$this.siblings("li").removeClass('selected');
				$this.addClass('selected');
			
				loadData('comunas','#select3', $(this).attr('rel'));
				
				return false;
					
			});
			
			
			// Combo Comuna 				
			$("body .virtual_select:eq(2) li").live('click', function(e){
				var $this = $(this);
				$this.parent().slideUp();
				$this.parent().prev().val($this.html());
				$this.parent().next().val($this.attr("rel"));
				$this.siblings("li").removeClass('selected');
				$this.addClass('selected');
							
				return false;
					
			});
			
			
		
			//$("select.nice").niceSelect();
			
			var loadData = function(filter,element,id){
				
				$.getJSON('ajax/data.php?f=' + filter + '&id=' + id, function(data) {

			  		var items = [];
			  		
				  	$.each(data, function(key, val) {
				  		if ( ( filter=='regiones' && key == 13 ) || filter == "ciudades" || filter == "comunas"   )
					    	items.push('<li class="" rel="' + key + '">' + val + '</li>');
												
					 });
						
					$(element).after($('<ul/>', {
	    				'class': 'virtual_select',
				    	html: items.join('')
					}));
					
					$(element +'~	 ul ~ ul' ).remove();
										
				});				
			};
			
			if( $("select.region").length > 0)
				loadData('regiones','#select1', 0);
			
			/* */
			$("select.region").niceSelect();
				
			/* */			
			$("select.ciudad").niceSelect();
			
			$("select.comuna").niceSelect();
						
		}
	});
	
	$(document).ready(function() {
			
			$("nav#menu a").mouseover( function() {
			  $(this).stop(true, true).animate({ opacity: 1 });
			})
			$("nav#menu a").mouseout( function() {
			  $(this).stop(true, true).animate({ opacity: 0 });
			 });
			 
			$(".shadowb").mouseover( function() {
			  $(this).stop(true, true).animate({ 'opacity': 0  });
			})
			 $(".shadowb").mouseout( function() {
			  $(this).stop(true, true).animate({ 'opacity': 1, 'filter':'' });
			 });
			
			$(".fade").css( "opacity",0);	
			
			$(".fade1 img").mouseover( function() {
			  $(".fade1").stop(true, true).animate({ opacity: 1 });
			 });
			
			$(".fade2 img").mouseover( function() {
			  $(".fade2").stop(true, true).animate({ opacity: 1 });
			 });
			
			 
			$(".fade3 img").mouseover( function() {
			  $(".fade3").stop(true, true).animate({ opacity: 1 });
			 });
			
			$(".fade4 img").mouseover( function() {
			  $(".fade4").stop(true, true).animate({ opacity: 1 });
			 });
			
			$(".fade img").mouseout( function() {
			  $(".fade").stop(true, true).animate({ opacity: 0 });
			 });
			 
			
		
			
		

			
			$("#contacto").fancybox({
				'width'				: 456,//455// 456
				'height'			: 456,//475// 456
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'
			});

			$(".terminos a").fancybox({
				'width'				: 440,//550
				'height'			: 435,//400
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'
			});
			
				
			
		});
