window.addEvent("domready",_dietcokeInit);

function getTheQuery()
{
	var pageidRegexp=new RegExp(".*?(pageid=)","gi");
	var pageidRegexp2=new RegExp("\&.*","gi");
	var loc = window.location.search;
	var pageid=loc.replace(pageidRegexp,"");
	var pageid=pageid.replace(pageidRegexp2,"");
	if(pageid) 
	{
	  var launchPage=DietCoke.pages[pageid];
	  if(launchPage)DietCoke.showWindow(launchPage.window,null,pageid);
	}
}

function _dietcokeInit(){
	DietCoke.init();
}
DietCoke = {
	loggedIn: false,
	flash:null,
	contextPath:"/",
	loginListeners: new Array(),
	alertMessages:"",
	
	init: function(){
		Dialog.overlayAlpha = 0;
		var myThis = this;
		if (SlidePane) {
			slidepane = new SlidePane();
			this.switchSlidePane("loggedOutPanel");
		}
		getTheQuery(); // Call the page dialog prompt
	},
	
	// SlidePane stuff
	switchSlidePane: function(to){
		var div = Extensions.getPrebuilt(to)
		if(div)slidepane.setContents(div);
	},
	
	// Dialog box stuff
	skins: {
		whiteBox:{
			width:565,
			header:"whiteHeader",
			body:"whiteBody",
			footer:"whiteFooter",
			closeButton:"whiteCloseButton",
			titleClass:"whiteTitle",
			frameClass:"whiteFrame"
		},
		whiteBox_wide:{
			width:748,
			header:"whiteHeader_wide",
			body:"whiteBody_wide",
			footer:"whiteFooter_wide",
			closeButton:"whiteCloseButton_wide",
			titleClass:"whiteTitle",
			frameClass:"whiteFrame_wide"
		}
	},
	windows: {
		registration:{
			defaultPage:"rego1",
			skin:"whiteBox",
			height:600
		},
		info:{
			defaultPage:"privacyPolicy",
			skin:"whiteBox",
			height:600
		},
		alert:{
			defaultPage:"passwordReminder",
			skin:"whiteBox",
			height:350
		},
		aboutCoke:{
			defaultPage:"aboutUs",
			skin:"whiteBox_wide",
			height:600
		},
		tellAFriend:{
			defaultPage:"tellAFriend",
			skin:"whiteBox",
			height:600
		},
		noFlash:{
			defaultPage:"noFlash",
			skin:"whiteBox",
			height:400
		}
	},
	pages: {
		rego1:{
			title:"Sign me up",
			url:"html/dialog_pages/signmeup_step1.htm",
			window:"registration",
			showClose:true,
			includeQueryString:false
		},
		regoValidate:{
			title:"Sign me up",
			url:"html/dialog_pages/signmeup_step2.htm",
			window:"registration",
			showClose:true,
			includeQueryString:false
		},
		regoBarCode:{
			title:"Sign me up",
			url:"html/dialog_pages/signmeup_barcode.jsp",
			window:"registration",
			showClose:true,
			includeQueryString:false
		},
		regoComplete:{
			title:"Sign me up",
			url:"html/dialog_pages/signmeup_complete.htm",
			window:"registration",
			showClose:true,
			includeQueryString:false
		},
		regoFooty:{
			title:"Sign me up",
			url:"html/dialog_pages/signmeup_footy.htm",
			window:"registration",
			showClose:true,
			includeQueryString:false
		},
		editProfile:{
			title:"Edit Profile",
			url:"html/dialog_pages/edit_profile.htm",
			window:"registration",
			showClose:true,
			includeQueryString:false
		},
		welcomeEditProfile:{
			title:"Welcome to the Coke Side of Life",
			url:"html/dialog_pages/welcome_edit_profile.htm",
			window:"registration",
			showClose:true,
			includeQueryString:false
		},
		privacyPolicy:{
			title:"Privacy Policy",
			url:"html/dialog_pages/privacy_policy.htm",
			window:"info",
			showClose:true,
			includeQueryString:false
		},
		termsOfUse:{
			title:"Terms of Use",
			url:"html/dialog_pages/terms_of_use.htm",
			window:"info",
			showClose:true,
			includeQueryString:false
		},
		contactUs:{
			title:"Contact Us",
			url:"html/dialog_pages/contact_us.htm",
			window:"info",
			showClose:true,
			includeQueryString:false
		},
		contactUsThanks:{
			title:"Contact Us",
			url:"html/dialog_pages/contact_us_thanks.htm",
			window:"info",
			showClose:true,
			includeQueryString:false
		},
		faqs:{
			title:"Frequently Asked Questions",
			url:"html/dialog_pages/faqs.htm",
			window:"info",
			showClose:true,
			includeQueryString:false
		},
		unclaimedPrizes:{
			title:"Prize Winners",
			url:"html/dialog_pages/unclaimed_prizes.htm",
			window:"info",
			showClose:true,
			includeQueryString:false
		},
		passwordReminder:{
			title:"Password Reminder",
			url:"html/dialog_pages/password_reminder.htm",
			window:"alert",
			showClose:true,
			includeQueryString:false
		},
		notLoggedIn:{
			title:"Log in or register",
			url:"html/dialog_pages/not_logged_in.htm",
			window:"alert",
			showClose:true,
			includeQueryString:false
		},
		notLoggedInGallery:{
			title:"Log in or register",
			url:"html/dialog_pages/not_logged_in.htm",
			window:"alert",
			showClose:true,
			includeQueryString:false
		},
		aboutUs:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_us.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutAdvert:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_advert.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutBrands:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_brands.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutCokeZero: {
			title:"About Coca-Cola Zero",
			url:"html/dialog_pages/about_cokezero.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutContour:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_contour.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutDietary:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_dietary.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutFacts:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_facts.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutFaq:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_faq.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutMade:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_made.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutOlympics:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_olympics.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutOrigins:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_origins.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		aboutSanta:{
			title:"About Coca-Cola",
			url:"html/dialog_pages/about_santa.htm",
			window:"aboutCoke",
			showClose:true,
			includeQueryString:false
		},
		tellAFriend:{
			title:"Tell a Friend",
			url:"html/dialog_pages/tell_a_friend.htm",
			window:"tellAFriend",
			showClose:true,
			includeQueryString:false
		},
		tellAFriendThanks:{
			title:"Tell a Friend",
			url:"html/dialog_pages/tell_a_friend_thanks.htm",
			window:"tellAFriend",
			showClose:true,
			includeQueryString:false
		},
		resetPassword:{
			title:"Reset Password",
			url:"html/dialog_pages/reset_password.sm",
			window:"alert",
			showClose:true,
			includeQueryString:true
		},
		resetPasswordThanks:{
			title:"Reset Password",
			url:"html/dialog_pages/reset_password_thanks.htm",
			window:"alert",
			showClose:true,
			includeQueryString:false
		},
		noFlash:{
			title:"Update Flash Player",
			url:"html/dialog_pages/noflash.htm",
			window:"noFlash",
			showClose:false,
			includeQueryString:false
		},
		alert:{
			title:"Diet Coke",
			url:"html/dialog_pages/alert.htm",
			window:"alert",
			showClose:true,
			includeQueryString:false
		},
		enteredCompetition:{
			title:"Thanks for your entry",
			url:"html/dialog_pages/alert.htm",
			window:"alert",
			showClose:true,
			includeQueryString:false
		}
	},
	showWindow: function(windowId, callback, page){
		var queryString = location.search;

		var window = this.windows[windowId];
		var page = this.pages[(page)?page:window.defaultPage];
		if(window && page){
			var skin = this.skins[window.skin];
			if(skin){
				var url = page.url;
				if( page.includeQueryString ) {
					url = url + queryString;
				}

				showStyledDialog(windowId, page.title, this.contextPath + url, page.showClose, window.height, skin.width, callback, skin.header, skin.body, skin.footer, skin.frameClass, skin.titleClass, skin.closeButton);
				return false;
			}
		}
		return true;
	},
	hideWindow: function(windowId) {
		var window = getDialog(windowId);
		window.hide();
	},
	showPage: function(pageId){
		var page = this.pages[pageId];
		if(page){
			var window = getDialog(page.window);
			if(window){
				var url = page.url;
				if( page.includeQueryString ) {
					url = url + location.search;
				}
				window.show(this.contextPath + url, page.title, page.showClose);
			}else{
				var ret = this.showWindow(page.window, null, pageId);
			}
			return false;
		}
		return true;
	},

	showAlert: function(messages) {
		this.alertMessages = messages;
		this.showPage('alert');
	},

	showEnteredCompetition: function(messages) {
		this.alertMessages = messages;
		this.showPage('enteredCompetition');
	},
	
	handleLoggedIn: function(loggedIn, interactive) {
		$$('.loginRight').setStyle("display", loggedIn ? "none" : "");
		
		for (var i = 0; i < this.loginListeners.length; i++) {
			this.loginListeners[i](loggedIn, interactive);
		}
	},
	
	reloadComp: function() {
		var me = this;
		new Ajax(this.contextPath + "filmcomp.sm", {
			update: $("contest_box"),
			method: "get",
			onComplete: function() {
				DietCoke.applySIFR();
			},
			onFailure: function() {
				/* This particular request fails when called by validate during the registration, so we
				retry it later. Something strange happens with Javascript when those popups are open. */
				setTimeout(function() {
					me.reloadComp();
				}, 2000);
			}
		}).request();
	},
	
	reloadDetails: function() {
		new Ajax(this.contextPath + "detailscomp.sm", {
			update: $("competition_form"),
			method: "get",
			onComplete: function() {
				DietCoke.applySIFR();
			}
		}).request();
	},
	
	reloadPrizes: function() {
		new Ajax(this.contextPath + "prizescomp.sm", {
			update: $("competition_form"),
			method: "get",
			onComplete: function() {
				DietCoke.applySIFR();
			}
		}).request();
	},
	
	reloadRateFilm: function(filmId) {
		new Ajax(this.contextPath + "rate/default," + filmId + ".sm?r=" + new Date().getTime(), {
			update: $("rateVideo"),
			method: "get",
			onComplete: function() {
				DietCoke.applySIFR();
			}
		}).request();
	},
	
	rateFilm: function(filmId, rating) {
		if (CSOL.loggedIn) {
			new Ajax(this.contextPath + "ratefilm," + filmId + ".do?value=" + rating, {
				update: $("rateVideo"),
				method: "get",
				onComplete: function() {
					DietCoke.applySIFR();
				}
			}).request();
		}
		else {
			setTimeout(function() {
				DietCoke.showPage('notLoggedIn');
			}, 1000);
		}
	},
	rateFilmOver: function(rating, imageName) {
		var label = $("rateVideoLabel");
		var result;
		switch(rating) {
			case 1:
				result = "Flat (1)";
			break
			case 2:
				result = "Lacks Fizz (2)";
			break
			case 3:
				result = "Quenching (3)";
			break
			case 4:
				result = "Refreshing (4)";
			break
			case 5:
				result = "Sparkling (5)";
			break
		}
		label.innerHTML = result;
		
		if (imageName != null) {
			document[imageName].src = this.contextPath + "images/rating_bottle.gif";
		}
	},
	rateFilmOut: function(imageName, rating) {
		var label = $("rateVideoLabel");
		var result = "";
		switch(rating) {
			case 1:
				result = "Flat (1)";
			break
			case 2:
				result = "Lacks Fizz (2)";
			break
			case 3:
				result = "Quenching (3)";
			break
			case 4:
				result = "Refreshing (4)";
			break
			case 5:
				result = "Sparkling (5)";
			break
		}
		var result = "Avg Rating: " + result;
		label.innerHTML = result;
		
		if (imageName != null) {
			document[imageName].src = this.contextPath + "images/rating_bottle_empty.gif";
		}
	},
	commentsPage: function(filmId, start) {
		new Ajax(this.contextPath + "comments/default," + filmId + ".sm?start=" + start, {
			update: $("comments"),
			method: "get"
		}).request();
	},
	genrePage: function(archive, filmId, start) {
		new Ajax(this.contextPath + "genre/default," + filmId + ".sm?start=" + start + "&archive=" + archive, {
			update: $("genres"),
			method: "get",
			onComplete: function() {
				DietCoke.applySIFR();
			}
		}).request();
	},	
	keepMePosted: function() {
		if (CSOL.loggedIn) {
			DietCoke.showPage('editProfile');
		}
		else {
			DietCoke.showPage('rego1');
		}
	},
	applySIFR: function() {
		if(typeof sIFR == "function"){
			sIFR.replaceElement(named({sSelector:"span.futuraText", sFlashSrc:this.contextPath + "swf/futura.swf", sColor:"#555555", sLinkColor:"#555555", sBgColor:"#FFFFFF", sHoverColor:"#555555", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0", sWmode:"transparent"}));
		}
	}
}
CSOL = DietCoke;