/*
$(document).ready(function(){
	$('#mainPageSearchForm').submit(function(){
        $('.mainSearchCrit').each(function(){
           if($(this).val() == 'bez min' || $(this).val() == 'bez max' || $(this).val() == 'no min' || $(this).val() == 'no max'){
                $(this).val('');
           }
        });
    });

    $('.indexLastAdded').each(function(){
        $(this).click(function(){
            var Id = $(this).attr('rel');

            indexLastAddedClick(Id);
        });
    });
    
    $('#mainSearchButton').mousedown(function(){
//		var SrcBack = $(this).attr("rel");
//		$(this).attr('src', SrcBack+'_on.gif');
		var src = $(this).attr('src');
		if (src.indexOf('_on.gif') == -1)
			$(this).attr('src', src.substring(0, src.length-4) + '_on.gif');
	});
	
	// city swap
    $("#cityPart").each(function () {

    	cityValue = $(this).val();
    	$(this).focus(function(){
      		if ($(this).val() == cityValue) {
      		$(this).val("");
      		$(this).css({color:'#757575'});
      		}
      	$(this).addClass("focus");
		}).blur(function(){
      		if ($.trim($(this).val()) == "") {
      		$(this).val(cityValue);
			$(this).css({color:'#000000'});
      		$(this).removeClass("focus");
     	 	}
		});
    });
    
    // uwaga wazne lazy load dla obrazkow    
    $(".lazyload").lazyload({ placeholder : layoutUrl+"themes/main/images/loader_offer.gif" });

    
    provideMinMaxInput();

    manageSearchForm();
});
*/

//darek: zliczanie klikniec w obiekty
/*function addObjectClick(object_id) {
		
		$.ajax({
			  type: "POST",
			  url: ajaxUrl + 'json/index/add-object-click/object_id/' + object_id  + '/',
			  data: {
			  },
			  success: function(result) {
                			
			  }
   			});
			return false;		
		
}
*/
function provideMinMaxInput(){

   // min-max numbers only
   $(".mainSearchCrit").keyup(function () {
    	this.value = this.value.replace(/[^0-9\.]/g,'');
   });

   // min-max swap
   swapValue = [];
   $(".mainSearchCrit").each(function(i){

        swapValue[i] = $(this).val();
        var ClassName = '';
        var Type = $(this).attr('rel');
        if(Type == 'min'){
            ClassName = 'searchListCriteriaInputMin';
        }else if($(this).attr('rel') == 'max'){
            ClassName = 'searchListCriteriaInputMax';
        }

      	$(this).focus(function(){
            $(this).removeClass(ClassName);
	});
        $(this).blur(function(){
            if ($.trim($(this).val()) == "" && (Type == 'min' || Type == 'max')) {
                $(this).addClass(ClassName);
            }
  	});
   });


}

function indexLastAddedClick(Id){
    $('.indexLastAddedOffersMoreRow_'+Id).hide();
    $('.indexOffersLastAddedItem_'+Id).each(function(){
        $(this).show();
    });
}

function  mainSearchSelectType(Selected){
    $('.mainSearchTypeBtn').each(function(){
        Selected.attr('style', 'font-weight: bold');
        $('#mainPageIdOrderType').val(Selected.attr('rel'));
        if(Selected.attr('rel') != $(this).attr('rel')){
            $(this).attr('style', 'font-weight: normal');
        }
    });
}

function showPopup(Message){
    if($('#mainPopupArea').is(':hidden')){
        $('#mainPopupAreaInside').html(Message);
        $('#mainPopupArea').show();
        setTimeout("$('#mainPopupArea').hide();", 30000);
    }
}

function insertOpenX(idZone){
    return true;
   var content = '';
   var src = 'http://r.domoklik.pl/delivery/ajs1.php';
   var rand = Math.floor(Math.random()*99999999999);
   if (!document.MAX_used) document.MAX_used = ',';
   var content1 = src+'?zoneid='+idZone+'&openxIdCity='+openxIdCity+'&openxMainBanner='+openxMainBanner+'&userlocation='+userlocation+'&lang='+lang+'&cb='+rand;
   if (document.MAX_used != ','){
       content1 += '&exclude="' + document.MAX_used;
   }
   content1 += '&charset=UTF-8';
   content1 += '&loc=' + escape(window.location);
   
   if (document.referrer){
       content1 += '&referer=' + escape(document.referrer);
   }
   if (document.context){
      content1 += '&context=' + escape(document.context);
   }
   if (document.mmm_fo){
       content1 +='&mmm_fo=1';
   }
   //content1 += '"></script>';
   var content2 ='';
   var Obj = document.getElementById('openXArea_'+idZone);
   var script = document.createElement('script');
   script.type= 'text/javascript';
   script.src = content1;
   Obj.appendChild(script);
}


function manageSearchForm(){
    $('input', '.mainSearchTypeArea').click(function(){
        var Input = $(this);
        var Value = $(Input).attr('value');
        switch(Value){
            case '1':
            case '5':
                searchFormSecondaryMode();
            break;
            case 'primary':
                searchFormPrimaryMode();
            break;
        }
    });
}

function searchFormSecondaryMode(){
if (typeof(debugmode) != 'undefined')
	console.log('a');
	document.getElementById('msearchForm_1').action = $('input[name="ppage"]').val();
	document.getElementById('msearchForm_2').action = $('input[name="ppage"]').val();
	var Select = $('select[name="idRealEstateType"]');

	var selectedValue = Select.val();
	$('option', Select).each(function(i, Opt){
		$(Opt).remove();
	});
	for (var i in idRealEstateTypeOptions)
		if (idRealEstateTypeOptions[i]['val'] != 0)
			Select.append($("<option></option>").attr("value",idRealEstateTypeOptions[i]['val']).text(idRealEstateTypeOptions[i]['text']).attr("selected", false));
	$('option', Select).each(function(i, Opt){
		if($(Opt).val() == selectedValue)
			$(Opt).attr('selected', 'selected');
	});
}

function searchFormPrimaryMode(Obj){
	document.getElementById('msearchForm_1').action = $('input[name="ppage"]').val();
	document.getElementById('msearchForm_2').action = $('input[name="ppage"]').val();
	var Select = $('select[name="idRealEstateType"]');
	var selectedValue = Select.val();
	$('option', Select).each(function(i, Opt){
		$(Opt).remove();
	});
	for (var i in idRealEstateTypeOptions)
		if (idRealEstateTypeOptions[i]['val'] == 1 || idRealEstateTypeOptions[i]['val'] == 2 || idRealEstateTypeOptions[i]['val'] == 0)
			Select.append($("<option></option>").attr("value",idRealEstateTypeOptions[i]['val']).text(idRealEstateTypeOptions[i]['text'])); 
	$('option', Select).each(function(i, Opt){
		if($(Opt).val() == selectedValue)
			$(Opt).attr('selected', 'selected');
	});
}

