$(document).ready(function(){
	var mode = 0;
	var price1 = 0;
	var price2 = 0;
	var sort = 0;
	var side = 0;
	var category = 0;
	var location = 422;
	var city = 0;
	var since = 0;
	var q = "";
	var page = 1;
		
	if(jQuery.url.param("mode")){
		mode = jQuery.url.param("mode");
	}
	if(jQuery.url.param("price1")){
		price1 = jQuery.url.param("price1");
	}
	if(jQuery.url.param("price2")){
		price2 = jQuery.url.param("price2");
	}
	if(jQuery.url.param("sort")){
		sort = jQuery.url.param("sort");
	}
	if(jQuery.url.param("side")){
		side = jQuery.url.param("side");
	}
	if(jQuery.url.param("category")){
		category = jQuery.url.param("category");
	}
	if(jQuery.url.param("location")){
		location = jQuery.url.param("location");
	}
	if(jQuery.url.param("city")){
		city = jQuery.url.param("city");
	}
	if(jQuery.url.param("since")){
		since = jQuery.url.param("since");
	}
	if(jQuery.url.param("page")){
		page = jQuery.url.param("page");
	}

	q = $('#txtSearch').val();
	category = $('#optCategoryId').val();
	city = $('#hidCityId').val();
	
	setEventsPageSearch(mode);
	setEventsPageSearchDynamic(mode,price1,price2,sort,side,category,location,city,since,q,page);
	/*
	*/
	
});

function verifyArticleStatus(uid){
	$.ajax({
		url: 'http://goonzi.com/admin/bridges/inc/verifyStatus.php',
		async: true,
		type: 'POST',
		data: ({uid:uid}),
		success: function(data){
			status = data;
			if(status!=1){ //STATUS_ONLINE
				$('#status-'+uid).empty();
				$('#status-'+uid).removeClass();
				$('#status-'+uid).addClass('articleStatusSold');
				$('#status-'+uid).html('Vendue');
			}
		}
	});
}
function setEventsPageSearchDynamic(mode,price1,price2,sort,side,category,location,city,since,q,page){
	
	$.ajax({
		url: 'http://goonzi.com/ajax/search/loadMenuGauche.php',
		async: true,
		type: 'POST',
		data: ({
			mode:mode,
			price1:price1,
			price2:price2,
			sort:sort,
			side:side,
			category:category,
			location:location,
			city:city,
			since:since,
			q:q,
			page:page
		}),
		success: function(data){
			
			var tabData = data.split("###H###");
			
			$('#filterElementsCity').html(tabData[0]);
			$('#filterElementsCity').fadeIn("slow");
			
			$('#filterElementsCategory').html(tabData[1]);
			$('#filterElementsCategory').fadeIn("slow");
			
			$('.filterMoreElements').click(function(){
				var infos = $(this).attr('id').split('-');
				$('#dialogMoreElements-'+infos[1]).dialog({
					resizable: false,
					draggable: false,
					autoOpen: true,
					height:400,
					width:700,
					modal: true,
					close: function(ev, ui) {
						$(this).dialog("destroy");
					},
					overlay: {
						backgroundColor: '#000',
						opacity: 0.5
					}
				});
				return false;
			});
			
		},error:function(e,f,g){
			// alert(e);
			// alert(f);
			// alert(g);
		}
	});
	/*
	$.ajax({
		url: 'http://www.goonzi.com/ajax/search/loadMenuGauche.php',
		async: true,
		type: 'POST',
		data: ({
			mode:mode,
			price1:price1,
			price2:price2,
			sort:sort,
			side:side,
			category:category,
			location:location,
			city:city,
			since:since,
			q:q,
			page:page
		}),
		success: function(data){
			
			var tabData = data.split("###H###");
			
			$('#filterElementsCity').html(tabData[0]);
			$('#filterElementsCity').fadeIn("slow");
			
			$('#filterElementsCategory').html(tabData[1]);
			$('#filterElementsCategory').fadeIn("slow");
			
			$('.filterMoreElements').click(function(){
				var infos = $(this).attr('id').split('-');
				$('#dialogMoreElements-'+infos[1]).dialog({
					resizable: false,
					draggable: false,
					autoOpen: true,
					height:400,
					width:700,
					modal: true,
					close: function(ev, ui) {
						$(this).dialog("destroy");
					},
					overlay: {
						backgroundColor: '#000',
						opacity: 0.5
					}
				});
				return false;
			});
			
			$('#cmd_test2').click(function(){
				alert(data);
			});
			
		},error:function(e,f,g){
			// alert(e);
			// alert(f);
			// alert(g);
		}
	});
	*/
}

function setEventsPageSearch(mode){
	if(mode == 0 || mode == 2 || mode == 3){
		
		// $('.articleDetailContainer').each(function(){
			// var id = $(this).attr('id');
			// tmp = id.split('-');
			// verifyArticleStatus(tmp[1]);
		// });
		
		
		$('.addFavorite').click(function(){
			addFavorite($(this));
		});

		$('.removeFavorite').click(function(){
			removeFavorite($(this));
		});
		
		$('.addComparator').click(function(){
			addComparator($(this));
		});
		
		$('.removeComparator').click(function(){
			removeComparator($(this));
		});
		
		$('.printThisArticle').click(function(){
			printThisArticle($(this));
		});
		
		$('.sendToFriend').click(function(){
			sendToFriend($(this));
		});
	}
	else if(mode == 1){
		//Load map
		if($('#map').length > 0){
			load();
		}
	}
	
	$('#linkViewComparator').click(function(){
		document.location.href = PAGE_COMPARE;
		return false;
	});
	
	$('#linkEmptyComparator').click(function(){
		clearComparator();
		return false;
	});
	
	$(".filter").toggle(function(){
		$(this).removeClass("active"); 
	}, function () {
		$(this).addClass("active");
	});
	
	$(".filter").click(function(){
		$(this).next(".filterElementsContainer").slideToggle("slow");
	});

	function formatKeyword(row){
		return row.keyword;
	}
	
	$("#txtSearch").autocomplete('http://www.goonzi.com/ajax/search/loadListKeywords.php',{
		dataType: "json",
		cacheLength: 0,
		async: true,
		extraParams: {
			categoryId:function(){
				return $("#optCategoryId").attr('value');
			}
		},
		parse: function(data){
			return $.map(data, function(row){
				return {
					data: row,
					value: row.keyword,
					result: row.keyword
				}
			});
		},
		formatItem: function(item){
			return formatKeyword(item);
		}
	});
	
	function formatLocation(row) {
		return row.name;
	}
	// $('#txtLocation').focus(function(){
	// });
	
	 // $.ajax({
		// url: 'http://www.goonzi.com/ajax/search/loadListCities.php',
		// async: true,
		// type: 'GET',
		// data:({q:"Vic"}),
		// success: function(data){
			// alert("Reu");
			// status = data;
			// if(status!=1){ //STATUS_ONLINE
				// $('#status-'+uid).empty();
				// $('#status-'+uid).removeClass();
				// $('#status-'+uid).addClass('articleStatusSold');
				// $('#status-'+uid).html('Vendue');
			// }
		// }
	// });
	
	
	
	$("#txtLocation").focus().autocomplete('ajax/search/loadListCities.php',{
		dataType: "json",
		cacheLength: 5,
		async: true,
		minChars: 3,
		parse: function(data){
			return $.map(data, function(row){
				return {
					data: row,
					value: row.name,
					result: row.name
				}
			});
		},
		formatItem: function(item){
			return formatLocation(item);
		}
	});
	
	$("#txtLocation").result(findLocationCallback).next().click(function(){
		$(this).prev().search();
	});
	
	$('#txtSearch').keypress(function(e){
		var code;
		if(!e){
			var e = window.event;
		}
		if(e.keyCode){
			code = e.keyCode;
		}
		else if(e.which){
			code = e.which;
		}
		if(code==13){
			$('form#search').submit();
		}
	});
	
	$('#btnFilterPrice').click(function(){
		filterPrice();
	});
	
	$('#txtPrice1, #txtPrice2').keypress(function(e){
		var code;
		if(!e){
			var e = window.event;
		}
		if(e.keyCode){
			code = e.keyCode;
		}
		else if(e.which){
			code = e.which;
		}
		if(code==13){
			filterPrice();
		}
		else{
			$('#filterButtonPrice').show();
		}
	});
	
	$('.btnRangeFilter').click(function(){
		var infos = $(this).attr('id').split('-');
		filter(infos[1]);
	});
	
	$('.rangeFilterTextbox').keypress(function(e){
		var infos = $(this).attr('id').split('-');
		var code;
		if(!e){
			var e = window.event;
		}
		if(e.keyCode){
			code = e.keyCode;
		}
		else if(e.which){
			code = e.which;
		}
		if(code==13){
			filter(infos[1]);
		}
		else{
			$('#filterButton-'+infos[1]).show();
		}
	});
	
	//SCROLLING
	/* if($.cookie('goonzy_last_article')){
		$.scrollTo($('#articleContainer-'+$.cookie('goonzy_last_article')),500);
		$.cookie("goonzy_last_article", null);
	} */
}

function findLocationCallback(event, data, formatted){
	$("#txtLocationId").attr('value',data.uid);
}

function filterPrice(){
	var price1 = $("#txtPrice1").attr('value');
	var price2 = $("#txtPrice2").attr('value');
	
	$('#errorInvalidPrice').hide();
	if((price1 == '' || price1 != '' && isFloat(price1)) && (price2 == '' || price2 != '' && isFloat(price2))){
		if(price1 != '' && price2 != '' && parseFloat(price1) > parseFloat(price2)){
			$("#txtPrice1").attr('value',price2);
			$("#txtPrice2").attr('value',price1);
			price1 = $("#txtPrice1").attr('value');
			price2 = $("#txtPrice2").attr('value');
		}
		
		var currentPrice1 = '';
		if(jQuery.url.param("price1")){
			currentPrice1 = jQuery.url.param("price1");
		}
		
		var currentPrice2 = '';
		if(jQuery.url.param("price2")){
			currentPrice2 = jQuery.url.param("price2");
		}

		var currentQuery = jQuery.url.attr("query");
		currentQuery = currentQuery.replace("&price1="+currentPrice1,"");
		currentQuery = currentQuery.replace("&price2="+currentPrice2,"");
		
		if(currentQuery != ''){
			currentQuery = '?'+currentQuery+'&price1='+price1+'&price2='+price2;
		}
		else{
			currentQuery = '?price1='+price1+'&price2='+price2;
		}

		document.location.href = PAGE_SEARCH+currentQuery;
	}
	else{
		$('#errorInvalidPrice').show();
	}
}

function filter(attributeId){
	var minRange = $("#txtAttr-"+attributeId+"-min").attr('value');
	var maxRange = $("#txtAttr-"+attributeId+"-max").attr('value');
	
	$('#errorInvalidRange-'+attributeId).hide();
	if((minRange == '' || minRange != '' && isFloat(minRange)) && (maxRange == '' || maxRange != '' && isFloat(maxRange))){
		if(minRange != '' && maxRange != '' && minRange > maxRange){
			$("#txtAttr-"+attributeId+"-min").attr('value',maxRange);
			$("#txtAttr-"+attributeId+"-max").attr('value',minRange);
			minRange = $("#txtAttr-"+attributeId+"-min").attr('value');
			maxRange = $("#txtAttr-"+attributeId+"-max").attr('value');
		}
		
		var currentMin = '';
		if(jQuery.url.param("attr-"+attributeId+"-min")){
			currentMin = jQuery.url.param("attr-"+attributeId+"-min");
		}
		
		var currentMax = '';
		if(jQuery.url.param("attr-"+attributeId+"-max")){
			currentMax = jQuery.url.param("attr-"+attributeId+"-max");
		}
		
		var currentQuery = jQuery.url.attr("query")
		currentQuery = currentQuery.replace("&attr-"+attributeId+"-min="+currentMin,"");
		currentQuery = currentQuery.replace("&attr-"+attributeId+"-max="+currentMax,"");
		
		if(currentQuery != ''){
			currentQuery = '?'+currentQuery+'&attr-'+attributeId+'-min='+minRange+'&attr-'+attributeId+'-max='+maxRange;
		}
		else{
			currentQuery = '?attr-'+attributeId+'-min='+minRange+'&attr-'+attributeId+'-max='+maxRange;
		}

		document.location.href = PAGE_SEARCH+currentQuery;
	}
	else{
		$('#errorInvalidRange-'+attributeId).show();
	}
}

var intervalPhotos = 0;
var oImgOver;
function artDetailImg(){
	$(".articleDetailImage").each( function(){	
		$(this).children("a").css("display","none");
		$(this).children("a:first").css("display","block");
		$(this).children("a").mouseover( function (){
			oImgOver = $(this);
			intervalPhotos = setInterval("switchPhotos()",1000);
		});
		$(this).mouseout( function (){
			clearInterval(intervalPhotos);
		});
	});
	
}
function artPhotoImg(){
	$(".articlePhotoImage").each( function(){	
		$(this).children("a").css("display","none");
		$(this).children("a:first").css("display","block");
		$(this).children("a").mouseover( function (){
			oImgOver = $(this);
			intervalPhotos = setInterval("switchPhotosImage()",1000);
		});
		$(this).mouseout( function (){
			clearInterval(intervalPhotos);
		});
	});
}
function artPhotoAbr(){
	$(".articleShortImage").each( function(){	
		$(this).children("a").css("display","none");
		$(this).children("a:first").css("display","block");
		$(this).children("a").mouseover( function (){
			oImgOver = $(this);
			intervalPhotos = setInterval("switchPhotosImageAbr()",1000);
		});
		$(this).mouseout( function (){
			clearInterval(intervalPhotos);
		});
	});
}
function switchPhotos(){
	oImgOver.css("display","none");
	if(oImgOver.next().attr("href") != undefined){
		oImgOver = oImgOver.next();
	}else{
		oImgOver = oImgOver.parent(".articleDetailImage").children("a:first");
	}
	oImgOver.css("display","block"); 
}
function switchPhotosImage(){
	oImgOver.css("display","none");
	if(oImgOver.next().attr("href") != undefined){
		oImgOver = oImgOver.next();
	}else{
		oImgOver = oImgOver.parent(".articlePhotoImage").children("a:first");
	}
	oImgOver.css("display","block"); 
}
function switchPhotosImageAbr(){
	oImgOver.css("display","none");
	if(oImgOver.next().attr("href") != undefined){
		oImgOver = oImgOver.next();
	}else{
		oImgOver = oImgOver.parent(".articleShortImage").children("a:first");
	}
	oImgOver.css("display","block"); 
}
$(document).ready(function (){
	artDetailImg();
	artPhotoImg();
	artPhotoAbr();
});
