$(function() {

    $('.help').live('click',function() {
        var dialogId = "#" + $(this).attr('rel');
        $(dialogId).dialog({
           width: 650,
           resizable: false,
           close: function() { $(this).dialog( 'destroy' );}
	    });
		return false;
	});

   $('#newsletter-signup').click(function(){
      $.ajax({
        type: "POST",
        url: "index.php",
        data: { 
          page : "/modules/newsletter/ajax_newsletter_signup", 
          email : $('#newsletter-email').val()
        },
        success: function(subscriber) {
         $("#newsletter-dialog").dialog({
	    width: 650,
	    resizable: false,
	    modal:true,
	    close: function() { $(this).dialog( 'destroy' );}
	    });
	     if(subscriber.id != null)
         {
            $("#newsletter-error").hide();
            $("#newsletter-success").show();
         }
         else
         {
            $("#newsletter-error").show().text(subscriber.errors.email);
            $("#newsletter-success").hide();
         }

	    },
        dataType:"json"
      });
      return false;
   });
   jQuery("#loading").ajaxStart(function(){jQuery(this).show()});
   jQuery("#loading").ajaxStop(function(){jQuery(this).hide()});


    $('#newsletter-email').focus(function(){
    $(this).val("");
    });

    $('#productImage a').lightBox();
    $('#enlargeImage a').lightBox();

    $('#thumbnails a').click(function(){
        $("#mainImage").attr('src', $(this).attr('href'));
        $('#productImage a').attr('href', $(this).attr('rel'));
        $('#enlargeImage a').attr('href', $(this).attr('rel'));
        return false;
    });

    $('input[name="shipping"]:radio').click(function() {
        if ($(this).attr("id") == "Siuntos24_Terminal2Terminal")
        {
            $("table.siuntos24").show();
        }
        else
        {
            $("table.siuntos24").hide();
        }
    });

    $('select[name="shipping_terminal_city"]').change(function(){
      $.ajax({
        type: "POST",
        url: "index.php",
        data: { 
          page : "/modules/siuntos24/ajax/get_city_terminals", 
          shipping_terminal_city : $('select[name="shipping_terminal_city"]').val(),
          shipping_terminal_id : $('select[name="shipping_terminal_id"]').val()
        },
        success: function(data) {
            $('.shipping-terminal').html(data);
	    }
      });
   });

});
