var pic_on = 1;
var base_url = '';

$(document).ready(function() { 
	var options = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       login_resp
	}
	// bind 'loginForm' and provide a simple callback function 
	$('#loginForm').ajaxForm(options);
	$('#logoutLink').click(function (){ $.get(this, login_resp); return false;} );
	$('#thumb1').click(function (){ return flip_thumbs(this, 1); });
	$('#thumb2').click(function (){ return flip_thumbs(this, 2); });
	$('#thumb3').click(function (){ return flip_thumbs(this, 3); });
	$('#thumb4').click(function (){ return flip_thumbs(this, 4); });
	$('#thumb5').click(function (){ return flip_thumbs(this, 5); });
	
	fill_fresh_ads();
	fill_fresh_ads_horiz();
});

function flip_thumbs(obj, ix){
	$(obj).hide();
	var path = obj.src;
	var psegs = path.split("thumbs/");
	$('#bigimage').attr("src", psegs[0]+psegs[1]);
	$('#thumb'+pic_on).fadeIn("fast");
	pic_on = ix;
}

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
	return true;
    var queryString = $.param(formData); 
    alert('About to submit: \n\n' + queryString); 
    return true;
} 
 
// post-submit callback 
function login_resp(responseText, statusText)  { 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); 
	if (statusText == 'success')
	$('#usermenu').fadeOut(300);
	$('#usermenu').replaceWith(responseText);
	$('#usermenu').show(300);
	
	var options = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       login_resp
	}
	$('#loginForm').ajaxForm(options);
	$('#logoutLink').click(function (){ $.get(this, login_resp); return false;} );
}

function init_base_url(url) {
	base_url = url;
}

function fill_fresh_ads() {
	if ($('#fresh_ads_box').attr('id') == 'fresh_ads_box'){
		var num = Math.floor($('#fresh_ads_box').parent().height() / 140);
		var url = "http://"+window.location.hostname + '/oglas/svezi/' + num;
		$.get(url, function(data){
			$('#fresh_ads_box').html(data);
			});
	}
}

function fill_fresh_ads_horiz() {
	var num = 5;
	var url = base_url + 'oglas/svezi/' + num;
	if ($('#fresh_ads_box_h1').attr('id') == 'fresh_ads_box_h1'){
		$.get(url, function(data){
			$('#fresh_ads_box_h1').html(data + '<div class="clear"></div>');
		});
	}
	if ($('#fresh_ads_box_h2').attr('id') == 'fresh_ads_box_h2'){
		$.get(url, function(data){
			$('#fresh_ads_box_h2').html(data + '<div class="clear"></div>');
		});
	}
}

function ad_extend_exp(ad_id){
	var url = base_url + 'oglas/extend/' + ad_id;
	$.get(url, null, function(data){
		window.location.reload();
		});
}
