/*global swfobject jQuery */

swfobject.registerObject("flashCanetons", "9.0.0", "expressInstall.swf");

function setFocusOnFlash() {
  var fl = $('.column1 object, #flashCanetons').get(0);
  if (fl) { fl.focus(); }
}

if (swfobject.hasFlashPlayerVersion("9.0.0")) {
  swfobject.addDomLoadEvent(setFocusOnFlash);

} 

function updateCombos(arrCombosID) {
	// TEST IN FIREBUG :
	// updateCombos([Math.ceil(Math.random()*48), Math.ceil(Math.random()*48), Math.ceil(Math.random()*48), Math.ceil(Math.random()*48), Math.ceil(Math.random()*48), Math.ceil(Math.random()*48)].slice(Math.ceil(Math.random()*6)))
	// updateCombos([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48])
	
	// Set the height of the container to match the new number of combos
	var $combos = jQuery("#parapluieCombos").animate({height: 158 * arrCombosID.length}, 700);
	
	if (arrCombosID.length !== 0 && jQuery('#callNow:visible').size() === 0) {
		jQuery('#callNow').css('opacity', 0).show().animate({opacity: 1}, 700);
	}
	
	// Remove the old combos with visual effect.
	jQuery('li', $combos).animate(
		{opacity: 0, height: 0},
		700,
		"swing",
		function () {
			jQuery(this).remove();
		}
	);
	
	// Add the new combos with visual effects.
	jQuery(arrCombosID).each(function (i, comboID) {
		// Get the informations about the combo
		var $xmlCombo = jQuery(window.xmlCombos).find('combo[id=' + comboID + ']');
		var $iComboNb = $xmlCombo.find('service').size();
		var strComboDetails = $xmlCombo.find('details').text();

		var strMois = $xmlCombo.find('mois').text();
		
		var strComboPrix = $xmlCombo.find('prix').find('montant').text();
		var fComboPrix = strComboPrix.replace(/[$*]/g, '').replace(/,/, '.');
		var iComboPrix = Math.ceil(fComboPrix);
		
		var strComboRabais = $xmlCombo.find('rabais').find('montant').text();
		var fComboRabais = strComboRabais.replace(/[$*]/g, '').replace(/,/, '.');
		var iComboRabais = Math.ceil(fComboRabais);
		
		var $strCombo = $xmlCombo.find('resume').text();
		var $strTitle = 't_' + $iComboNb + "_services";

		// Modifié par Martin Labrecque
		var $services = $xmlCombo.find('service').text();			

		if ($services.indexOf('IHVE') >= 0) {
			$strTitle += "_ihve";
		} else if ($services.indexOf('IHVI') >= 0) {
			$strTitle += "_ihvi";
		} else if ($services.indexOf("IHV") >= 0) {
			$strTitle += "_ihv";
		}
		
		if ($services.indexOf("Tel 150") >= 0) {
			$strTitle += "_150";
		}
		
		/*if ($xmlCombo.find('service:contains(IHVI)').size()) {
			$strTitle += "_ihvi";
		} else if ($xmlCombo.find('service:contains(IHV)').size()) {
			$strTitle += "_ihv";
		}
		if ($xmlCombo.find('service:contains(Tel 150)').size()) {
			$strTitle += "_150";
		}*/
		// Fin Modification
		
		// Build the combo box
		var $combo = jQuery(
		'<li>' +
		'  <img src="images/combos/' + $strTitle + '.png" alt="Nom du combo" height="24" />' +
		'  <div class="combo_details"><div class="combo_details_top"></div><div class="bordIe" style="height: 112px;">' +
		'    <a href="combo.asp?id=' + comboID + '" class="images_block" target="_blank" onclick="window.open(\'combo.asp?id=' + comboID + '\', \'winorg\', \'width=610,height=780,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no\'); return false;">' +
		'      <img src="images/combos/icons_' + $strCombo + '.png" alt="" height="84" width="284" />' +
		'      <img src="images/combos/prix' + strMois + '_' + iComboPrix + '.png" alt="" height="62" width="99" />' +
		'      <img src="images/combos/rabais' + strMois + '_' + iComboRabais + '.png" alt="" height="84" width="108" />' +
		'    </a>' +
		'    <a href="combo-details.asp?id=' + comboID + '" class="images_block" target="_blank" onclick="window.open(\'combo-details.asp?id=' + comboID + '\', \'winorg\', \'width=620,height=780,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no\'); return false;">' +
		'      <img src="images/combos/b_notes_legales.png" alt="" height="28" width="157" />' +
		'    </a>' +
		'    <a href="combo.asp?id=' + comboID + '" class="images_block" target="_blank" onclick="window.open(\'combo.asp?id=' + comboID + '\', \'winorg\', \'width=610,height=720,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no\'); return false;">' +
		'      <img src="images/combos/b_savoir_plus.png" alt="" height="28" width="334" />' +
		'    </a>' +
		'  </div><div class="combo_details_bottom"></div></div>' +
		'</li>' +		
        '<br/>');
		
		// Display the combo box
		$combo
			.css({
				opacity: 0,
				height: 0
			})
			.animate(
				{opacity: 1, height: 154},
				700,
				"swing");
		$combos.prepend($combo);
	});
}

jQuery(function ($) {
	if (jQuery('.column1 object').size() !== 0) {
		$.ajax({
			url: 'xml/combos.xml',
			dataType: 'xml',
			success : function (xml) {
				window.xmlCombos = xml;
			},
			error : function (textStatus, errorThrown) {
//				console(errorThrown, textStatus);
			}
		});
	}
});
