// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function _log(x){ if (typeof(console)!='undefined') console.log(x); else { /*$('body').append($('<div></div>').text(x));*/ } }

// type: ok, error
// message: html
//timeout: milisec

function flash_message(type, message, timeout){
	$('#flash-message').html('<div class="flash-message flash-message-' + type + '"><span class="icon"></span><span class="message">' + message + '</span></div>');
	$('#flash-message').effect('highlight', { color: (type=='error') ? '#ff0000' : '#00ff00' }, 3000).unbind('click').bind('click', function(){ $('#flash-message').hide(); return false; });
	//$('#flash-message, #flash-message a').effect( 'highlight', { color: ($('#flash-message .flash-message-error').length) ? '#817F84' : '#817F84' }, 2000);
	$('#flash-message .message').effect( 'pulsate', { times:2 }, 1000);
	setTimeout(function() { $('#flash-message').fadeOut(); }, timeout);
}

function ie(version_min,version_max){
		return (jQuery.browser.msie && (parseInt(jQuery.browser.version)>=version_min) && (parseInt(jQuery.browser.version)<=version_max));
}

function promotion_dialog(options){
	$.ajax({
		url: options.url,
		method:'GET',
		success:function(html){
			var dialog = $('<div></div>').appendTo('body');
			dialog.dialog($.extend({ 
				modal:true, 
				width:'auto', 
				dialogClass:'dialog-promotion', 
				resizable:false, 
				position:['center', 40],
				open: function(){ 
					$('.ui-widget-overlay').unbind('click.dialog').bind('click.dialog', function(){ $(dialog).dialog('close'); }) } }, options.dialog))
				.dialog('open')
				.html(html)
				.find('.dialog-js-close')
				.bind('click', function(){ $(dialog).dialog('close'); return false; });
		}
	});
};

$(function(){
	if (ie(0,6)){
		promotion_dialog({ url:'ie6.html', dialog: { width:400 } });
	}
});

function deal_external_url(selector){
	$('a.deal-js-external_url', selector).each(function(){
		var reg = /.*#(.*)/.exec($(this).attr('href'));
		if (reg!=null) $(this).attr('href', 'http://' + reg[1]);
	});
}

//============== OLD =================


;(function($) {
   $.fn.category_search = function(text_search, select_all, deselect_all) {
        var form = $('.categories .search_form')
        $(select_all).bind('click', function(e) {
          form.find(".category input[type='checkbox']").attr('checked', true);
          form.submit();
        });
        $(deselect_all).bind('click', function(e) {
          form.find(".category input[type='checkbox']").attr('checked', false);
          form.submit();
        });
        $(this).bind('submit', function(e) {
          var text_search_form = $(text_search);
          var params = form.serializeArray();
          params = params.concat(text_search_form.serializeArray());
          var refresh_filter = $(e.target).hasClass('local_deals');
          if(refresh_filter)
            params.push({name: 'search[refresh_filter]', value: 1})
          var url = form.attr('action')
          $.get(url, params, function(data) {
            $('.top-offers').html(data.deals);
            if(data.filter)
              $('.categories_filter').html(data.filter);
          }, 'json');
          return false;
        });
        $(this).find("input[type='checkbox']").bind('click', function() { form.submit(); });
      }; 
 })(jQuery);

;(function($) {
  $.fn.text_search = function() {
    $(this).bind('submit', function(e) {
        var url = $(this).attr('action')
        var data = $(this).serializeArray();
        text_search_start(url, data);
        return false;
    });
  };
})(jQuery);
  
function text_search_start(url, data) {
     $.get(url, data, function(data) {  
        $('.top-offers').html(data.deals);
        if(data.filter)
          $('.categories_filter').html(data.filter);
      }, 'json');
};

;(function($) {
  $.fn.city_select = function() {
    $(this).unbind().bind('change', function(e) {
      window.location.href = $(e.target).val();
    });
  };
})(jQuery);


;(function($) {
  $.fn.deal_dialog = function(dialog_selector) {
    var self = $(this);
    $(this).bind('mouseover', function(e) {
      $(this).find(dialog_selector).show();
    });
    $(this).bind('mouseout', function(e) {
      $(this).find(dialog_selector).hide();
    });
  };
})(jQuery);


