﻿/// <reference path="jquery-1.3.2.min.js"/>

OWL.eventState = OWL.eventState || {};

(function() {

	var that = OWL.eventState;

	var thatObj = {
		startStateCheck: function(currentStateTypeID, currentEventStateID, eventID, eventAffiliateID, checkInterval) {
			//currentStateTypeID - id of state when page loaded
			//eventID - id of event to check
			//eventAffiliateID - id of event affiliate
			//checkInterval - time between checks

			var newStateTypeID = currentStateTypeID;
			var newEventStateID = currentEventStateID;

			//'mainBody' - id of div

			//check state
			//has it changed
			//	yes
			//		get new style
			//		get new mainBody
			//
			//start timer to check again

			//alert(checkInterval);

			/*/
			var changeoutFunction = function() {
			$.ajax({
			type: "get",
			dataType: "html",
			url: '/Concert/Main/' + eventAffiliateID,
			cache: false,
			success: function(result) {

						if (navigator.appName.indexOf("Microsoft") != -1) {
			//ie doesn't know how to function correctly so just reload the whole page
			location.reload();
			}
			else {
			//set main body contents
			$('#mainBody')
			.empty()//clear contents
			.append(result)//set contents
			;

							//we got the main body, now set the css
			var eventAffiliateCss = document.getElementById('eventAffiliateCss');
			if (eventAffiliateCss) {
			//add random number at the end so the browser will refresh
			eventAffiliateCss.href = '/Concert/GetEventAffiliateCss/' + eventAffiliateID + '?rnd=' + Math.random();
			}

							currentStateTypeID = newStateTypeID;

							OWL.styler.rotateAds(eventAffiliateID);

							//restart timeout
			setTimeout(checkStateFunction, checkInterval);
			}
			},
			error: function(xhr, textStatus, errorThrown) {
			//show error
			//OWL.helpers.defaultErrorCallback(xhr, textStatus, errorThrown);
			//there was an error try again, but with the same timeout length
			//	since we know the state type has changed
			setTimeout(checkStateFunction, checkInterval);
			}
			});
			}
			//*/
			var checkStateFunction = function() {
				$.ajax({
					type: "get",
					dataType: "json",
					url: '/Concert/CheckState/' + eventID,
					cache: false,
					success: function(result) {

						newStateTypeID = result.EventStateTypeID;
						newEventStateID = result.EventStateID;
						checkInterval = result.CheckInterval;
						//alert(newStateTypeID);
						//alert(newEventStateID);
						//alert(checkInterval);

						if (currentStateTypeID !== newStateTypeID
							|| currentEventStateID !== newEventStateID) {
							//state type or event state id is different
							//change out main body and new style

							//reload the page, don't change out main body
							location.reload();
							//changeoutFunction();
						}
						else {
							//they're the same start timeout over
							setTimeout(checkStateFunction, checkInterval);
						}
					},
					error: function(xhr, textStatus, errorThrown) {
						//show error
						//OWL.helpers.defaultErrorCallback(xhr, textStatus, errorThrown);
						//there was an error try again with longer timeout
						setTimeout(checkStateFunction, checkInterval * 2);
					}
				});
			};

			//start timeout for the first time
			setTimeout(checkStateFunction, checkInterval);
		},
		lastFunction: function() { }
	};

	OWL.lang.augmentObject(that, thatObj, true);
})();
