dojo.require('dojo.fx');

var blkbryPop = 'http://shop.t-mobile.at/82032';
var shopLnk = 'http://shop.t-mobile.at/221254/1_2_3/10012/index.html';

var aktiveButton = 1;
var buttonKaufen = 'blkbryKaufen';
var popup = 'popup';

var startTarif = 2;
var maxTarife = 5;

var startMobile = 10;
var maxMobile = 2;

var animRun = 0;

var handyDetail = 'iphone16';
var openHandyDetail = '';

var aktivCol = null;

function changeButton() {
	/*var current = startTarif+startMobile;
	if(current > 20 && current < 30) {
		fadeOutElm(['blkbryVorMerken'],10);
		fadeInElm(['blkbryKaufen'],400);
	} else {
		fadeOutElm(['blkbryVorMerken'],10);
		fadeInElm(['blkbryKaufen'],400);
	}*/	
}

function gotToTarif(varTarif) {
	fadeOutElm(['popup'],400);
	dojo.style(dojo.byId('popup'), "display", "none");
	showText(varTarif);
	startTarif = varTarif;
}

function changePreis() {
	var zahlRahmen = dojo.byId('blkbryZahl');
	
	var toShow = startTarif+startMobile;
        changeButton();
	//console.log(toShow);
	switch(toShow) {
			case 11: // csa
				newImg  = '_images/betrag/16-csa.gif';
				shopLnk = 'http://shop.t-mobile.at/221217/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82032';
				popup = 'popup';
				handyDetail = 'iphone16';
				break;
			case 12: // cse
				newImg  = '_images/betrag/16-cse.gif';
				shopLnk = 'http://shop.t-mobile.at/221322/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82032';
				popup = 'popup';
				handyDetail = 'iphone16';
				break;	
			case 13: // ca
				newImg  = '_images/betrag/16-ca.gif';
				shopLnk = 'http://shop.t-mobile.at/221215/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82032';
				popup = 'popup';
				handyDetail = 'iphone16';
				break;
			case 14: // ceur
				newImg  = '_images/betrag/16-ceur.gif';
				shopLnk = 'http://shop.t-mobile.at/221216/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82032';
				popup = 'popup';
				handyDetail = 'iphone16';
				break;
			case 15: // easy
				newImg  = '_images/betrag/16-ceasy.gif';
				shopLnk = 'http://shop.t-mobile.at/221443/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82032';
				popup = 'popup';
				handyDetail = 'iphone16';
				break;	
			case 21: // csa
				newImg  = '_images/betrag/32-csa.gif';
				shopLnk = 'http://shop.t-mobile.at/221228/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82033';
				popup = 'popup';
				handyDetail = 'iphone32';
				break;
			case 22: // cse
				newImg  = '_images/betrag/32-cse.gif';
				shopLnk = 'http://shop.t-mobile.at/221229/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82033';
				popup = 'popup';
				handyDetail = 'iphone32';
				break;	
			case 23: // ca
				newImg  = '_images/betrag/32-ca.gif';
				shopLnk = 'http://shop.t-mobile.at/221226/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82033';
				popup = 'popup';
				handyDetail = 'iphone32';
				break;
			case 24: // ceur
				newImg  = '_images/betrag/32-ceur.gif';
				shopLnk = 'http://shop.t-mobile.at/221227/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82033';
				popup = 'popup';
				handyDetail = 'iphone32';
				break;
			case 25: // easy
				newImg  = '_images/betrag/32-ceasy.gif';
				shopLnk = 'http://shop.t-mobile.at/221335/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82032';
				popup = 'popup';
				handyDetail = 'iphone32';
				break;
			case 31: // csa
				newImg  = '_images/betrag/16-csa.gif';
				shopLnk = 'http://shop.t-mobile.at/221223/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82039';
				popup = 'popup';
				handyDetail = 'iphone16w';
				break;
			case 32: // cse
				newImg  = '_images/betrag/16-cse.gif';
				shopLnk = 'http://shop.t-mobile.at/221398/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82039';
				popup = 'popup';
				handyDetail = 'iphone16w';
				break;	
			case 33: // ca
				newImg  = '_images/betrag/16-ca.gif';
				shopLnk = 'http://shop.t-mobile.at/221220/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82039';
				popup = 'popup';
				handyDetail = 'iphone16w';
				break;
			case 34: // ceur
				newImg  = '_images/betrag/16-ceur.gif';
				shopLnk = 'http://shop.t-mobile.at/221221/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82039';
				popup = 'popup';
				handyDetail = 'iphone16w';
				break;
			case 35: // easy
				newImg  = '_images/betrag/16-ceasy.gif';
				shopLnk = 'http://shop.t-mobile.at/221443/1_2_3/10012/index.html';
				blkbryPop = 'http://shop.t-mobile.at/82039';
				popup = 'popup';
				handyDetail = 'iphone16w';
				break;	
				
		}
		
	if(toShow > 20)
		toShowBtn = 2;
	else
		toShowBtn = 1;
		
	//if(aktiveButton != 	toShowBtn) {
	//	changeButton();
	//}
	
	dojo.fadeOut({
				node: zahlRahmen,
				duration: 400,
				onEnd : function () {
					dojo.query('#blkbryZahl > img')[0].src = newImg;
					dojo.fadeIn({
								node: zahlRahmen,
								duration: 500
								}).play(250);
					animRun = 0;
				}
				}).play();
		
}

function fadeInElm(varArray, varDuration) {
	dojo.forEach(varArray, function (e) {
				dojo.fadeIn({
						beforeBegin: function() {
							dojo.style(dojo.byId(e), "opacity", 0);
							dojo.style(dojo.byId(e), "display", "block");
						},	
						node: dojo.byId(e),
						duration: varDuration
					}).play();
				});
}

function fadeOutElm(varArray, varDuration) {
	dojo.forEach(varArray, function (e) {
				dojo.fadeOut({ 
							 beforeBegin: function() {
									dojo.style(dojo.byId(e), "opacity", 1);
									//dojo.style(dojo.byId(e), "display", "none");
								},
							  node: dojo.byId(e),
							  duration: varDuration
						    }).play();
							            }
				);
}

/*function clickBlkbry() {
	if(dojo.isIE == '6')
		window.open(blkbryPop, 'Info', 'height=786,width=1024,resizable=yes,toolbar=yes,location=yes,status=yes,scrollbars=yes', '');
	else { 
		dojo.query('#detailOne > a')[0].href = blkbryPop;
	}
}*/
function clickBlkbry() {
	dojo.style(handyDetail, 'display', 'block');
	openHandyDetail = handyDetail;
}

function clickTarifTxt() { 
		fadeInElm([popup], 400);
}

function clickKaufen() {
	if(dojo.isIE == '6')
		window.open(shopLnk, 'Info', 'height=786,width=1024,resizable=yes,toolbar=yes,location=yes,status=yes,scrollbars=yes', '');
	else
		dojo.query('#'+buttonKaufen+' > a')[0].href= shopLnk;
}

function moveHandyLeft () {
	if(animRun == 0){
		animRun = 1;
		dojo.fadeOut({
					node: dojo.byId('handy'+startMobile),
					duration: 400,	
					onEnd : function () {
						startMobile -= 10;
						
						if(startMobile < 1)
							startMobile = (maxMobile*10);
						
						dojo.fadeIn({
									node: dojo.byId('handy'+startMobile),
									duration: 500
									}).play(250);
						changePreis();
					}
					}).play();
	}
}

function moveHandyRight () {
	if(animRun == 0){
		animRun = 1;

		dojo.fadeOut({
					node: dojo.byId('handy'+startMobile),
					duration: 400,
					onEnd : function () {
						startMobile += 10;
						
						if(startMobile > (maxMobile*10))
							startMobile = 10;
						dojo.fadeIn({
									node: dojo.byId('handy'+startMobile),
									duration: 500
									}).play(250);
						changePreis();
						}
					}).play();
	}
}

function showText(newTarif) {
	if(animRun == 0){
		animRun = 1;
		dojo.fadeOut({
					node: dojo.byId('blkbryText'+startTarif),
					duration: 400,	
					onEnd : function () {						
						dojo.fadeIn({
									node: dojo.byId('blkbryText'+newTarif),
									duration: 500
									}).play(250);
						changePreis();
					}
					}).play();
	}
}


function moveTextLeft() {
	if(animRun == 0){
		animRun = 1;
		dojo.fadeOut({
					node: dojo.byId('blkbryText'+startTarif),
					duration: 400,	
					onEnd : function () {
						startTarif -= 1;
						if(startTarif == 3 || startTarif == 4)
                                                      startTarif = 2;
						if(startTarif < 1)
						    startTarif = maxTarife;
						dojo.fadeIn({
									node: dojo.byId('blkbryText'+startTarif),
									duration: 500
									}).play(250);
						changePreis();
					}
					}).play();
	}
}

function moveTextRight() {
	if(animRun == 0){
		animRun = 1;
		
		dojo.fadeOut({
					node: dojo.byId('blkbryText'+startTarif),
					duration: 400,
					onEnd : function () {
						startTarif += 1;
						if(startTarif == 3 || startTarif == 4)
                                                      startTarif = 5;
						if(startTarif > maxTarife)
							startTarif = 1;
						dojo.fadeIn({
									node: dojo.byId('blkbryText'+startTarif),
									duration: 500
									}).play(250);	
						changePreis();
					}
					}).play();
	}
}

function closeHandyDetailLayer () {
	dojo.style(openHandyDetail, 'display', 'none');
	}

dojo.addOnLoad(function () {
	dojo.query('#'+buttonKaufen+' > a')[0].href= shopLnk;
	//dojo.query('#detailOne > a')[0].href = blkbryPop;
	var preImg = new Array();	
	
	fadeOutElm(['blkbryText1','blkbryText3','blkbryText4','blkbryText5','handy20','handy30','blkbryVorMerken','detilsLinks','blkbryBtnTwo', 'blkbryBtnOne'],  10);

	
	fadeInElm(['blkbryHolder', 'blkbryTextHolder'], 500);
	fadeInElm(['blkbryPlus', 'blkbryGleich', 'blkbryZahl'], 800);
	fadeInElm(['detilsLinks','blkbryBtnTwo', 'blkbryBtnOne'],1000);
	
	dojo.forEach(['_images/tarifOne.gif','_images/tarifTwo.gif','_images/tarifThree.gif','_images/tarifFour.gif','_images/tarifFive.gif','_images/tarifSix.gif'],
				 function(e,i) { 
						preImg[i] = new Image(); 
						preImg[i].src = e;
						}
					);
	
	dojo.connect(dojo.byId('detailOne'), 'click', clickBlkbry);																							  
	dojo.connect(dojo.byId('detailTwo'), 'click', clickTarifTxt);
	
	dojo.connect(dojo.byId('blkbryKaufen'), 'click', clickKaufen);
	dojo.connect(dojo.byId('blkbryVorMerken'), 'click', clickKaufen);
	
	
	dojo.connect(dojo.byId('blkbryBtn2left'), 'click', moveTextLeft);
	dojo.connect(dojo.byId('blkbryBtn2right'), 'click', moveTextRight);
	
	dojo.connect(dojo.byId('blkbryBtn1left'), 'click', moveHandyLeft);
	dojo.connect(dojo.byId('blkbryBtn1right'), 'click', moveHandyRight);
	
	dojo.connect(dojo.byId('blkbryCloseBtn'), 'click', function () { fadeOutElm(['popup'], 500); dojo.style(dojo.byId('popup'), "display", "none");});
	//dojo.connect(dojo.byId('addInfoClose'), 'click', function () { fadeOutElm(['popupAlternative'], 500); dojo.style(dojo.byId('popupAlternative'), "display", "none");});
	dojo.connect(dojo.byId('popupIPONEINFO'), 'click', function () { fadeOutElm(['popupIPONEINFO'], 500); dojo.style(dojo.byId('popupIPONEINFO'), "display", "none");});
	dojo.connect(dojo.byId('apPTechDetails'), 'click', function (ev) { dojo.stopEvent(ev); fadeInElm(['popupIPONEINFO'], 500);});

dojo.connect(dojo.query('.tarifTableHolder')[0], 'onmouseout', function () { 
																	if(aktivCol) {
																		dojo.removeClass(aktivCol, 'pinkBorder');
																	}
																});
	
	dojo.connect(dojo.query('.titelCol')[0], 'onmousemove', function () {
														if(aktivCol) {
															dojo.removeClass(aktivCol, 'pinkBorder');
														}			 
													});
	
	dojo.query('.mouseOverCol').forEach(function (e){
		dojo.connect(e, 'onmousemove', function () {
										if(aktivCol) {
											if(e.id != 	aktivCol.id)  {
												dojo.removeClass(aktivCol, 'pinkBorder');
											}
										}	   
										aktivCol = e;
										dojo.addClass(e, 'pinkBorder');
									});
	});
	
});