
var Subscribe = {
	IsValidEmail: function(val){
			validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
			if (val.search(validRegExp) == -1){
				return false;
			} else {
				return true;
			}
	},
	setError: function(error){
		$('#subs-error').html('<span style="color:#ff0000;">'+error+'</span>');
		this.earthquake(1, 8);
	},	
	earthquake: function(direction, balls){
		if ( balls>0 ){
			direction = -1 * direction; 
			$('#news-subscription').animate({marginTop:balls*direction, marginLeft:Math.floor(2*Math.random() - 1)*balls},
					50,
					function(){Subscribe.earthquake(direction, balls-1);}
					);
		} else {
			$('#news-subscription').css({marginTop:0,marginLeft:0});	
		}	
	},	
	Go: function(type, id){
 		var email = $('#subs-mail').val();
		if (!this.IsValidEmail(email)){
			this.setError('Не корректный email');
		} else {
          
                        if (type == 'send'){
                            postdata = 'email=' + email+'&sendtype=send&item_id='+id;
                        } else {
                            postdata = 'email='+email+'&sendtype=subscribe';
                        }
			$.ajax({
				   type: "POST",
				   url: "/subscribe/",
                                   dataType:"json",
				   data: postdata,
				   success: function(msg){
					if  (msg.result){
						$('#news-subscription').html("<span>"+msg.msg+"<span>")
					} else {
						Subscribe.setError(msg.error);
					}
				   },
                                   error:function(x,e,s){alert(s);}
				 });
		}
	},	
	Show: function(){
		$('#form_subcribe').show('slow');	
	}

}


Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++)	{
		if(this[i] == p_val) {
			return i;
		}
	}
	return -1;
}

Array.prototype.remove = function(from, to) {
	  var rest = this.slice((to || from) + 1 || this.length);
	  this.length = from < 0 ? this.length + from : from;
	  return this.push.apply(this, rest);
};


var CatSearch = {
		brends : Array(),
		types : Array(),
		page :0,
		getItems: function(){
                        $('#catalog-items').html('<p align="center">Загрузка...</p>');
			$('#catalog-items').load('/catalog/index/format/html',{"brend":this.brends,"type":this.types,"page":this.page});
		},
		toggleBrand: function(brand_id){
			var pos = this.brends.in_array(brand_id);
			if ( pos > -1){
				$('#brand'+brand_id).removeClass('selected');
				this.brends.remove(pos);				
			} else {
				$('#brand'+brand_id).addClass('selected');
				this.brends.push(brand_id);
			}
			this.page = 0;
			this.getItems();
		},
		toggleType: function(type_id){
			var pos = this.types.in_array(type_id);
			if ( pos > -1){
				$('#type'+type_id).removeClass('selected');
				this.types.remove(pos);				
			} else {
				$('#type'+type_id).addClass('selected');
				this.types.push(type_id);
			}
			this.page = 0;
			this.getItems();
		},
		paginator:function(pg){
			this.page=pg;
			this.getItems();
		}
}

var PresentfbSearch = {
		tags : Array(),
		page :0,
		getItems: function(){
                        $('#catalog-items').html('<p align="center">Загрузка...</p>');
			$('#catalog-items').load('/catalog/presentfb/format/html',
                                                 {"tag":this.tags,"page":this.page},
                                                 function(){$('#catalog-items').show();}
                                              );
		},
		toggleTag: function(tag_id){
			var pos = this.tags.in_array(tag_id);
			if ( pos > -1){
				$('#tag'+tag_id).removeClass('selected');
				this.tags.remove(pos);				
			} else {
				$('#tag'+tag_id).addClass('selected');
				this.tags.push(tag_id);
			}
			this.page = 0;
			this.getItems();
		},	
		paginator:function(pg){
			this.page=pg;			
			this.getItems();
		}
}

var PresentSearch = {
		tags : Array(),
		page :0,
		getItems: function(){
                        $('#catalog-items').html('<p align="center">Загрузка...</p>');
			$('#catalog-items').load('/catalog/present/format/html',
                                                 {"tag":this.tags,"page":this.page},
                                                 function(){$('#catalog-items').show();}
                                              );
		},
		toggleTag: function(tag_id){
			var pos = this.tags.in_array(tag_id);
			if ( pos > -1){
				$('#tag'+tag_id).removeClass('selected');
				this.tags.remove(pos);				
			} else {
				$('#tag'+tag_id).addClass('selected');
				this.tags.push(tag_id);
			}
			this.page = 0;
			this.getItems();
		},	
		paginator:function(pg){
			this.page=pg;			
			this.getItems();
		}
}

function hover_image_load(img){
    var $i = $(img);
    return function(){
           if ($i.width()>$i.height())
                $i.css({width:250})
           else
		$i.css({height:250});
           $i.mouseenter(function(){
               $(this).stop();
               if ($(this).width()>$(this).height())
                   $(this).animate({width:420,marginTop:0},100);
               else
                   $(this).animate({height:420,marginTop:0},100);
	   });
           $i.mouseout(function(){
              $(this).stop();
              if ($(this).width()>$(this).height())
                  $(this).animate({width:250,marginTop:35},400);
              else
                  $(this).animate({height:250,marginTop:35},400);
           });
    }
}



$(document).ready(function(){
        $('#subs-mail').focus( function()
        {if(this.value == 'введите email'){this.value='';}});

        $('#subs-mail').blur( function()
        {if(this.value == ''){this.value='введите email';}});

        // принудительно в любом случае провоцируем событие onload
        $('.hover_image').each(function(row, el){
            var src = $(el).attr('src');
            $(el).load(hover_image_load(el));
            $(el).attr('src',src+'#'+Math.random(1,1000));
        });
        if ($('#trava-carusel').TravaCarusel){
            $('#trava-carusel').TravaCarusel();
        }
});

	

