jQuery(document).ready(function(){
	jQuery('#addToFavouriteButton').jFav();
});


function newClassifiedPage(route){
	//jQuery("#classified_email_contact").format({type:"email"});  TODO fix email validation
	jQuery("#classified_price").format({precision: 0,autofix:true});
	jQuery("#classified_tel1_phone").format({precision: 0,autofix:true});
	jQuery("#classified_tel2_phone").format({precision: 0,autofix:true});
	jQuery("#classified_tel3_phone").format({precision: 0,autofix:true});
	jQuery("#classified_tel4_phone").format({precision: 0,autofix:true});
	jQuery("#classified_icq_contact").format({precision: 0,autofix:true});

	jQuery("#classified_category_id").change(function() {
			CaterogyIdChange(route);
		}
    );
	
    if (jQuery('#classified_dogovor').attr('checked')) NewPriceDisablePrice();

    jQuery('#classified_dogovor').click(function(){
        if (jQuery('#classified_dogovor').attr('checked')) {
            NewPriceDisablePrice();
        } else {
            NewPriceEnablePrice();
        }
    });
}

function addToNotepad(item_id, siteurl) {
	jQuery('#add_notepad_'+item_id).hide();
	toggleAjaxGif(item_id);
	jQuery.ajax({
	    url: siteurl+'?item_id='+item_id,
	    success: function (data, code) {
			SuccessNotepadAdd(data, item_id);
	    }
	});
}

function removeFromNotepad(item_id, siteurl) {
	jQuery('#remove_notepad_'+item_id).toggle();
	toggleAjaxGif(item_id);
	jQuery.ajax({
	    url: siteurl+'?item_id='+item_id,
	    success: function (data, code) {
			SuccessNotepadRemove(data, item_id);
	    }
	});
}

function toggleNotepad(item_id, siteurl) {
	jQuery.ajax({
	    url: siteurl+'?item_id='+item_id,
	    dataType: 'json',
	    success: function (data, code) {
			if (data['shift']=='-1') { SuccessNotepadRemove(data['count'], item_id); } 
			if (data['shift']=='1') { SuccessNotepadAdd(data['count'], item_id); }
	    }
	});
	
}


function SuccessNotepadAdd(data, item_id) {
	jQuery.jGrowl("Объявление добавлено!");
	toggleAjaxGif(item_id);
	jQuery('#bloknot_num').html('('+data+')');
	//jQuery('#add_notepad_'+item_id).toggle();
	jQuery('#remove_notepad_'+item_id).toggle();
}


function SuccessNotepadRemove(data, item_id) {
	jQuery.jGrowl("Объявление удалено!");
	toggleAjaxGif(item_id);
	jQuery('#bloknot_num').html('('+data+')');
	jQuery('#add_notepad_'+item_id).toggle();
	//jQuery('#remove_notepad_'+item_id).toggle();
}

function hideRemovedAtNotepad(item_tr_id) {
	jQuery("#"+item_tr_id).fadeOut("slow", function(){
		jQuery(this).remove();
		jQuery("#item_table > tbody > tr").each(function(i, n){
			if (i%2==0) { 
				jQuery(n).attr("class", "grey_block");
			} else {
				jQuery(n).removeClass("grey_block");
			}
		});
	});
}

function toggleAjaxGif(item_id){
	jQuery('#ajax_loader_'+item_id).toggle();
}


function AddICQ() {
	jQuery('#icq').show();
	jQuery('#add_icq').hide();
}

function AddAddress() {
	jQuery('#address').show();
	jQuery('#add_address').hide();
}

function AddPhoneNumber() {
	phones=jQuery("#telephones .telephone:hidden");
	jQuery(phones[0]).show();
	if (phones.length==1) jQuery(".add_telephone").hide();
}

function AddFileField() {
	fls=jQuery("#files .in:hidden");
	jQuery(fls[0]).show();
	if (fls.length==1) jQuery(".add_foto").hide();	
}

function NewPriceDisablePrice() {
    jQuery('#classified_price').attr("disabled","disabled");
    jQuery('#classified_currency').attr("disabled","disabled");
    jQuery('#classified_torg').attr("disabled","disabled");
};

function NewPriceEnablePrice() {
    jQuery('#classified_price').removeAttr("disabled");
    jQuery('#classified_currency').removeAttr("disabled");
    jQuery('#classified_torg').removeAttr("disabled"); 
};

function wopen(url, name, w, h) {
	w += 32;
	h += 96;
	var win = window.open(
			url,
			name,
			'width=' + w + ', height=' + h + ', ' +
	'location=no, menubar=no, status=no, toolbar=no, scrollbars=yes');
	win.resizeTo(w, h);
	win.focus();
}

function CaterogyIdChange(siteurl) {
	cat_id = jQuery("#classified_category_id option:selected").attr('value');
	type_id = jQuery("#classified_type_id option:selected").attr('value');
	jQuery.ajax({
	    url: siteurl+'?id='+cat_id,
	    dataType: "html",
	    success: function (data) {
	    	jQuery("#classified_type_id").html(data);
	    	if (type_id!='') {
    	    	options=jQuery("#classified_type_id option");
    	    	jQuery.each(options, function(num,obj) {
    		    	if (jQuery(obj).attr('value')==type_id) {
    			    	jQuery(obj).attr('selected','selected');
    		    	}
    		    });
	    	}
	    }
	});
	jQuery("#classified_type_id").removeAttr("disabled");
}


function addFavorite() {
	window.external.AddFavorite('http://baza.com.ua/', 'Белая Церковь: доска объявлений');
}
