﻿

Event.observe(window, "load", OnDocumentLoad);
Event.observe(window, "unload", OnDocumentUnload);

function OnDocumentUnload ( evObj )
{
}

function OnDocumentLoad ( evObj )
{
    FooterLoad();
    //MainLoad(); // todo: remove
}

function MainLoad()
{
    if ($("PageUrlTracker")) {
        pageTracker._trackPageview($("PageUrlTracker").down("a").href);
    }

    if ($("EventTracker") && $("EventTracker").down("li").innerHTML != "") {
        pageTracker._trackEvent($("EventTracker").childElements()[0].innerHTML,
            $("EventTracker").childElements()[1].innerHTML,
            $("EventTracker").childElements()[2].innerHTML);
    }

	new NavigationExtender($("Navigation").down("ul"));
	new TOP10PanelPaging($("Top10"), $("Content"));
	new MP3LinkExtender();
	
	if ( $("VideoList") )
	    new VideoNavigationExtender($("VideoList"));

	
	$("Newsletter").select("input.text").each(
		function(el)
		{
			new InputTextLabel(el);
		}
	);
	
	if ( $("ContactDetail") )
	    $("ContactDetail").select("input.text").each(
		    function(el)
		    {
			    new InputTextLabel(el);
		    }
	    );

	if ( $("ContactDetail") )
	    $("ContactDetail").select("textarea").each(
		    function(el)
		    {
			    new InputTextLabel(el);
		    }
	    );

	if ( $("Booking") )
	    $("Booking").select("input.text").each(
		    function(el)
		    {
			    new InputTextLabel(el);
		    }
	    );

	if ( $("Booking") )
	    $("Booking").select("textarea").each(
		    function(el)
		    {
			    new InputTextLabel(el);
		    }
	    );

	if ( $("PersonalInfo") )
	    $("PersonalInfo").select("input.text").each(
		    function(el)
		    {
			    new InputTextLabel(el);
		    }
	    );

	if ( $("PersonalInfo") )
	    $("PersonalInfo").select("textarea").each(
		    function(el)
		    {
			    new InputTextLabel(el);
		    }
	    );
	
	new NewsletterPanelExtender($("Newsletter"));
	    
}

function FooterLoad ()
{
	new BannerRotationExtender($("Partner"));
}

/********** TOP10PanelPaging **********/
var TOP10PanelPaging = Class.create();
TOP10PanelPaging.prototype = 
{
	initialize : function (panel, comparePanel)
	{
		this.FirstPageClick = this.OnFirstPageClick.bindAsEventListener(this);
		this.SecondPageClick = this.OnSecondPageClick.bindAsEventListener(this);
		
		this.panel = $(panel);
		this.comparePanel = $(comparePanel);
		this.pagingNav = this.panel.down("#Top10Paging");
		
		if ( this.comparePanel.getHeight() < this.panel.getHeight() )
		{
		    this.ShowItems (1, 5);
		    this.pagingNav.setStyle({display: "block"});
		    Event.observe(this.pagingNav.down(".first"), "click", this.FirstPageClick);
		    Event.observe(this.pagingNav.down(".second"), "click", this.SecondPageClick);
		}
		
		Effect.Appear(this.panel.down("ul"), {duration: 1});
	},
	
	ShowItems : function (from, to)
	{
	    var index = 1;
        this.panel.select("li").each( function(el) 
        {
            el.setStyle({display:  (index >= from && index <= to ) ? "block" : "none"});
            ++index;
        }, this);
	},

	OnFirstPageClick : function (evObj)
	{
        this.ShowItems (1, 5);
	},
	
	OnSecondPageClick : function (evObj)
	{
        this.ShowItems (6, 10);
	}
}
/********** TOP10PanelPaging **********/

/********** MP3LINK EXTENDER **********/
var MP3LinkExtender = Class.create();
MP3LinkExtender.prototype =
{
    initialize: function() {
        this.LinkClick = this.OnLinkClick.bindAsEventListener(this);
        this.LinkOver = this.OnLinkOver.bindAsEventListener(this);
        this.LinkOut = this.OnLinkOut.bindAsEventListener(this);

        var hrefs = $("Page").select("a.mp3");
        for (var i = 0; i < hrefs.length; ++i) {
            hrefs[i].songURL = hrefs[i].href;

            hrefs[i].writeAttribute("onclick", "void(0);return false;");
            hrefs[i].href = "javascript:void(0);";

            Event.observe(hrefs[i], "click", this.LinkClick);
            Event.observe(hrefs[i], "mouseover", this.LinkOver);
            Event.observe(hrefs[i], "mouseout", this.LinkOut);
        }
    },

    OnLinkClick: function(evObj) {
        var evEle = Event.element(evObj);
        var link = null;
        link = (evEle.tagName == "a" && evEle.className == "mp3") ? evEle : link;
        link = (evEle.up("a.mp3")) ? evEle.up("a.mp3") : link;
        link = (evEle.up("li") && evEle.up("li").down("a.mp3")) ? evEle.up("li").down("a.mp3") : link;

        pageTracker._trackPageview(link.songURL);

        pageTracker._trackEvent("MP3", "Played", link.songURL);

        GetFlashMovieObject("MP3Player").PlayMP3(link.songURL);

        $("Footer").scrollTo();

        return false;
    },

    OnLinkOver: function(evObj) {
        var evEle = Event.element(evObj);
        var btnImg = null;
        btnImg = (evEle.up("a.mp3") && evEle.up("a.mp3").down("img.mp3")) ? evEle.up("a.mp3").down("img.mp3") : btnImg;
        btnImg = (evEle.down("img.mp3")) ? evEle.down("img.mp3") : btnImg;
        btnImg = (evEle.up("li") && evEle.up("li").down("img.mp3")) ? evEle.up("li").down("img.mp3") : btnImg;

        btnImg.src = btnImg.src.replace("-inactive", "-active");
    },

    OnLinkOut: function(evObj) {
        var evEle = Event.element(evObj);
        var btnImg = null;
        btnImg = (evEle.up("a.mp3") && evEle.up("a.mp3").down("img.mp3")) ? evEle.up("a.mp3").down("img.mp3") : btnImg;
        btnImg = (evEle.down("img.mp3")) ? evEle.down("img.mp3") : btnImg;
        btnImg = (evEle.up("li") && evEle.up("li").down("img.mp3")) ? evEle.up("li").down("img.mp3") : btnImg;

        btnImg.src = btnImg.src.replace("-active", "-inactive");
    }
}
/********** MP3LINK EXTENDER **********/


/********** NAVIGATION EXTENDER **********/
var NavigationExtender = Class.create();
NavigationExtender.prototype = 
{
	initialize : function (navigation)
	{
		this.navigation = $(navigation);
		this.ElementOver = this.OnElementOver.bindAsEventListener(this);
		this.ElementOut = this.OnElementOut.bindAsEventListener(this);

		var hrefs = this.navigation.getElementsByTagName("a");
		for ( var i = 0; i < hrefs.length; ++i )
		{
			Event.observe(hrefs[i], "mouseover", this.ElementOver);
			Event.observe(hrefs[i], "mouseout", this.ElementOut);
		}
	},

	OnElementOver : function (evObj)
	{
		Event.element(evObj).up("li").down("img").src = Event.element(evObj).up("li").down("img").src.replace("-inactive", "-active");
		Event.element(evObj).up("li").className = "over";
	},
	
	OnElementOut : function (evObj)
	{
		Event.element(evObj).up("li").down("img").src = Event.element(evObj).up("li").down("img").src.replace("-active", "-inactive");
		Event.element(evObj).up("li").className = "";
	}
}
/********** NAVIGATION EXTENDER **********/

/********** NEWSLETTER PANEL EXTENDER **********/
var NewsletterPanelExtender = Class.create();
NewsletterPanelExtender.prototype = 
{
	initialize : function (panel)
	{
		this.panel = $(panel);
		this.visible = false;
		this.LinkClick = this.OnLinkClick.bindAsEventListener(this);
		
		Event.observe(this.panel.down("legend"), "click", this.LinkClick);

		this.panel.select("p").each( function(el) 
		{ 
		    Effect.Fade(el, { duration: 1});
		}, this);
	},

	OnLinkClick : function (evObj)
	{
		this.panel.select("p").each( function(el) 
		{ 
		    if (this.visible) 
		        Effect.Fade(el, { duration: 1.0 });
		    else
		        Effect.Appear(el, { duration: 1.0 });
		}, this);
		this.visible = !this.visible;
	}
}
/********** NEWSLETTER PANEL EXTENDER **********/

/********** BANNER ROTATION EXTENDER **********/
var BannerRotationExtender = Class.create();
BannerRotationExtender.prototype = 
{
	initialize : function (panel)
	{
		this.panel = $(panel).down("ul");
		
		this.Timer = this.OnTimer.bindAsEventListener(this);
		this.AnimationFinished = this.OnAnimationFinished.bindAsEventListener(this);
		
		if ( this.panel.down("li") )
		    new PeriodicalExecuter(this.Timer, 5); 
	},
	
	OnTimer : function (evObj)
	{
	    fadedObject = this.panel.down("li");
	    this.panel.appendChild(fadedObject);
	    //new Effect.Move(this.panel, { x: -89, y: 0, mode: 'relative', afterFinish:this.AnimationFinished });
	},

	
	OnAnimationFinished : function (evObj)
	{
	    fadedObject = this.panel.down("li");
	    fadedObject.remove();
	    this.panel.setStyle({left: "0px"});
	}
}
/********** BANNER ROTATION EXTENDER **********/

/********** VIDEONAVIGATION EXTENDER **********/
var VideoNavigationExtender = Class.create();
VideoNavigationExtender.prototype = 
{
	initialize : function (navigation)
	{
		this.ItemClick = this.OnItemClick.bindAsEventListener(this);
		
		this.navigation = $(navigation);
		this.cookieStore = new CookieJar({expires:3600});

		var items = this.navigation.select("li div.category");
		for ( var i = 0; i < items.length; ++i )
		{
            if ( this.cookieStore.get("v_category") == items[i].innerHTML )
                items[i].up("li").className = "selected";
                
		    Event.observe(items[i], "click", this.ItemClick);
		}
	},

	OnItemClick : function (evObj)
	{
	    var evEle = Event.element(evObj);
	    
	    if ( this.navigation.down("li.selected") )
		    this.navigation.down("li.selected").className = "";
		
		evEle.up("li").className = "selected";
		
		this.cookieStore.put("v_category", evEle.innerHTML);
	}
}
/********** VIDEONAVIGATION EXTENDER **********/

/********** INPUT TEXTLABEL **********/
var InputTextLabel = Class.create();
InputTextLabel.prototype = 
{
    initialize : function ( input )
    {
        this.FocusFound = this.OnFocusFound.bindAsEventListener(this);
        this.FocusLost = this.OnFocusLost.bindAsEventListener(this);
        
        this.input = $(input);
        this.textLabel = $F(input);
        
        Event.observe(this.input, "focus", this.FocusFound);
        Event.observe(this.input, "blur", this.FocusLost);
    },
    
    OnFocusFound : function ( evObj )
    {
        if ( this.input.getValue() == this.textLabel )
            this.input.clear();
        this.input.select();
    },
    
    OnFocusLost : function ( evObj )
    {
        if ( this.input.getValue() == "" )
            this.input.value = this.textLabel;
    }
}
/********** INPUT TEXTLABEL **********/

function GetFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}




/********** COOKIE LIB **********/
/**
 * Javascript code to store data as JSON strings in cookies. 
 * It uses prototype.js 1.5.1 (http://www.prototypejs.org)
 * 
 * Author : Lalit Patel
 * Website: http://www.lalit.org/lab/jsoncookies
 * License: Creative Commons Attribution-ShareAlike 2.5
 *          http://creativecommons.org/licenses/by-sa/2.5/
 * Version: 0.4
 * Updated: Aug 11, 2007 10:09am
 * 
 * Chnage Log:
 *   v 0.4
 *   -  Removed a extra comma in options (was breaking in IE and Opera). (Thanks Jason)
 *   -  Removed the parameter name from the initialize function
 *   -  Changed the way expires date was being calculated. (Thanks David)
 *   v 0.3
 *   -  Removed dependancy on json.js (http://www.json.org/json.js)
 *   -  empty() function only deletes the cookies set by CookieJar
 */

var CookieJar = Class.create();

CookieJar.prototype = {

	/**
	 * Append before all cookie names to differntiate them.
	 */
	appendString: "__CJ_",

	/**
	 * Initializes the cookie jar with the options.
	 */
	initialize: function(options) {
		this.options = {
			expires: 3600,		// seconds (1 hr)
			path: '',			// cookie path
			domain: '',			// cookie domain
			secure: ''			// secure ?
		};
		Object.extend(this.options, options || {});

		if (this.options.expires != '') {
			var date = new Date();
			date = new Date(date.getTime() + (this.options.expires * 1000));
			this.options.expires = '; expires=' + date.toGMTString();
		}
		if (this.options.path != '') {
			this.options.path = '; path=' + escape(this.options.path);
		}
		if (this.options.domain != '') {
			this.options.domain = '; domain=' + escape(this.options.domain);
		}
		if (this.options.secure == 'secure') {
			this.options.secure = '; secure';
		} else {
			this.options.secure = '';
		}
	},

	/**
	 * Adds a name values pair.
	 */
	put: function(name, value) {
		name = this.appendString + name;
		cookie = this.options;
		var type = typeof value;
		switch(type) {
		  case 'undefined':
		  case 'function' :
		  case 'unknown'  : return false;
		  case 'boolean'  : 
		  case 'string'   : 
		  case 'number'   : value = String(value.toString());
		}
		var cookie_str = name + "=" + escape(Object.toJSON(value));
		try {
			document.cookie = cookie_str + cookie.expires + cookie.path + cookie.domain + cookie.secure;
		} catch (e) {
			return false;
		}
		return true;
	},

	/**
	 * Removes a particular cookie (name value pair) form the Cookie Jar.
	 */
	remove: function(name) {
		name = this.appendString + name;
		cookie = this.options;
		try {
			var date = new Date();
			date.setTime(date.getTime() - (3600 * 1000));
			var expires = '; expires=' + date.toGMTString();
			document.cookie = name + "=" + expires + cookie.path + cookie.domain + cookie.secure;
		} catch (e) {
			return false;
		}
		return true;
	},

	/**
	 * Return a particular cookie by name;
	 */
	get: function(name) {
		name = this.appendString + name;
		var cookies = document.cookie.match(name + '=(.*?)(;|$)');
		if (cookies) {
			return (unescape(cookies[1])).evalJSON();
		} else {
			return null;
		}
	},

	/**
	 * Empties the Cookie Jar. Deletes all the cookies.
	 */
	empty: function() {
		keys = this.getKeys();
		size = keys.size();
		for(i=0; i<size; i++) {
			this.remove(keys[i]);
		}
	},

	/**
	 * Returns all cookies as a single object
	 */
	getPack: function() {
		pack = {};
		keys = this.getKeys();

		size = keys.size();
		for(i=0; i<size; i++) {
			pack[keys[i]] = this.get(keys[i]);
		}
		return pack;
	},

	/**
	 * Returns all keys.
	 */
	getKeys: function() {
		keys = $A();
		keyRe= /[^=; ]+(?=\=)/g;
		str  = document.cookie;
		CJRe = new RegExp("^" + this.appendString);
		while((match = keyRe.exec(str)) != undefined) {
			if (CJRe.test(match[0].strip())) {
				keys.push(match[0].strip().gsub("^" + this.appendString,""));
			}
		}
		return keys;
	}
};
/********** COOKIE LIB **********/
