$(function(){
	$(".view_all").hover(function(){
		$(".view_all ul").toggle();
	});
	
	$(".to_top").click(function(){
		$("html, body").animate({scrollTop:0}, 'slow');
		return false;
	});
	
	$('.default_open').removeClass('default_open').addClass('close').parent().parent().parent().find('.hidden').show();
	
	if($('.show_form').hasClass('close')){
		$(".show_form").toggle(function(){
			$(this).removeClass("close");
			$(this).addClass("open");
			$(this).parents(".expandable").find(".hidden").animate({ height: 'hide', opacity: 'hide' }, 'slow');
			/*setTimeout(function() {
				$(".busy").show();
			}, 500);*/
			},function(){
			$(this).removeClass("open");
			$(this).addClass("close");
			$(this).parents(".expandable").find(".hidden").animate({ height: 'show', opacity: 'show' }, 'slow');
			/*setTimeout(function() {
				$(".busy").fadeOut();
			}, 5000);*/
		});
	} else {
		$(".show_form").toggle(function(){
			$(this).removeClass("open");
			$(this).addClass("close");
			$(this).parents(".expandable").find(".hidden").animate({ height: 'show', opacity: 'show' }, 'slow');
			/*setTimeout(function() {
				$(".busy").fadeOut();
			}, 5000);*/
			},function(){
			$(this).removeClass("close");
			$(this).addClass("open");
			$(this).parents(".expandable").find(".hidden").animate({ height: 'hide', opacity: 'hide' }, 'slow');
			/*setTimeout(function() {
				$(".busy").show();
			}, 500);*/
		});
	}
	
	
	//$(".radio").dgStyle();
	//$(".checkbox").dgStyle();
	//$('.selectbox').selectbox();
	
	//////////////////////////
	//tabb nav / form loops//
	/////////////////////////
	
	//login
	$('.forgot_password').blur(function(){
		$('#str_login').focus();
	});
	$('.login_btn').hover(function(){
		$(this).css({backgroundPosition: '0 -39px'});
	},function(){
		$(this).css({backgroundPosition: '0 0'});
	});
	$('.login_btn').focus(function(){
		$(this).css({backgroundPosition: '0 -39px'});
	});
	$('.login_btn').blur(function(){
		$(this).css({backgroundPosition: '0 0'});
	});
	
	//home_search
	$('.show_advanced_search').blur(function(){
		$('#search').focus();
	});
	$('.search_all_btn').hover(function(){
		$(this).css({backgroundPosition: '0 -45px'});
	},function(){
		$(this).css({backgroundPosition: '0 0'});
	});
	$('.search_all_btn').focus(function(){
		$(this).css({backgroundPosition: '0 -45px'});
	});
	$('.search_all_btn').blur(function(){
		$(this).css({backgroundPosition: '0 0'});
	});
	
	//advanced search, side_search, add ad, edit ad, user register
	$('.advanced_search_btn').blur(function(){
		$('#str_search_string').focus();
	});
	$('.side_search_btn').blur(function(){
		$('#keyword_search').focus();
	});
	$('.add_ad_btn,.edit_ad_btn').blur(function(){
		$('#ad_type_pon').focus();
	});
	$('.register_btn').blur(function(){
		$('#u_login').focus();
	});
	$('.register_fbtn,.register_pbtn').blur(function(){
		$('#str_phone1').focus();
	});
	$('.search_btn').hover(function(){
		$(this).css({backgroundPosition: '0 -40px'});
	},function(){
		$(this).css({backgroundPosition: '0 0'});
	});
	$('.search_btn').focus(function(){
		$(this).css({backgroundPosition: '0 -40px'});
	});
	$('.search_btn').blur(function(){
		$(this).css({backgroundPosition: '0 0'});
	});
	
	///////////////////////////////////
	///////////////////////////////////
	
	$(".upload_photo").filestyle({
		image: imagesPath + "browse_btn.gif",
		imageheight : 33,
		imagewidth : 125,
		width : 310
	});
	
	//Configure MIN i MAX values for slider component
	sliderData['max'] = 1001;
	sliderData['min'] = 0;
	if(!sliderData['high']){
			sliderData['high'] = sliderData['max'];
	}
	if(!sliderData['low']){
			sliderData['low'] = sliderData['min'];
	}
	$("#range_slider").slider({
		range: true,
		min: sliderData['min'],
		max: sliderData['max'],
		values: [sliderData['low'], sliderData['high']],
		slide: function(event, ui) {
			//Sliding slider values
			prices = convertPrice(ui.values[0],ui.values[1], $("#range_slider").slider("option", "max"));
			$("#amount").val(prices[0] + ' - ' + prices[1]);
			$("#int_price_from").val(ui.values[0]);
			$("#int_price_to").val(ui.values[1]);
		},
		change: function(event, ui){
			//console.log(ui.values);
		}
	});
//	$("#amount").val($("#range_slider").slider("values", 0) + '€' + ' - ' + $("#range_slider").slider("values", 1) + '€');
	
	//Initial slider values
	prices = convertPrice($("#range_slider").slider("values", 0),$("#range_slider").slider("values", 1), $("#range_slider").slider("option", "max"));
	$("#amount").val(prices[0] + ' - ' + prices[1]);
	
	$("#int_price_from").val($("#range_slider").slider("values", 0));
	$("#int_price_to").val($("#range_slider").slider("values", 1));

	$(".all_images a").click(function(){
		//var largePath = $(this).attr("rel");
		//var imageTitle = $(this).attr("title");
		//$(".top_image img").attr({ src: largePath });
		//$(".zoom_hint").attr({ href: largePath });
		//$(".zoom_hint").attr({ title: imageTitle });
		$('.item_images_set').hide();
		$('#itemImage_' + $(this).attr('rel')).fadeIn();
	});
	
});

function convertPrice(min, max, maxLimit){
	 min = '' + min;
	 max = '' + max;	
	if(min == 0) {
		min = "Dogovor";
	} else {
		for (var i = 0; i < Math.floor((min.length-(1+i))/3); i++){
			min = min.substring(0,min.length-(4*i+3))+','+min.substring(min.length-(4*i+3));
		}
		min = min + '€';
	}
	if(parseInt(max) >= maxLimit) {
		max = "Bez ograničenja";
	} else {
		for (var i = 0; i < Math.floor((max.length-(1+i))/3); i++){
			max = max.substring(0,max.length-(4*i+3))+','+max.substring(max.length-(4*i+3));
		}
		max = max + '€';
	}
	return {0:min, 1:max};
};

function addFormField(maxNumber) {
	var id = parseInt($("#id").val());
	if (id < parseInt(maxNumber)){
		$("#more_fields").append("<div class='rel mb15' id='row" + id + "'><input class='upload_photo' type='file' name='image_" + id + "' id='image_" + id + "' />&nbsp;&nbsp<a href='#null' class='remove'>Ukloni polje</a></div>");
		$("#image_" + id).filestyle({ 
			image: imagesPath + "browse_btn.gif",
			imageheight : 33,
			imagewidth : 125,
			width : 310
		});
		
		$("#row" + (id - 1) + " .remove").remove();
		id = id + 1;
		$("#id").val(id);
	}
}

function removeField() {
	$(this).parent().remove();
	$("#id").val(parseInt($("#id").val()) - 1);
	$("#row" + parseInt($("#id").val() - 1)).append("<a href='#null' class='remove'>Ukloni polje</a>");
}

function reloadSelectBox(selectBoxID, onClickFunction){
	$('#' + selectBoxID + '_input').remove();
	$('#' + selectBoxID + '_container').remove();
	if (onClickFunction != ''){
		$('#' + selectBoxID).selectbox({onClickFunc: onClickFunction});
	}else{
		$('#' + selectBoxID).selectbox();
	}
}

//function PopulateSelectBox(options) {
//	if (options) {
//		selectBoxArray = $('<div></div>');
//		selectBoxArray = $(selectBoxArray).append($('<option></option>').text('--------').attr('value', ''));
//		$.each(options, function(j, val) {
//			$(selectBoxArray).append($('<option></option>').text(val).attr('value', j));
//		});
//		return $(selectBoxArray).children();
//	}
//}

/////////////////////////od
function setupSliderData(sliderObj){
	if (sliderObj){
		sliderVals = $('#range_slider').slider('values');
		if(sliderObj.val() == '') {
			if($(sliderObj).attr('id') == 'int_category_id') {
				tmp = parseInt(sliderData['max']);
				$.each (ad_max_price, function(j, val){
					if(parseInt(val) > tmp) {
						tmp = parseInt(val);
					}
				});
				$("#range_slider").slider("option", "max", tmp+1).slider('values', sliderVals);
			} else if($(sliderObj).attr('id') == 'int_sub_category_id' && ad_max_price[$('#int_category_id').val()]) {
				$('#range_slider').slider("option", "max", parseInt(ad_max_price[$('#int_category_id').val()])+1).slider('values', sliderVals);
			} else {
				$('#range_slider').slider('option', 'max', sliderData['max']).slider('values', sliderVals);
			}
		} else if(ad_max_price[sliderObj.val()]) {
			$('#range_slider').slider("option", "max", parseInt(ad_max_price[sliderObj.val()])+1).slider('values', sliderVals);
		} else {
			$('#range_slider').slider("option", "max", sliderData['max']).slider('values', sliderVals);
		}
	}
	$('#int_price_from').val($('#range_slider').slider('values', 0));
	$('#int_price_to').val($('#range_slider').slider('values', 1));
	return $('#range_slider').slider('values');
}
function populateSubCats(cat_id){
	$('#int_sub_category_id').empty();
	if (cat_id && parseInt(cat_id) > 0){
		$.get(
			populateSubCatsLink,
			{ part: 'GetSubCats', int_cat_id: cat_id},
			function(data) {
				var p = PopulateSelectBox(data);
				$('#int_sub_category_id').html(p);
				if(ad_max_price[$("#int_sub_category_id").val()]){
					$("#range_slider").slider("option", "max", parseInt(ad_max_price[$('#int_sub_category_id').val()])+1).slider("values", $("#range_slider").slider("values"));
					$("#int_price_from").val($("#range_slider").slider("values", 0));
					$("#int_price_to").val($("#range_slider").slider("values", 1));
				} else if ($("#int_sub_category_id").val()) {
					$("#range_slider").slider("option", "max", sliderData['max']);
				}
				prices = convertPrice($("#range_slider").slider("values", 0), $("#range_slider").slider("values", 1), $("#range_slider").slider("option", "max"));
				$("#amount").val(prices[0] + ' - ' + prices[1]);
			}, 'json' 
		);
	}
}
function PopulateSelectBox(options) {
	if (options) {
		selectBoxArray = $('<div></div>');
		selectBoxArray = $(selectBoxArray).append($('<option></option>').text('--------').attr('value', ''));
		$.each(options, function(j, val) {
			if(val['max_price'] > 0) {
				ad_max_price[j] = val['max_price'];
			}
			if(jFix[0] == j || jFix[1] == j) {
				$(selectBoxArray).append($('<option></option>').text(val['name']).attr('value', j).attr('selected', 'selected'));
			} else {
				$(selectBoxArray).append($('<option></option>').text(val['name']).attr('value', j));
			}
		});
		return $(selectBoxArray).children();
	}
};
///////////////////////////////////////// DO

function addEmptyImageFormField(maxNumber, emptyImages) {
	var id =  parseInt($('#id').val());
	if (id < parseInt(maxNumber)){
		$("#more_fields").append("<table class='edit_ad_images mb15'><tr class='rel mb15' id='row" + id + "'><td class='edit_photo_field'><input class='upload_photo' type='file' name='image_" + emptyImages[id] + "' id='image_" + emptyImages[id] + "' /></td><td>Foto " + emptyImages[id] + "&nbsp;&nbsp<a href='#null' class='remove'>Ukloni polje</a></td></tr></table>");
		$("#image_" + emptyImages[id]).filestyle({ 
			image: imagesPath + "browse_btn.gif",
			imageheight : 33,
			imagewidth : 125,
			width : 310
		});
		
		$("#row" + (id - 1) + " .remove").remove();
		id = id + 1;
		$("#id").val(id);
	}
}
