$(function(){
	var self = this;
	var s_cookie = {category:'',ids:''};
	var cookie_name = "mejorprecioar_compare";
	var cookie_domain = (location.href.indexOf('terra.com.ar') > 0) ? ".terra.com.ar" : ".mejorprecio.com.ar";
	var current_page = $('#product-list');
	var current_category = $('#boxProduct').attr('class');
	var current_subcategory = current_page.attr('class');

	if(current_page.size() == 0){
		current_page = $('#product-galery');
		current_subcategory = current_page.attr('class');
	}
	
	this.init = function(){
		self.get_cookie();
		
		$('#resultOffset').change(self.result_offset);
		$('#orderType').change(self.order_type)
		$('#product-visualization li a').click(self.update_page_type);
	};
	
	// método para criar cookie
	this.set_cookie = function(page_type, subcategory){
		var category = ($.trim(subcategory) == "" ? current_subcategory : subcategory);
		var ids = ($.trim(s_cookie.ids) == "" ? "null" : s_cookie.ids);
		var value = page_type +"|"+ category +"|"+ ids;
		var expires = "";
		document.cookie = cookie_name +"="+ escape(value) +''+ expires +"; domain="+ cookie_domain +"; path=/;";
		
		// leio o cookie novamente
		self.get_cookie();
	};
	
	// método para pegar um cookie específico
	this.get_cookie = function(){
		var start = document.cookie.indexOf(cookie_name +"=");
		var len = start + cookie_name.length + 1;
		
		if(!start && cookie_name != document.cookie.substring(0, cookie_name.length)){
			return
		}
		
		if(start == -1){
			return;
		}
		var end = document.cookie.indexOf( ";", len );
		
		if(end == -1){
			end = document.cookie.length;
		}
		
		var cookie_data = unescape(document.cookie.substring(len,end)).split('|');
		
		if(cookie_data.length >= 3 && $.trim(cookie_data[0]) != "" && $.trim(cookie_data[0]) != "undefined"){
			s_cookie = {
				page_type: cookie_data[0],
				category: cookie_data[1],
				ids: cookie_data[2]
			};
		}else{
			s_cookie = {
				page_type: "product-galery",
				category: cookie_data[0],
				ids: cookie_data[1]
			};
		}
	};
	
	// método para deletar cookie
	this.erase_cookie = function(){
		document.cookie = cookie_name +"=; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain="+ cookie_domain +"; path=/;";
	};
	
	// método que checa se o a categoria atual é mesma categoria do cookie
	this.check_current_cookie = function(){
		if(s_cookie && s_cookie.category == current_subcategory){
			return true;
		}else{
			return false;
		}
	};
	
	// ordenação
	this.order_type = function(){
		var ord = $(this).val();
		location.href = ord;
	};
	
	// ordenação
	this.result_offset = function(){
		var param = "num="+ $(this).val();
		
		if(!param) return;
		
		if(location.href.match(/num=[^&]+/gi)){
			location.href = location.href.replace(/&?start=[0-9]{1,}/,'').replace(/num=[^&]+/gi,param);
		}else {
			var href = location.href.replace(/&?start=[0-9]{1,}/,'');
			location.href = href + ((href.indexOf('?') > -1) ? (href[href.length - 1] == '?' ?  '' : '&') : '?') + param;
		}
	};
	
	// pega o tipo da página (galeria|lista)
	this.get_page_type = function(){
		return $('#storeFront').attr('class');
	};
	
	// atualiza o tipo da página no cookie
	this.update_page_type = function(e){
		e.stopPropagation(); e.preventDefault();
		
		var type = $(this).attr('href').replace(/.*v=([^&]+).*/,"$1");
		if(type == "lista"){
			page_type = "product-list";
		}else if(type == "galeria"){
			page_type = "product-galery";
		}
		
		var category = "";
		if(s_cookie.category != ""){
			category = s_cookie.category;
		}
		
		self.set_cookie(page_type, category);
		
		location.href = $(this).attr('href');
	};
	
	this.init();
});

// erro nas imagens
function image_error(img){
	$(img).parents('a.image');
	
	var new_url = $(img).parents('a.image').next('h3').find('a').attr('href');
	$(img).parents('a.image').attr('href',new_url);
	
	var cDepto = $('#boxProduct').attr('class') || $(img).parents('p, li').attr('class').match(/^([a-z\-]{1,})/)[1];
	var imageNotFoundUrl = "http://img.mejorprecio.com.ar/no_disponible/" + cDepto + "-no-disponible90.gif";
	
    img.src = imageNotFoundUrl;
	
	// atribui ações aos links de imagem
	$('.image:not(.error)').mouseover(self.addzoom);
	
	$('.image span.zoom').remove();
	
    return true;
}
	





























