
var popsommaire;

$().ready(function() 
{   
	  $(".positionable").each(function positionner() {
			$(this).position({
				of: $(this).parent(),
				my: "right top ",
				at: "right top",
				offset: 0,
				collision: "fit"
				});
			});
	 $( ".positionable" ).css( "opacity", 0.5 );
	 $('.menu li').removeClass('actif');
	 $('#onglet_magazines').addClass('actif');

	 
	 $("#wrapper_sommaire").hide();
	 $("#loading_image").hide();
	 $("#total_articles").hide();

	 $("#validation_commande").click(function(){submit_commande();});
   	 $("#validation_recherche").click(function(){submit_recherche();});

 	$("#wrapper_contacts").hide(); // contact
 	$("#lien_contacts").click(function(){voirContacts();});
	
 	$('#periode > option:selected').val('0');
 	$("#select_titre_all").click(function(){
 		if($(this).is(':checked'))  
 			{
 			$('#select_titre_obs').attr('checked',false) ;
 			$('#select_titre_sca').attr('checked',false);
 			$('#select_titre_chl').attr('checked',false) ;
 			$('#select_titre_hs').attr('checked',false) ;
 			}
 		});
 	$("#select_titre_obs,#select_titre_sca,#select_titre_chl,#select_titre_hs").click(function(){
 		if ( $(this).is(':checked') )	$('#select_titre_all').attr('checked',false) ;
 		});
   	 
   	getPanier();	  
     
    $(function() {
       		$( "#ADT_DEB" ).datepicker({
       			showOn: "button",
       			buttonImage: "img/2011/calendar.gif" ,
       			buttonImageOnly: true,
       			dateFormat: "dd/mm/yy" , 
       			minDate:null , 
       			altFormat : "yy-mm-dd" , 
       			altField: "#ADRT_DEB"
       		});

       		$( "#ADT_FIN" ).datepicker({
       			showOn: "button",
       			buttonImage: "img/2011/calendar.gif" ,
       			buttonImageOnly: true,
       			dateFormat: "dd/mm/yy" , 
       			minDate:null , 
       			altFormat : "yy-mm-dd" , 
       			altField: "#ADRT_FIN"
   			});
		});
       			

});	

//---------------------------------------------------------------------

function showDialog(obj)
{  
		
	if ( obj == 'wrapper_sommaire') titre = 'Sommaire';
	if ( obj == 'wrapper_sommaire') titre = '';

	if ( obj == 'wrapper_contacts' ) $("#" + obj ).dialog({autoOpen: false, modal: true, height:380,width:790});
	else $("#" + obj ).dialog({autoOpen: false, modal: true,height: 460,width: 850});
			
	$("#" + obj ).dialog( "option", "hide", 'slide' );
	$("#" + obj ).dialog( "option", "show", 'slide' );
	$("#" + obj ).dialog("open");   
	$("#" + obj ).dialog( "option" , "title" ,titre);
	$("#" + obj ).dialog( "option", "resizable", false );
		
	$("#" + obj ).bind( "dialogclose", function(event, ui) {
		$("#loading_image").hide();
		});
return false;
}
//---------------------------------------------------------------------
function voirContacts() 
{
	
	showDialog('wrapper_contacts','') ;
	return ;
}

function open_sommaire()
{
	showDialog('wrapper_sommaire') ;
}
	
function get_sommaire()
{
	$("#loading_image").show();
	var donnees = '' ;
	$.ajax({ 
	    	url : racine_abov + "abovision2.php?P1=PRL&P2=2011_ACC&PG=SOMMAIRE" ,
	    	type : 'POST' ,
	    	dataType : 'html' ,
			data : donnees ,
			async: true,
        	success:   processXml_sommaire  // post-submit callback
	    });
}
//--- sommaire -----------------------------------------------------
function go2Sommaire(mid,numparu,datparu,libelle,price,page_number)
{
	var donnees = "P1=PRL&P2=2011_ESP&PG=CATALOGUE&TYPE=ISSUE_MID&VAL1=" + mid + "&VAL2=" + numparu + "&VAL3=" + datparu + "&VAL4=" + libelle + "&VAL5=" + price + "&VAL6=" + page_number ;
	$.ajax({ 
	url : racine_abov + 'ab_session.php',
	type : 'POST' ,
	dataType : 'xml' ,
	data : donnees ,
	async: false,
	success:   processXml  // post-submit callback
	});
}
//--- achat -----------------------------------------------------
function go2Panier(mid,numparu,datparu,libelle,price,page_number,formule,revue)
{
	var donnees = "action=1&revue=" + revue + "&formule=" + formule + "&mt=" + price;
	donnees += '&mid=' + mid + '&numparu=' + numparu+ '&datparu=' + datparu + '&libelle=' + libelle + '&page_number=' + page_number;
	donnees += '&P1=' + $('#P1').val();
	donnees += '&P2=' + $('#P2').val();
	donnees +=  '&site=' + $('#site').val();

	$.ajax({ 
	url : racine_abov + 'offre_magazines_submit.php',
	type : 'POST' ,
	dataType : 'xml' ,
	data : donnees ,
	async: false,
	success:   processPanier  // post-submit callback
	});
}
function getPanier()
{

	var donnees = "action=L";
	donnees += '&P1=' + $('#P1').val();
	donnees += '&P2=' + $('#P2').val();
	donnees +=  '&site=' + $('#site').val();
// alert(racine_abov + 'offre_magazines_submit.php');
	$.ajax({ 
	url : racine_abov + 'offre_magazines_submit.php',
	type : 'POST' ,
	dataType : 'xml' ,
	data : donnees ,
	async: true,
	success:   processContenuPanier  // post-submit callback
	});
}
function videPanier(id)
{
	var donnees = "action=V";
	donnees += '&P1=' + $('#P1').val();
	donnees += '&P2=' + $('#P2').val();
	donnees +=  '&id_article=' + id;

	$.ajax({ 
	url : racine_abov + 'offre_magazines_submit.php',
	type : 'POST' ,
	dataType : 'xml' ,
	data : donnees ,
	async: true,
	success:   processContenuPanier  // post-submit callback
	});
}
function submit_commande()
{
	var sURL = unescape(window.location.pathname);
	window.location.href = sURL + '?P1=PRL&P2=2011_MAG&PG=PANIER';
	return false ;
}
function processXml(responseXML) 
{
	get_sommaire();
}
function processPanier(responseXML) 
{
	$('#r_rc').text('0');
	if ( $('rc', responseXML).text() != '1' ) 
	{
	alert($('message', responseXML).text());
	$('#r_rc').text($('rc', responseXML).text());
	$("#loading_image").hide(); 
	return;
	}
	
	
	getPanier();
}
function processContenuPanier(responseXML) 
{
// alert(responseXML);
var ligne_HTML = '<div class="titre"><libelle></div>';
ligne_HTML += '<div class="numero">N&deg; <numparu></div><div class="date">du <datparu></div>';
ligne_HTML += '<div class="corbeille"><a class="panier_corbeille" href="javascript:videPanier(<id>);"/></div>';
ligne_HTML += '<div class="prix"><mt></div>';

var divHtml = '' ;
var h = 150 ;
$('articles', responseXML).find('article').each(
		function()
		{
			var id = $(this).find('id').text();	
			var libelle = $(this).find('libelle').text();	
			var revue = $(this).find('revue').text();	
			var numparu = $(this).find('numparu').text();	
			var datparu = $(this).find('datparu').text();	
			var mt = $(this).find('mt').text();	
			var temp = ligne_HTML.replace('<revue>',revue);
			temp = temp.replace('<id>',id);
			temp = temp.replace('<libelle>',libelle);
			temp = temp.replace('<numparu>',numparu);
			temp = temp.replace('<datparu>',datparu);
			temp = temp.replace('<mt>',mt + '&nbsp;&euro;');
			divHtml += temp ;
			h += 70;
		});

if ( divHtml != '' )
	$('#produits_header').replaceWith( '<div id="produits_header"><h3>MAGAZINES</h3></div>') ;
	else
	$('#produits_header').replaceWith( '<div id="produits_header"><h3>votre panier est vide</h3></div>') ;
$('#produits').replaceWith( '<div class="produits" id="produits">' + divHtml + '</div>');
$('.total').text($('total', responseXML).text()+ ' €');
$('#panier_Box').height(h + 'px') ;
if ( divHtml != '' ) {$("#produits_header").show();$("#btn_panier").show();$("#total_articles").show(); }
	else {$("#total_articles").hide();$("#btn_panier").hide();}

}

function processXml_sommaire(responseHTML) 
{
	$("#loading_image").hide(); 
	$('#sommaire_content').replaceWith(responseHTML);
	open_sommaire();

	return false ;
}

//--- recherche -----------------------------------------------------
function submit_recherche()
{
	var url = racine_abov + "abovision2.php?P1=" + $('#P1').val() + "&P2=" + $('#P2').val() + "&PG=MISEARCH" ;

	var periode = $("#periode option:selected").val();
	var periode_deb = $("#ADRT_DEB").val();
	var periode_fin = $("#ADRT_FIN").val();
	var mots = $("#MOTS").val();
	var titres = '' ;
	if ( $('#select_titre_all').is(':checked') ) titres = '' ;
	else
		{
		if ( $('#select_titre_obs').is(':checked') ) titres += ',OBS' ;
		if ( $('#select_titre_sca').is(':checked') ) titres += ',SCA' ;
		if ( $('#select_titre_chl').is(':checked') ) titres += ',CHL' ;
		if ( $('#select_titre_hs').is(':checked') ) titres += ',OBH,SCH' ;
		}
	if ( titres != '' ) titres = titres.substring(1) ;
	
	var donnees = "&PERIODE=" + periode + "&PDEB=" + periode_deb + "&PFIN=" + periode_fin + "&MOTS=" + mots+ "&TITRES=" + titres ;
	$.ajax({ 
	    	url : url ,
	    	type : 'POST' ,
	    	dataType : 'html' ,
			data : donnees ,
			async: true,
        	success:  processXml_search   // post-submit callback
	    });

	return false ;
}


function processXml_search(responseHTML) 
{
	alert(responseHTML);
	//responseHTML='<div id="resultats">www</div>' ;
	$("#loading_image").hide(); 
	$('#resultats').replaceWith(responseHTML);
	$("#resultats").show();
	//if ( $("#nbresultats").length == 0  ) $("#resultats").hide();
	return false ;
}


