function toggleHiddenChildrenOfType(itemID, childrenType)
{
	var items  = $("#" + itemID + " > " + childrenType) ;

    if (items)
         items.toggleClass("hidden") ;
}

function toggleHiddenTROfTable(tableID)
{
    var table = $("#" + tableID) ;

     if (table) {
         var  tbody = table.find('tbody') ;
         $(tbody).children().toggleClass("tohide") ;
         $(tbody).children().toggleClass("toshow") ;
    }
}

var fadeEffect = false ;

function toggleShoppingCart() 
{
	toggleHiddenChildrenOfType( 'shoppingCartLinks', 'a' ) ;
    $("#shoppingCartTable").toggleClass("tohide").toggleClass("toshow") ;
    $("#shoppingCartFrame").toggleClass("tohide").toggleClass("toshow") ;
	$("#noJavaScript").addClass("hidden") ;

	if (fadeEffect == true) {
		$(".tohide").animate({ height: 'hide', opacity: 'hide' }, 'slow') ;
		$(".toshow").animate({ height: 'show', opacity: 'show' }, 'slow') ;
	}
	else {
		$(".tohide").slideUp("slow") ;
		$(".toshow").slideDown("slow") ;
	}

	if ( $("#shoppingCartFrame").hasClass("toshow") ) {
		prepareIFrames(document, true) ;
	}

	scroll(0,0); 
}

function addIFrameRow(tableID)
{
	var tableShoppingCart = $("#" + tableID) ;
	
	if(tableShoppingCart) {
		var enclosingTD = tableShoppingCart.parent() ;
		
		tableShoppingCart.addClass('toshow') ;
		$('<table id="shoppingCartFrame" class="tohide" style="border-color: #079515; border-width: 1px; border-style: solid;"><tr id="iframeRow">' +
					'<td width="780" valign="top" id="iframeCell">' +
						'<iframe class="autoHeight" scrolling="auto" height="100%" width="100%" frameborder="0" src="cartfile/fcaddie.php" name="shoppingCartFrame" ></iframe>' +
					'</td>' +
				'</tr></table>').appendTo(enclosingTD);
	}
	
	var ahrefs = $("a[href='cartfile/caddie.php?lang=1']") ;
	ahrefs.each(function(index){
		try {
			if (tableShoppingCart) {
				$(this).attr("href", "javascript:toggleShoppingCart();") ;
				$(this).attr("target", "") ;
			}
			else {
				$(this).addClass("hidden") ;
			}
		}
		catch(err) {
			// alert("patching href: " + err) ;
		}
	});

	var location = escape( window.document.location ) ;
	var ahrefs = $("a[href='cartfile/fsend2afriend.php']") ;
	ahrefs.each(function(index){
		try {

			$(this).attr("href", "cartfile/fsend2afriend.php?page=" + location) ;
			$(this).attr("target", "_blank") ;
			$(this).click( function () {
				window.open (this.href, 'Envoyer &agrave; un(e) ami(e)', 'height=220,width=440,resizable=true,scrollbars=true'); return false ;
				}
			) ;
		}
		catch(err) {
			// alert("patching href: " + err) ;
		}
	});
}

function patchAddToCaddie()
{
	var images = 	$("input[type='image'][src$='add_caddie.gif']") ;
	
	images.removeAttr('onclick').unbind('click') ;
	images.each(function(index){
		try {
			$(this).attr("src", "cartfile/im/add_caddie.gif" ) ;
			var p = $(this).parent() ;
			var inputs = $(this).siblings("[type='hidden']") ;
						
			p.append("<input type='hidden' name='btnidx' value='" + index + "' >") ;
		}
		catch(err) {
		//	alert(err) ;
		}
	})
}

function displayShoppingCartInfo()
{
	theURL = window.location.href ;
	
	if (theURL.indexOf("#btn_") != -1) {
		alert("L'article choisi a été ajouté au panier !") ;
	}

	return ;

	$.get("cartfile/admin/getcartinfo.php", { dataType: "xml" }, function(xmlReturnVal){ 
		if (xmlReturnVal != null) {            
			try {       
				result = xmlReturnVal.documentElement ;				                 
				var cartInfo = $("#cartInfo") ;
				cartInfo.empty() ;
				cartInfo.append(document.createTextNode($('caddy', result).attr("message"))) ;
				
				var sc = $("#showCartLink") ;
				var hc = $("#hideCartLink") ;
				var cartInfoTitle = $("#cartInfoTitle") ;
				
				if (sc.hasClass("hidden") && hc.hasClass("hidden")) {
					cartInfoTitle.empty() ;
					if ($('caddy', result).attr('narticles') > 0) {
						cartInfoTitle.append(document.createTextNode($('caddy', result).attr("title"))) ;
						cartInfoTitle.removeClass("hidden") ;
					}
				}
				else {
					cartInfoTitle.addClass("hidden") ;
				}
				
				// alert("lastorder ok: " + $('lastorder', result).attr("ok")) ;
				if ( $('lastorder', result).attr("ok") == 1 ) {
					toggleShoppingCart() ;
				}
			}
			catch(errv) {
				// alert("exception: " + errv) ;
			}
		}
	});
}

function userSessionTracking(theAction)
{
	theURL = window.location.href ;
    theHost = window.location.protocol + '//' + window.location.host;
	theURL = theURL.substring(theHost.length, theURL.length) ;
	
	$.get("cartfile/admin/usession.php", { action: theAction, url: theURL, dataType: "xml" }, function(xmlReturnVal){ 
		if (xmlReturnVal != null) {            
			try {       

			}
			catch(errv) {
			//	alert("exception: " + errv) ;
			}
		}
	});
}

$(function() {
	$("#noJavaScript").addClass("hidden") ;
	addIFrameRow('shoppingCartTable') ;

	if ($("#shoppingCartFrame").hasClass("toshow") )
		$("#showCartLink").addClass("hidden") ;
	else
		$("#showCartLink").removeClass("hidden") ;

	$(".tohide").hide() ;

	patchAddToCaddie() ;
	userSessionTracking("load html") ;
	displayShoppingCartInfo() ;
});
