// Version: 17-Jul-2009 
function addBid(user_id, product_id, owner_id){
	if ( user_id == owner_id )
		return;
	popup('?m=market&a=add_bid&user_id='+user_id+'&product_id='+product_id+'&owner_id='+owner_id);
}

function showPhotos(product_id){
	popup('?m=market&a=show_photos&product_id='+product_id);
}

function showMap(product_id){
	popup('?m=market&a=show_map&product_id='+product_id);
}

function editMyProducts(){
	prods = getMySelectedProducts();
	if (prods.length > 1)
		alert('U dient maximaal 1 product te selecteren');
	else if (prods.length < 1)
		alert('U dient minimaal 1 product te selecteren');
	else // == 1
		document.location = '?m=market&a=edit_product&id=prod&product_id='+prods[0];
}

function fs_validation_limit_number_of_adds(element_id, form_id, result_id, params){
	value = getValue(element_id);
	element_name = el(element_id+'_input').name;
	response = AJAX.getSynch("?m=market&a=validate_number_of_adds_limit&form_id="+form_id+"&value="+value+"&result_id="+result_id, "", []);
	return response == "ok";

}

function archiveMyProducts(){
	prods = getMySelectedProducts();
	popup('?m=market&a=acknowledge_archive&products='+prods.join());
}

function markMyProductsAsSold(){
	prods = getMySelectedProducts();
	popup('?m=market&a=acknowledge_sold&products='+prods.join());
}

function getMySelectedProducts(){
	var children = el('ad_table').getElementsByTagName('input');
	var products = new Array();
	for(i = 0; i < children.length; i++){
		if (children.item(i).checked &&
			children.item(i).name != '')
			products.push(children.item(i).name);
	}
	return products;
}

function action_BCMarketFunction(element, condition){
	elements = element.split('__');
	category_id = el(elements[0]+'_'+elements[2]+'_input').value;
	if (condition){
		if (el('marktplaats_fs_market_category_id_input') != undefined){
			changed = (el('marktplaats_fs_market_category_id_input').value != category_id);
			el('marktplaats_fs_market_category_id_input').value = category_id;			 
			// veld gewijzigd, event aanroepen
			on_marktplaats_fs_market_category_id_event();		
			url  = '?m=market&a=get_market_categories&id='+elements[1]+'&category_id='+category_id+'';
			if (changed)
				AJAX.get(url, AJAX.replaceContent, {'id':'marktplaats_form_group_fs_market_category_id'});
		} else {
			// KAN NIET? HIJ IS UNDEFINED
			//changed = (el('marktplaats_fs_market_category_id_input').value != category_id);
			el('left_column_market_category_id_input').value = category_id;
			// veld gewijzigd, event aanroepen
			on_left_column_market_category_id_event();
			url  = '?m=market&a=get_market_categories&id='+elements[1]+'&category_id='+category_id+'&search=1';
			//if (changed)
				AJAX.get(url, AJAX.replaceContent, {'id':'left_column_form_group_market_category_id'});
		}
	} else {
		if (el('marktplaats_fs_market_category_id_input') != undefined){
			if (el('marktplaats_fs_market_category_id_input').value != ''
			&& category_id == ''){
				el('marktplaats_fs_market_category_id_input').value = category_id;
			}
		} else {
			if (el('left_column_market_category_id_input').value != ''
			&& category_id == ''){
				el('left_column_market_category_id_input').value = category_id;
			}
		}
	}
}

function equal_or_sub(element, value){
	return (element.substring(0,value.length) == value);
}

function enableImage(image_id, photos_count)
{
	for(var idx = 0; idx < photos_count; idx++ )
	{
		if(document.getElementById(idx))
			document.getElementById(idx).style.display = 'none';
	}
	if(document.getElementById(image_id))
		document.getElementById(image_id).style.display = '';

	var width = Math.max(605, el(image_id).offsetWidth+26) + "px";
	el('popup_window').style.width = width;
	el('popup_titlebar').style.width = width;
	/*
	if(document.getElementById(image_id).height>600)
	{
		var scaling=document.getElementById(image_id).height / 600;
		var imageWidth=document.getElementById(image_id).width / scaling;
		
		el('popup_window').style.height = "750px";
		el('popup_window').style.width = (imageWidth+30) + "px";
		el('popup_titlebar').style.width = (imageWidth+30) + "px";
		document.getElementById(image_id).height=600;
	}
	else if(document.getElementById(image_id).width>600)
	{
		var scaling=document.getElementById(image_id).height / 600;
		var imageHeight=(document.getElementById(image_id).width / scaling)+150;
		
		el('popup_window').style.width = "630px";
		el('popup_titlebar').style.width = "630px";
		el('popup_window').style.height = imageHeight + "px";
		document.getElementById(image_id).height=600;
	}
*/
}

function updateImageThumbnails(first_id, show_photos, photos_count)
{
	first_photo_index = first_id;
	if(photos_count > 7)
	{
		for(var idx = 0; idx < photos_count; idx++ )
		{
			if(document.getElementById('thumb_'+idx))
				document.getElementById('thumb_'+idx).style.display = 'none';
		}
		
		for(var idx2 = first_id; idx2 < (show_photos + first_id); idx2++ )
		{
			if(document.getElementById('thumb_'+idx2))
				document.getElementById('thumb_'+idx2).style.display = '';
		}
		
		if(first_id == 0)
		{
			if(el('arrow_left'))
				el('arrow_left').style.display = 'none';
		}
		else
		{
			if(el('arrow_left'))
				el('arrow_left').style.display = '';
		}
		
		
		if(first_id >= (photos_count - show_photos))
		{
			if(el('arrow_right'))
				el('arrow_right').style.display = 'none';
		}
		else
		{
			if(el('arrow_right'))
				el('arrow_right').style.display = '';
		}
	}
	else
	{
		for(var idx = 0; idx < photos_count; idx++ )
		{
			if(document.getElementById('thumb_'+idx))
				document.getElementById('thumb_'+idx).style.display = '';
		}
	}
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function toggleSearchDiv()
{
	
	if(el('advance_search_container'))
	{
		displayFlashObjects(el('advance_search_container').style.display != 'none');
		if(el('advance_search_container').style.display == 'none')
		{
			el('advance_search_container').style.display = '';
			el('advance_search_link').innerHTML = ' -';
			
			var left_pos = findPosX(el('advance_search_container').parentNode);
			
			el('advance_search_container').style.left = '0 px';
			//el('advance_search_container').style.position = 'absolute';
			el('simple_search_container').style.display = 'none';
		}
		else
		{
			el('advance_search_container').style.display = 'none';
			el('advance_search_link').innerHTML = 'uitgebreid zoeken';
			el('simple_search_container').style.display = '';
		}
	}
	return false;
}


function toggleMarketMenu( list_id )
{
	if(el('newest_products'))
	{
		el('newest_products').style.display = 'none'
	}
	
	if(el('latest_bids_products'))
	{
		el('latest_bids_products').style.display = 'none'
	}
	
	if(el('newest_cats_products'))
	{
		el('newest_cats_products').style.display = 'none'
	}
	
	if(el(list_id))
	{
		el(list_id).style.display = 'block'
	}
	
	
}
