$.ajaxSetup({cache: false});

$(document).ready(function(){
	$("#tabs").tabs();
	initDrop();
	initLightbox();
	initCouponValutaion ();
	initStateSelector ();
	$('.accordion').accordion({
		active: ".selected",
		autoHeight: false,
		header: ".heading",
		collapsible: true,
		event: "click",
		changestart: function(event, ui) {
			ui.newHeader.parents('div.slide-block').addClass('active');
			ui.oldHeader.parents('div.slide-block').removeClass('active');
		}
	});
    
    $('#subscribe-form').submit(function() 
    {
        jQuery.ajax({
            type:'POST',
            dataType:'html',
            data:$(this).serialize(),
            url:$(this).attr('action'),
						/*
						success: function(data) {
							if(data)
							{
								if (data.match(/^err:/i) == 'err:') {
									$('#subscribe_email_error').html(data.replace('err:', ''));
								}
								else if (data.match(/^ok:/i) == 'ok:') {
									$('#subscribe_email').val('');
									$('#header ul.social > li.memino > a').removeClass('opened');
									$('#subscribe_email_error').html(data.replace('ok:', ''));
									setTimeout(function() { $("div.drop-newsletter").hide('fast', function() { $('#subscribe_email_error').html(''); }); }, 3000);
								}
							}
						}
						*/
						success: function(data) {
							if(data)
							{
								if (data.match(/^err:/i) == 'err:') {
									$('#subscribe_email_error').html(data.replace('err:', ''));
								}
								else if (data.match(/^exi:/i) == 'exi:' || data.match(/^ok:/i) == 'ok:') {
									$('#subscribe_email').val('');
									$('#header ul.social > li.memino > a').removeClass('opened');
									$("div.drop-newsletter").hide('fast', function() { $('#subscribe_email_error').html(''); });
									
									var message = data.replace('exi:', '');
									if (data.match(/^ok:/i) == 'ok:') message = data.replace('ok:', '');
									$.fancybox(message, 
									{
										'titleShow': false,
										'overlayColor': '#000',
										'overlayOpacity': 0.6,
										'scrolling': 'no'
									});
								}
							}
						}
        });
        return false;
    });
});

function initLightbox(){
	$('a.lightbox-opener').fancybox({
		'titleShow': false,
		'overlayColor': '#000',
		'overlayOpacity': 0.6,
		'scrolling': 'no',
		'onStart': function(){ 
		  if($(this.orig).hasClass('only-once'))
		  {
  		  $(this.orig).unbind('click.fb');
  		  $(this.orig).attr('href', '#');
  		}
		},
		'onComplete': function(){
		  $('#imprimir-lightbox a.btn-print').click(function(e){
		    $(window).focus(function(){
		      $('#imprimir-lightbox a.close').trigger('click');
		    });
      });
		}
	});
	
	$('a.only-once').click(function(e){
	  e.preventDefault();
	});
	
	var _closer = $('div.lightbox').find('a.close');
	_closer.click($.fancybox.close);
	_closer.click(function(){
		return false;
	});
}
function initDrop(){
	$('#header ul.social > li > a').each(function(){
		if ($(this).hasClass('blog-link')) return true;
		$(this).unbind("click").bind("click", function(){			
			if(!$(this).hasClass('opened')){
				$('#header ul.social > li > a').removeClass('opened');
				$('div.drop-social, div.drop-newsletter').hide();
				$(this).parent('li').find('div.drop-social, div.drop-newsletter').show();
				$(this).addClass('opened');
			}else{
				$(this).parent('li').find('div.drop-social, div.drop-newsletter').hide();
				$(this).removeClass('opened');
			}
			
			return false;
		});	
		
	});
    
    $('#header ul.social > li > div.drop-social > a.close').each(function(){
        $(this).unbind("click").bind("click", function(){            
            $(this).parent('div').hide();
            $('#header ul.social > li > a').each(function(){
                if($(this).hasClass('opened')){
                    $(this).removeClass('opened');
                }
            });
            return false;
        });    
        
    });
    
    $('#header ul.social > li > div.drop-newsletter > a.close').each(function(){
        $(this).unbind("click").bind("click", function(){            
            $(this).parent('div').hide();
            $('#header ul.social > li > a').each(function(){
                if($(this).hasClass('opened')){
                    $(this).removeClass('opened');
                }
            });
            return false;
        });    
        
    });
}


function initCouponValutaion (){
	if ($('ul.rating').hasClass('no-ajax')) return true;
	//if ($('ul.rating').hasClass('voted')) return true;
	
	$('ul.rating > li').unbind("mouseover").bind("mouseover", function (){
		var ul_selector = $(this).parents("ul");
		if (! ul_selector.hasClass('voted')) {
			var value = $(this).attr("id");
			ul_selector.children("li").each (function(){
				$(this).removeClass("active");
				if ( $(this).attr("id") <= value  ){
					$(this).addClass("active");
				}
			})
		}
	});
	
	$('ul.rating').unbind('mouseout').bind('mouseout', function (){
		if (! $(this).hasClass('voted')) {
			var value = $(this).attr("ref");
			$(this).children("li").each (function(){
				$(this).removeClass("active");
				if ( $(this).attr("id") <= value  ){
					$(this).addClass("active");
				}
			})
		}
	});
	
	$('ul.rating > li > a').each(function() {
		$(this).unbind("click").bind("click", function () {
			var data = { value: $(this).attr("ref"), id: $(this).attr("id") } ;
			var ul_selector = $(this).parents("ul");
			var url = "/coupon/values";
			$.ajax({ url: url,
				type: 'post',	
				data: data,
				success: function (response){
					ul_selector.attr('ref', response);
					ul_selector.children("li").each (function(){
						$(this).removeClass("active");
						if ($(this).attr("id") <= response) { $(this).addClass("active"); }
						var link = $(this).find('a').remove();
						$(this).append('<p id="' + link.attr('id') + '" ref="' + link.attr('ref') + '" class="' + $(this).attr('class') + '"></p>');
					});
				},
				beforeSend: function (){
					ul_selector.effect("drop", {}, 100);
				},
				complete: function (request, textStatus){
					ul_selector.effect("pulsate", {}, 200, function(){ 
						// fix IE visual effect
						if(jQuery.browser.msie) $(this).removeAttr("style");
					});
					ul_selector.addClass('voted');
					ul_selector.children('li').children('a').each(function(){ 
						$(this).unbind('click').bind('click', function (){ return false; });
					});
				}
			})
			return false;
		});
	});
	
}



function initStateSelector (){
	$("#top_page_state_selector").unbind("change").bind("change", function (){
		$(this).parent("form").submit();
	});
} 

