// Lisa adviseur javascript
// @author: Lucien Immink
var defaultImg = null;
var isOnShop = false;
var delayTimer = 0;
$(document).ready(function() {
	defaultImg = $("#lisaimg img").attr("src");
	$("#lisaadviseur").click(function() {
		// are we on a shop page?
		if ($(".shopnewcontainer")[0] != undefined) {
			isOnShop = true;	
		}
		// start by replacing the img of lisa with the animated flash lisa beginning with the first question
		if (!isOnShop) {
			$("#lisaAdvice").media({
				src: '/rabomobiel/static/flash/lisa.swf?aid=' + aid + '&flv=flv/start.flv&isOnShop='+ isOnShop,
				width: 500,
				height: 340,
				bgColor: 'transparent',
				attrs: { wmode: 'transparent', allowScriptAccess: 'sameDomain' }
			});
		} else {
			$("#lisaAdvice").media({
				src: '/rabomobiel/static/flash/lisaShop.swf?aid=' + aid + '&flv=flv/start.flv&isOnShop='+ isOnShop,
				width: 800,
				height: 193,
				bgColor: 'transparent',
				attrs: { wmode: 'transparent', allowScriptAccess: 'sameDomain' }
			});
		}
		if (isOnShop) {
			//$("#lisaimg").css("position", "absolute");
			//$("#lisaimg").css("margin", "-28px 0 0 105px");
			$("#lisaAdvice object, #lisaAdvice embed").attr("id","lisaFlash");
			$("#lisaAdvice").css("top",$("#lisaimg").offset().top - 135);
			$("#lisaAdvice").css("left",$("#lisaimg").offset().left - 10);
			if ($.browser.msie && $.browser.version < 7 ) {
				$("#lisaAdvice").css("top",$("#lisaimg").offset().top - 135);
			$("#lisaAdvice").css("left",$("#lisaimg").offset().left + 120);
			}
			//$(".shoptop").css("opacity", 0.3);
			if ($(".lisaShopOverlay")[0] == undefined) {
				var divElem = function () {
					return [
						'div', { className: 'lisaShopOverlay'}
					];
				};
				$("#content").tplPrepend('', divElem);
			}
			$(".lisaShopOverlay").show();
			$(".lisaShopOverlay").css("opacity", 0.7);
			$("#lisaimg").hide();
		} else {
			// dynamicly set the position relative to the position of the img.
			$("#lisaAdvice").css("top",$("#lisaimg").offset().top - 191);
			$("#lisaAdvice").css("left",$("#lisaimg").offset().left - 401);
			$("#lisaimg img").attr("src","/rabomobiel/static/images/lisa_big.jpg");
		}
		// update the image so it reflects the new size
		//
		return false;
	});
});
function closeLisa() {
	// undo all we did
	$("#lisaAdvice").html("");
	$("#lisaimg img").attr("src",defaultImg);
	$("#lisaimg").show();
	if (isOnShop) {
		$("#lisaimg").css("position", "relative");
		$("#lisaimg").css("margin", "0");
		$(".shoptop").css("opacity", 1);
		$(".lisaShopOverlay").hide();
	}
}
function debug(message) {
	if (window.console) console.log(message);	
}
function updateSubscription(abb,amount,subscriptionlength,added) {
	if (abb == "rm") {
		abb = "RM";
		shop.selectedSubscription = abb+"-"+amount+"-"+subscriptionlength;	
	} else if (abb == "bundelvrij") {
		abb = "BB";
		shop.selectedSubscription = abb+"-"+subscriptionlength;	
	} else if (abb == "prepaid") {
		abb = "PREPAID";
		shop.selectedSubscription = abb;	
	}
	if (Number(added) >= 0) {
		shop.maxPrice = Number(added);
	} else {
		shop.maxPrice = null;	
	}
	shop.start();
	// update the tabs
	var tab = 0;
	if (abb =='BB') {
		tab = 2;
	} else if (Number(subscriptionlength) == 12) {
		tab = 1
	} else if (abb == "PREPAID") {
		tab = 3;
    }
    $(".subscriptiontabs > ul").tabs('select', tab);
}
function isEmptyShop() {
	// wait untill the shop is ready plz
	delayTimer = setTimeout("checkShop()", 50);
}
function getFlashMovie(movieName) {
  	var isIE = navigator.appName.indexOf("Microsoft") != -1;
  	return (isIE) ? window[movieName] : document[movieName];
}
function checkShop() {
	if (delayTimer) {
		clearTimeout(delayTimer);	
	}
	if (shop.isReady) {
		if ($(".nophones").css("display") == "block") {
			getFlashMovie("lisaFlash").sendToActionscript("true");
		} else {
			getFlashMovie("lisaFlash").sendToActionscript("false");	
		}
	} else {
		// not ready? let's wait
		delayTimer = setTimeout("checkShop()", 50);
	}
}