Ajax.Responders.register({
		onCreate: function() {
			$('loading').show();
		},
		onComplete: function() {
			$('loading').hide();
			//$('ad728').src = 'http://www.pooki.fr/banner/728x90.php';
			$('ad300').src = 'http://www.pooki.fr/banner/300x250.php';
			$('ad160').src = 'http://www.pooki.fr/banner/160x600.php';
		}
		
	}
);

var loaded = new Array();
function load(file)
{
	if ( ! ( file in loaded ) )
	{
		loaded[file] = 1;
		
		var script = document.createElement('script');
		script.src = file;
		script.type = 'text/javascript';
		document.getElementsByTagName('head').item(0).appendChild(script);
	}
}

function alertt(msg)
{
	if ( null != $('status') ) {
		$('status').update(msg).show();
	} else {
		alert(msg);
	}
}

function message(msg, e)
{
	$('messageText').update(msg);
	Position.clone($(e), $('message'));
	new Effect.Appear($('message'), {to: 0.8});
	setTimeout('hide_message()', 5000);
}

function hide_message()
{
	new Effect.Fade($('message'), {from: 0.8});
}

function doSearch()
{
	if ( $F('k').length >= 2 ) {
		
		//return true;
		
		window.location = '/recherche/' + $F('k').replace(/\'| |\"/g,'-') + '.html';
		/*new Ajax.Request('/ajax/get_search_url.php', {
				parameters: {
					k: $F('k')
				},
				onSuccess: function(t) {
					window.location = t.responseText;
				}
			}
		);*/
	} else {
		alert('Votre recherche doit comporter au moins 2 caractères !');
		return false;
	}
}

/****************************
 * HOME
 ****************************/

var home_show_top_current = 'homeTopProductsGlobal';
function home_show_top(show) {
	if ( show != home_show_top_current ) {
		$(home_show_top_current + 'Tab').removeClassName('tabOn');
		$(show + 'Tab').addClassName('tabOn');
		
		$(home_show_top_current).hide();
		$(show).show();
		
		home_show_top_current = show;
	}
}

/****************************
 * NEWS
 ****************************/

function news_display(type, status) {
	var tab = $('newsList').getElementsByClassName(type);
	for(i=0;i<tab.length;i++){
		if ( true == status ) {
			new Effect.BlindDown(tab[i]);
		} else {
			new Effect.SwitchOff(tab[i]);
		}
	}
}

var moreOptions = 0;
function show_more_options() {
	if(moreOptions == 0){
		moreOptions = 1;
		new Effect.BlindDown('moreOptions', {duration: 0.2});
	} else{
		moreOptions = 0;
		new Effect.SwitchOff('moreOptions', {duration: 0.2});
	}
}

/****************************
 * CARD
 ****************************/

function card_load_content(id, content, div_id) {
	var div_id = ( typeof div_id == 'undefined' ? content : div_id );
	$('cardTab_' + div_id).blur();
	$('cardLeft').scrollTo();
	$('loading').show();
	
	new Effect.BlindUp('cardContent', {
			duration: 0.3,
			afterFinish: function() {
				new Ajax.Updater('cardContent', '/ajax/load_content.php', {
						parameters: {
							id: id,
							content: content
						},
						onComplete: function() {
							card_tab_select(div_id);
							new Effect.BlindDown('cardContent', {
									duration: 0.3
								}
							);
						}
					}
				);
			}
		}
	);
}

var card_tab_select_current = 'resume';
function card_tab_select(tab) {
	$('cardTab_' + card_tab_select_current).removeClassName('tabOn');
	$('cardTab_' + tab).addClassName('tabOn');
	card_tab_select_current = tab;
}

/****************************
 * CARD CHILD
 ****************************/

function card_child_load_content(id, content, page) {
	//$('cardTab_' + content).blur();
	$('cardLeft').scrollTo();
	$('loading').show();
	
	new Effect.BlindUp('cardContentChild', {
			duration: 0.3,
			afterFinish: function() {
				var parameters = new Object();
				if($('listForm') != null){
					parameters = $('listForm').serialize(true);
				}
				parameters.page = ( typeof page == 'undefined' ? 1 : page );
				parameters.id = id;
				parameters.content = content;
				new Ajax.Updater('cardContentChild', '/ajax/load_content_child.php', {
						parameters: parameters,
						onComplete: function() {
							new Effect.BlindDown('cardContentChild', {
									duration: 0.3
								}
							);
						}
					}
				);
			}
		}
	);
}

/****************************
 * LISTING
 ****************************/

function list_search(page) {
	$('list').scrollTo();
	
	new Effect.BlindUp('listContent', {
			duration: 0.3,
			afterFinish: function() {
				parameters = $('listForm').serialize(true);
				parameters.page = ( typeof page == 'undefined' ? 1 : page );
				
				new Ajax.Updater('listContent', '/ajax/list_search.php', {
						parameters: parameters,
						onComplete: function() {
							$('listContent').show();
						}
					}
				);
			}
		}
	);
}

/****************************
 * BOX
 ****************************/

var box_open = false;

function open_box(page, params)
{
	$('ad').innerHTML = $('ad').innerHTML;
	box_was_open = box_open;
	box_open = true;
	$('loading').show();
	
	if ( Prototype.Browser.IE ) {
		$('page').scrollTo();
	}
	
	if ( ! box_was_open ) {
		new Effect.Appear('cover', {
				duration: 0.2,
				to: 0.8,
				beforeStart: function() {
					$('ad', 'rightAd').invoke('setStyle', {
							visibility: 'hidden'
						}
					);
				}
			}
		);
	}
	
	new Ajax.Updater('box', '/boxes/' + page + '.php', {
			parameters: params,
			evalScripts: true,
			onComplete: function() {
				if ( ! box_was_open ) {
					new Effect.BlindDown('box', {
							duration: 0.8
						}
					);
				}
			}
		}
	);
}

function close_box()
{
	box_open = false;
	
	Effect.BlindUp('box', {
			duration: 0.5,
			afterFinish: function() {
				Effect.Fade('cover', {
						duration: 0.2,
						afterFinish: function() {
							$('ad', 'rightAd').invoke('setStyle', {visibility: ''});
						}
					}
				);
			}
		}
	);
}

function publish(type, params)
{
	if ( isNaN(params.productId) ) {
		message('Vous devez être sur une fiche produit pour pouvoir publier.', 'rightPublish');
	} else {
		open_box('publish_' + type, params);
	}
}

function form_request(f)
{
	form_clear_warnings(f);
	$(f).request();
}

function form_warning(e)
{
	$(e).addClassName('warning');
}

function form_clear_warnings(f)
{
	$(f).getElements().each(
		function(e) {
			$(e).removeClassName('warning');
		}
	);
	
	if ( null != $('status') ) {
		$('status').update('');
	}
}

function changeCouleur(ligne)
{
	ligne.bgColor = '#F2F2F2';
}

function remetCouleur(ligne)
{
	ligne.bgColor = '#FFFFFF';
}

// COMPARE PAGE

function listModels(id){
	var models = $('p'+id);
	var constructors = $('c'+id);
	id_constructor = (constructors[constructors.selectedIndex].value);
		
	if(id_constructor == ''){
		models.options.length=0;
		models.options[0]=new Option('Sélectionnez d\'abord le constructeur','0');
		models.disabled='disabled';
		return false;
	}
		
	new Ajax.Request('http://www.pooki.fr/ajax/list_models.php?c='+id_constructor+'&t=mobile', {
			onSuccess: function(t) {
				models.options.length=0;

				var res= eval('('+ t.responseText +')');
				num=res['models'].length;

				if (num==0) {
					models.options.length=0;
					models.options[0]=new Option('Pas de produits trouvés','0');
					models.disabled='disabled';
					return false;
				}
				models.options[0]=new Option('Choisissez un modèle','0');
				for (var x=0;x<num;x++)
				{
					models.options[x+1]=new Option(res['models'][x]['type'],res['models'][x]['id']);
				}
				models.disabled='';
				}
			}
	);
	
}