/**
 *  Main JS file
 *
 *  Author: Hanzo Studio
 *  URL: http://www.hanzo.es
 *
**/

// Define document ready event
$(document).ready(function() {
  
  // Init all objects
  MYEGOO_core.init();
  MYEGOO_notification_bar.init();
  MYEGOO_slider.init();
  MYEGOO_shopping_form.init();

  //alert(navigator.appName);
  browserVersion = navigator.userAgent;
  if(browserVersion.indexOf("MSIE 7") != -1)
	  MYEGOO_notification_update_explorer.init();
});

// Define  window scroll event
$(window).scroll(function () {

});

// Define resize event
$(window).resize(function() {
  MYEGOO_slider.onResizeBrowser();
});


/**
 *  AJAX JS for Myegoo
 *
 *  Author: Hanzo Studio
 *  URL: http://www.hanzo.es
 *
**/

var MYEGOO_ajax = {
  
  checkDomain : function(name, callback) {
	$.ajax({
		url: MYEGOO_config.URLs.checkDomain,
		type: "POST", 
		data: "name="+name,
	    success: function( data ) {
			callback(data);
		}
	});
  }
}

/**
 *  JS Callbacks for Myegoo
 *
 *  Author: Hanzo Studio
 *  URL: http://www.hanzo.es
 *
**/

var MYEGOO_callbacks = {
	
		
}

/**
 *  JS Core for Myegoo
 *
 *  Author: Hanzo Studio
 *  URL: http://www.hanzo.es
 *
**/

var MYEGOO_core = {
	
	init : function() {
		
	  /* Scroll to target */
	  $('a.scroll_to_target').scrollToTarget();
	  
	  /* First child - Last child */
	  $('#header .navigation li:first-child').addClass('first-child');
	  $('#features .column:first-child').addClass('first-child');
	  $('#features .column:last-child').addClass('last-child');
	  $('#full_features_list .column:first-child').addClass('first-child');
	  $('#full_features_list .column:last-child').addClass('last-child');
	  $('#full_features_list .column .item:last-child').addClass('without_border');
	  $('#price_plans .column:first-child').addClass('first-child');
	  $('#price_plans .column:last-child').addClass('last-child');
	  $('#price_plans .column ul li:first-child').addClass('first-child');
	  $('#price_plans .column ul li:last-child').addClass('last-child');
	  $('#price_plans .years_selector p:first-child').addClass('first-child');
	  $('#price_plans .years_selector p:last-child').addClass('last-child');
	  $('.sidebar .widget ul li:last-child').addClass('last-child');
	  $('#shopping_section .wrapper .column:last-child').addClass('last-child');
	  $('#footer .wrapper .navigation ul li:first-child').addClass('first-child');
	  $('.post_comments .comments_list .comment:first-child').addClass('first-child');
	  $('.post_comments .comments_list .comment:last-child').addClass('last-child');
	  
	  /* Rounded corners */  
	  DD_roundies.addRule('#main_content .wrapper','0 0 4px 4px', true);
	  DD_roundies.addRule('#shopping_section .starting_form','4px 4px 0 0 ', true);
	  DD_roundies.addRule('#price_plans','4px', true);
	  DD_roundies.addRule('#price_plans .column .price_plan_table','4px', true);
	  DD_roundies.addRule('#price_plans .column .price_plan_table ul','4px', true);
	  DD_roundies.addRule('.hfeed .post img','6px', true);  
	  DD_roundies.addRule('.button','4px', true);
	  DD_roundies.addRule('p.showhide_fullfeatures_button a','3px', true);
	  DD_roundies.addRule('#footer .wrapper .social strong','2px', true);
	  
	  /* Overlay labels */
	  $('#header .search_form input').hanzoOverlayLabels();
	  $('#shopping_section .starting_form input').hanzoOverlayLabels();
		  
	  /* Animated hovers */
	  $('#header .navigation li a' ).animatedHovers(500, ['color']);
	  $('#features .column' ).animatedHovers(500, ['background-color']);
	  $('#features .column p' ).animatedHovers(500, ['color']);
	  $('#footer .wrapper .navigation ul li a').animatedHovers(500, ['color']);
	  $('#price_plans .column .price_plan_table ul li.with_tooltip').animatedHovers(200, ['background-color','color']);
	  
	  /* Lazy images loader */
	  $('#slider img').hanzoLazyImageLoader();
		  
	  /* Add arrows */
	  $('#footer .social li strong').prepend('<span class="arrow"></span>');
	  $('#price_plans .years_selector p').append('<span class="arrow"></span>');
	  
	  /* Simple events */  
	  $('#full_features_list .column .item img').each(function(){ $(this).closest('.item').addClass('without_border'); });
	  $('#price_plans .years_selector p label').bind('click',function() {
		  $(this).closest('.years_selector').find('p').removeClass('checked'); 
		  $(this).closest('p').addClass('checked');
		  anios = $(this).attr('for');
		  if (anios == 'years_1')
		  {
			  $('#price-ena').html(MYEGOO_variables.ENA.price);
			  $('#pay-ena').html('');
			  $('#price-enp').html(MYEGOO_variables.ENP.price);
			  $('#pay-enp').html('');
		  }
		  else if (anios == 'years_3')
		  {
			  $('#price-ena').html(MYEGOO_variables.EA3.price_month);
			  $('#pay-ena').html(MYEGOO_variables.TRANSLATE.uniquePayment+MYEGOO_variables.EA3.price+MYEGOO_variables.CURRENCY_SYMBOL);
			  $('#price-enp').html(MYEGOO_variables.EP3.price_month);
			  $('#pay-enp').html(MYEGOO_variables.TRANSLATE.uniquePayment+MYEGOO_variables.EP3.price+MYEGOO_variables.CURRENCY_SYMBOL);
		  }
		  else if (anios == 'years_5')
		  {
			  $('#price-ena').html(MYEGOO_variables.EA5.price_month);
			  $('#pay-ena').html(MYEGOO_variables.TRANSLATE.uniquePayment+MYEGOO_variables.EA5.price+MYEGOO_variables.CURRENCY_SYMBOL);
			  $('#price-enp').html(MYEGOO_variables.EP5.price_month);
			  $('#pay-enp').html(MYEGOO_variables.TRANSLATE.uniquePayment+MYEGOO_variables.EP5.price+MYEGOO_variables.CURRENCY_SYMBOL);
		  }
	  });
	  
	  /* Show and hide full features list */
	  $('p.showhide_fullfeatures_button a').toggle(
			  function(){
				  $(this).removeClass('expand').addClass('collapse').find('span').html(MYEGOO_variables.TRANSLATE.hideFuncionalities);
				  $('#full_features_list').slideDown( function(){
					   $.scrollTo('#full_features_list',500,{easing:'easeInSine'});
				  }); 
				 
				  
			  },
			  function() {
				  $(this).removeClass('collapse').addClass('expand').find('span').html(MYEGOO_variables.TRANSLATE.showFuncionalities);
				  $.scrollTo('#price_plans',500,{easing:'easeOutSine'});
				  $('#full_features_list').slideUp( function() {
					  
				   }); 
				  
	  });
	  
	  
	  /* Tooltips */
	  $('#price_plans .column .price_plan_table ul li.with_tooltip').each(function(){
		 
		 // Create the tooltip from the title attribute and remove it
		 var tooltipHtml = '';
		 tooltipHtml += '<div class="tooltip">';
		 tooltipHtml += ' <div class="content">';
		 tooltipHtml +=    $(this).attr('title');
		 tooltipHtml += ' </div>';
		 tooltipHtml += ' <div class="arrow">';
		 tooltipHtml += ' </div>';
		 tooltipHtml += '</div>';
		 $(this).removeAttr('title');		 
		 $(this).append(tooltipHtml);
		 		 
		 // Add events
		 $(this).bind('mouseenter',function(){
				$(this).find('.tooltip').fadeIn('fast');
		 });			
		 $(this).bind('mouseleave',function(){
				$(this).find('.tooltip').fadeOut('fast');
		 });
		 
		  
	  });
	  
	  // Some IE bug fix
		$('.section_header .search_form div.input_text label').bind('click',function(){
		 $('.section_header .search_form div.input_text input').trigger('focus'); 
	    });
	
	  // Design fix
	  $('#shopping_section .shopping_form div.radiobutton input[name=shopping_years]').closest('.column').find('div.radiobutton:last-child').css('background-image','none');
	}
}



/**
 *  JS Notification bar for Myegoo
 *
 *  Author: Hanzo Studio
 *  URL: http://www.hanzo.es
 *
**/

var MYEGOO_notification_bar = {
	
	init : function() {
		
	  $('#notification p.icon_close a').bind('click', function(){
		  $('#notification').slideUp();	
		  return false;	  
	  });
	  setTimeout(function(){
		  $('#notification ').slideDown();
		},2000);	
	  
	  
	}
	
}

var MYEGOO_notification_update_explorer = {
		
		init : function() {
			
		  $('#update_explorer p.icon_close a').bind('click', function(){
			  $('#update_explorer').slideUp();	
			  return false;	  
		  });
		  setTimeout(function(){
			  $('#update_explorer').slideDown();
			},2000);	
		}
}

/**
 *  JS Shopping Form for Myegoo
 *
 *  Author: Hanzo Studio
 *  URL: http://www.hanzo.es
 *
**/

var MYEGOO_shopping_form = {
	
	init : function() {
		
		// Custom radiobuttons
	  	$('#shopping_section .shopping_form  input[type=radio]').hanzoNewRadio();
		$('#domain_extension').hanzoSelectToDiv();
		
		// Collapse link
		$('#shopping_section .shopping_form .shopping_footer p.collapse a').bind('click',function() { 
			MYEGOO_shopping_form.onCollapse();
			return false;
		});
		
		// Events on submit
		$('#shopping_section form#check_domain').bind('submit', function(){
			MYEGOO_shopping_form.onSubmit();
			return false;
		});

		$('#user-build-button').bind('onclick',function(){alert('hello world');});
		// Select domain type
		$('.column.domains .radiobutton label').bind('click',function(){
			registered = $('input[value="registered"]').attr('checked');

			if (registered == false) { 
				$('#transformed_domain_extension').show();
				$('.domain_extension_bubble').show();
				$('input[name="url"]').val($('input[name="url_hidden"]').val());
				$('#shopping_section .starting_form #transformed_domain_extension').fadeIn();
				$('#shopping_section .starting_form #transformed_domain_extension_free').hide();
			}
			else { 
				$('#transformed_domain_extension').hide();
				$('.domain_extension_bubble').hide();
				$('input[name="url"]').val($('input[name="free-domain"]').val());
				$('#shopping_section .starting_form #transformed_domain_extension').hide();
				$('#shopping_section .starting_form #transformed_domain_extension_free').fadeIn();
			}
		});
		// Select years
		$('.column.years .radiobutton label, .column.domains .radiobutton label').bind('click', function(){
			registered = $('input[value="registered"]').attr('checked');
			years = $('.column.years .radiobutton.checked').attr("id");
			
			if (years == '1')
			{
				if (registered == false)
				{ 
					price = MYEGOO_variables.ENP.price_month;
					only_pay = MYEGOO_variables.ENP.price;
					$('.column.last-child .price strong em').html(price);
					$('#pago_unico').html(only_pay);
					$('#descuento_anual').html("");
				}
				else
				{
					price = MYEGOO_variables.ENA.price_month;
					only_pay = MYEGOO_variables.ENA.price;
					$('.column.last-child .price strong em').html(price);
					$('#pago_unico').html(only_pay);
					$('#descuento_anual').html("");
				}
				
			}
			else if (years == '3')
			{
				if (registered == false)
				{
					price = MYEGOO_variables.EP3.price_month;
					only_pay = MYEGOO_variables.EP3.price;
					$('.column.last-child .price strong em').html(price);
					$('#pago_unico').html(only_pay);
					$('#descuento_anual').html(", "+MYEGOO_variables.TRANSLATE.youSave+MYEGOO_variables.CURRENCY_SYMBOL+((MYEGOO_variables.ENP.price*3)-MYEGOO_variables.EP3.price));
				}
				else
				{
					price = MYEGOO_variables.EA3.price_month;
					only_pay = MYEGOO_variables.EA3.price;
					$('.column.last-child .price strong em').html(price);
					$('#pago_unico').html(only_pay);
					$('#descuento_anual').html(", "+MYEGOO_variables.TRANSLATE.youSave+MYEGOO_variables.CURRENCY_SYMBOL+((MYEGOO_variables.ENA.price*3)-MYEGOO_variables.EA3.price));
				}
				
			}
			else if (years == '5')
			{
				if (registered == false)
				{
					price = MYEGOO_variables.EP5.price_month;
					only_pay = MYEGOO_variables.EP5.price;
					$('.column.last-child .price strong em').html(price);
					$('#pago_unico').html(only_pay);
					$('#descuento_anual').html(", "+MYEGOO_variables.TRANSLATE.youSave+MYEGOO_variables.CURRENCY_SYMBOL+((MYEGOO_variables.ENP.price*5)-MYEGOO_variables.EP5.price));
				}
				else
				{
					price = MYEGOO_variables.EA5.price_month;
					only_pay = MYEGOO_variables.EA5.price;
					$('.column.last-child .price strong em').html(price);
					$('#pago_unico').html(only_pay);
					$('#descuento_anual').html(", "+MYEGOO_variables.TRANSLATE.youSave+MYEGOO_variables.CURRENCY_SYMBOL+((MYEGOO_variables.ENA.price*5)-MYEGOO_variables.EA5.price));
				}
				
			}
			
			return false;
		});
		
		$('#buy_domain_submit').bind('click',function(){
			registered = $('input[value="registered"]').attr('checked');
			years = $('.column.years .radiobutton.checked').attr("id");
			if (years == '1')
			{
				if (registered == false) product = 'ENP';
				else product = 'ENA';
			}
			else if (years == '3')
			{
				if (registered == false) product = 'EP3';
				else product = 'EA3';
			}
			else if (years == '5')
			{
				if (registered == false) product = 'EP5';
				else product = 'EA5';
			}
			var name =  $('#shopping_section .starting_form input[id="url"]').val();
			
			$('form#payform input[name="type"]').val(product);
			$('form#payform input[name="title"], form#payform input[name="domain"]').val(name);
			if (registered == false)
				$('form#payform #paybox-button').click();
			else 
				$('form#freeform #freebox-button').click();

			return false;
		});
		$('#free_domain_submit').bind('click',function(){
			
			var name =  $('#shopping_section .starting_form input[id="url"]').val();
			
			// Estos valores estan pasados por PHP al formulario de registro de dominio original, que ahora permanece oculto
			var group = $('#freeform #free-group').val();
			var validated = $('#freeform #free-validated').val();
			
			$('#form_free input[name="free-domain"],#form_free input[name="free-fullname"]').attr("value",name);
			$('#form_free input[name="free-group"]').attr("value",group);
			$('#form_free input[name="free-validated"]').attr("value",validated);
			$('form#form_free').submit();
			return false;
		});
		
		// Some IE bug fix
		$('#shopping_section .starting_form div.input_text label').bind('click',function(){
			$('#shopping_section .starting_form div.input_text input').trigger('focus'); 
	    });
		
	},
	
	onSubmit : function(){
		
		// Check for domain
		var name =  $('#shopping_section .starting_form input[id="url"]').val();
		if($.trim(name) != "") {
			$('#shopping_section .starting_form button span').addClass('loading')
			$('#shopping_section .starting_form button span').html(MYEGOO_variables.TRANSLATE.loading);
			MYEGOO_ajax.checkDomain(name, MYEGOO_shopping_form.onReceive);
		}
	},

	onCollapse : function(){
		$('#shopping_section .starting_form button').removeClass('loading')
		$('#shopping_section .starting_form button span').html(MYEGOO_variables.TRANSLATE.create);
		$('#shopping_section .starting_form div.input_text').removeClass('available_name');
		$('#shopping_section .starting_form .domain_extension_bubble').fadeOut();
		$('#shopping_section .starting_form .transformed_select').fadeOut();
		$('#shopping_section .shopping_form').slideUp();
	},
	
	onReceive : function(data) {
		// data viene como STRING, asi que lo convertimos a JSON para procesarlo
		myJSON = eval("(" + data + ");");
		$('#shopping_section .starting_form input[name="url"]').val(myJSON.domain).trigger('focus');
		$('input[name="url_hidden"]').val(myJSON.domain);
		$('input[name="free-domain"]').val(myJSON.ename);
		$('#shopping_section .starting_form select#domain_extension').val(myJSON.extension).trigger('change');
		
		// Expand component
		$('#shopping_section .starting_form button span').removeClass('loading')
		$('#shopping_section .starting_form button span').html(MYEGOO_variables.TRANSLATE.newone);
		$('#shopping_section .starting_form div.input_text').removeClass('not_available_name')
		.addClass('available_name');
		$('#shopping_section .starting_form .domain_extension_bubble').fadeIn();
		$('#shopping_section .starting_form .transformed_select').removeClass('not_available_name');
		$('#shopping_section .starting_form #transformed_domain_extension').fadeIn();
		//$('#shopping_section .starting_form .transformed_select #transformed_domain_extension_free').hide();
		$('#shopping_section .shopping_form').slideDown();
	}
}


/**
 *  JS Slider for Myegoo
 *
 *  Author: Hanzo Studio
 *  URL: http://www.hanzo.es
 *
**/

var MYEGOO_slider = {
	
	config : {
		
		item_fullview_width : 330,
		item_normalview_width : 164,
		item_borders_width : 1,
		item_height : 305,
		items_advance : 3
	},
	
	init : function() {
		
		this.prepareSliderHTML();
		this.addEvents();
		this.onResizeBrowser();
		
	},
	
	// Modify the html of the slider
	prepareSliderHTML : function() {
		
		// Create controls
		var controlsHtml = '';
		controlsHtml += '<div class="controls">';
        controlsHtml += '  <a class="next" href="#next">'+MYEGOO_variables.TRANSLATE.next+'</a>';
        controlsHtml += '  <a class="previous href="#previous">'+MYEGOO_variables.TRANSLATE.prev+'</a>';
        controlsHtml += '</div>';
		
		$('#slider').append(controlsHtml);
				
		// Create slider mask
		$('#slider .items').wrap('<div class="slider_mask"></div>');	
		
		// Adjust width and position of the items container
		var numOfItems = $('#slider .items .item').length;
		var itemsContainerWidth = (((numOfItems)*(this.config.item_normalview_width+this.config.item_borders_width))+this.config.item_normalview_width+this.config.item_borders_width+20)+'px';
		$('#slider .items').css('width', itemsContainerWidth);
		$('#slider .items').css('left', '0px');
		
		// Wrap title text for fade effect
		$('#slider .items h2').each(function() {
            $(this).contents().wrap('<span></span>');
        });
		
		// Create each item mask and set positions
		$('#slider .items .item').each(function() {	
			$(this).wrap('<div class="item_mask"></div>');	
			$(this).parent('.item_mask').css('width',MYEGOO_slider.config.item_normalview_width+'px');				
			$(this).css('left','-'+((MYEGOO_slider.config.item_fullview_width-MYEGOO_slider.config.item_normalview_width)/2)+'px');
		});
		
		
	},
	
	addEvents : function() {
		
		// Item events
		$('#slider .items .item').each(function() {			
			
			$(this).bind('mouseenter', function() {

					$(this).stop().animate({
						left: 0
					},200);
					
					$(this).closest('.item_mask').stop().animate({
						width: MYEGOO_slider.config.item_fullview_width+'px'
					},200, function() {
					  $(this).find('h2').stop().slideDown('fast', function(){
					  		$(this).find('span').stop().fadeIn('fast');
					  });					
					});
					
			});
			
			$(this).bind('mouseleave', function() {
				
					$(this).find('h2 span').stop().fadeOut('fast');
					$(this).find('h2').stop().slideUp('fast');
					
					$(this).stop().animate({
						left: '-'+((MYEGOO_slider.config.item_fullview_width-MYEGOO_slider.config.item_normalview_width)/2)+'px'
					},200);
					
					$(this).closest('.item_mask').stop().animate({
						width: MYEGOO_slider.config.item_normalview_width+'px'
					},200);
				}			
			);
			
		});
		
		// Controls events
		$('#slider .controls a.next').bind('click',function() {
			
			MYEGOO_slider.controlNext();
			return false;
			
		});
		
		$('#slider .controls a.previous').bind('click',function() {
			
			MYEGOO_slider.controlPrevious();
			return false;
		});
		
	},
	
	// When control next is triggered
	controlNext : function(){
		
			var currentLeft = parseInt($('#slider .items').css('left'));
			var newLeft = currentLeft-((MYEGOO_slider.config.item_normalview_width+MYEGOO_slider.config.item_borders_width)*MYEGOO_slider.config.items_advance);
			var itemsContainerTotalWidth = $('#slider .items').width();
			
			if(newLeft > itemsContainerTotalWidth*-1) {  
			    MYEGOO_slider.checkControlsVisibility(newLeft);
				$('#slider .items').stop().animate({
				   left: newLeft+'px'
			    }, 500);
			}
	},
	
	// When control previous is triggered
	controlPrevious : function(){
		
			var currentLeft = parseInt($('#slider .items').css('left'));
			var newLeft = currentLeft+((MYEGOO_slider.config.item_normalview_width+MYEGOO_slider.config.item_borders_width)*MYEGOO_slider.config.items_advance);
				
			if (newLeft >= 0) newLeft = 0;
			
			MYEGOO_slider.checkControlsVisibility(newLeft);
			
			$('#slider .items').stop().animate({
					left: newLeft+'px'
			}, 500);
			
	},
	
	checkControlsVisibility : function(newLeft) {
		 
		 var maskWidth = $('#slider .slider_mask').width();
		 var itemsWidth = $('#slider .items').width();
		 //console.log('left:'+newLeft+' maskWidth:'+maskWidth+' itemsWidth:'+itemsWidth);
		 
	     if (newLeft == 0) { 
		    $('#slider .controls a.previous').fadeOut();
		 } else {
		    $('#slider .controls a.previous').fadeIn();
		 }
		 
		 if(newLeft-maskWidth < itemsWidth*-1) {
			 $('#slider .controls a.next').fadeOut();
		 } else {
		     $('#slider .controls a.next').fadeIn();
		 }
	
	},
	
	onResizeBrowser : function() {
		var currentLeft = parseInt($('#slider .items').css('left'));
		this.checkControlsVisibility(currentLeft);
	}
	
	
}




