// stop image flicker on hover in ie6
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

var balesampan = {
	
	// add the smooth rollover effect to the nav
	innitNav : function(){
	
		$('#nav-inner > ul > li > a').removeClass("hover");
		
		$('#nav-inner > ul > li > a').mouseover(function(){
			if($(this).parent().hasClass("selected")){
				return;
			}
			var hover = document.createElement("DIV");
			hover.className 	= "hover";
			hover.style.display = "none";
			hover.style.width 	= $(this).width() + "px";
			hover.style.height 	= $(this).height() + "px";
			$(this).parent().append(hover);
			$(this).parent().children("div.hover").fadeIn("slow");
		});
		
		$('#nav-inner > ul > li > a').mouseout(function(){
			if($(this).parent().hasClass("selected")){
				return;
			}
	
			$(this).parent().children("div.hover").remove();
		   
			var hover = document.createElement("DIV");
			hover.className 	= "hover";
			hover.style.width 	= $(this).width() + "px";
			hover.style.height 	= $(this).height() + "px";
			$(this).parent().append(hover);
		   
			$(this).parent().children("div.hover").fadeOut("slow", function(){
				$(this).parent().children("div.hover").remove();
			});
		});
		
		$('#nav-inner > ul > li > a').focus(function(){
			if($(this).parent().hasClass("selected")){
				return;
			}
			var hover = document.createElement("DIV");
			hover.className 	= "hover";
			hover.style.display = "none";
			hover.style.width 	= $(this).width() + "px";
			hover.style.height 	= $(this).height() + "px";
			$(this).parent().append(hover);
			
			$(this).css("color","#050505");
			
			$(this).parent().children("div.hover").fadeIn("slow");
		});
		
		$('#nav-inner > ul > li > a').blur(function(){
			if($(this).parent().hasClass("selected")){
				return;
			}
	
			$(this).parent().children("div.hover").remove();
		   
			var hover = document.createElement("DIV");
			hover.className 	= "hover";
			hover.style.width 	= $(this).width() + "px";
			hover.style.height 	= $(this).height() + "px";
			$(this).parent().append(hover);
			
			$(this).css("color","#fff");
			
			$(this).parent().children("div.hover").fadeOut("slow", function(){
				$(this).parent().children("div.hover").remove();
			});
		});
	
	},
	
	// gets the weather feed
	getWeather : function(){
		$.getFeed({
			url: '/_inc/inc.proxy.php?url=http://weather.msn.com/RSS.aspx?wealocations=wc:IDXX0080&weadegreetype=F',
			success: function(feed) {
				var html = feed.items[0].description;
				// all very messy this, but necessary, as is the protective logic
				if(html.indexOf('Current conditions (')==-1){
					return;
				}
				var sentences = html.split('Current conditions (')[1];
				if(sentences.indexOf('All times')==-1){
					return;
				}
				sentences = sentences.split('All times')[0]
				if(sentences.indexOf('<br />')==-1){
					return;
				}
				sentences = sentences.split('<br />');
				if(sentences[2].indexOf('.')==-1){
					return;
				}
				var lastSentence = sentences[2].split('.')[1];
				var buffer = new StringBuffer;
				buffer.append(sentences[0]);
				buffer.append(sentences[1]);
				buffer.append(lastSentence);
				html = buffer.toString();
				if(html.indexOf('(')==-1){
					return;
				}
				html = html.split('(')[0];
				if(html.indexOf('PM)')!=-1){
					html = html.replace('PM)','PM, Lombok');
				}else if(html.indexOf('AM)')!=-1){
					html = html.replace('AM)','AM, Lombok');
				}
				var buffer = new StringBuffer;
				buffer.append("<span class=\"as-of\">...");
				buffer.append(html);
				html = buffer.toString();
				if(html.indexOf('<img')==-1){
					return;
				}
				html = html.split('<img');
				if(html[1].indexOf('/>')==-1){
					return;
				}
				var temp = html[1].split('/>');
				var buffer = new StringBuffer;
				buffer.append(temp[0]);
				buffer.append('/>');
				buffer.append('<span class="temp">');
				buffer.append(temp[1]);
				temp = buffer.toString();
				var buffer = new StringBuffer;
				buffer.append(html[0]);
				buffer.append("</span><img");
				buffer.append(temp);
				buffer.append('</span>');
				html = buffer.toString();
				$('#result').append(html);
				$("#result").removeClass("hide");
				$("#note").removeClass("hide");
			}    
		});
	},
	
	// create and display our custom banner
	displayBanner : function(){
		
		// globals for banner
		var iIsClicked = false; // integer. nav item that has been clicked. set as false initially.
		var iIsShown = true; // integer. nav item that is being shown. set as false initially.
		var oIsOn = false; // object. highlighting border being shown. set as false initially.
		//var sNavImgDir = (sNavImgDir)? sNavImgDir:'home'; // string. the directory containing these specific nav images
		var aImages = arguments;
		
		// creates banner
		$('#banner') 
		.before('<div id="banner-nav">') 
		.cycle({ 
			fx:     'fade', 
			speed:  1000, 
			timeout: 0, 
			pager:  '#banner-nav',
			// three functions controlling the nav cloak -
			// that stops users clicking another image - 
			// before their first choice has finished
			before:  function(){
				if(iIsClicked!=iIsShown){
					$("#banner-nav-cloak").show();
				}
			},
			after:   function(){
				if(iIsClicked!=iIsShown){
					$("#banner-nav-cloak").hide();
				}
				iIsShown = iIsClicked;
			},
			pagerClick: function(zeroBasedSlideIndex, slideElement){
				iIsClicked = zeroBasedSlideIndex;
			}
		});
		
		$("#banner img").css("visibility","visible");
		
		var bg = document.createElement("DIV");
		$(bg).attr("id", "banner-nav-bg");
		$("#banner-nav").append($(bg));
		
		$("#banner-nav").hover(
			function(){
				$(this).animate({
					top : "0px"
				});
				$("#banner-nav-bg").css("background-image","url(/_i/banner-nav-bg-up.png)");
			},
			function(){
				$(this).animate({
					top : "-78px"
				});
				$("#banner-nav-bg").css("background-image","url(/_i/banner-nav-bg.png)");
			}
		);
		
		// add our custom markup and functions to standard nav
		$.each($("#banner-nav > a"), function(i){
			
			$(this).attr("id","nav" + (i+1));
			//$(this).css("background-image","url(/_i/banner/home/mini_0"+(i+1)+".jpg)");
			var src = "/_i/banner/mini_0"+(aImages[i])+".jpg";
			$(this).html('<img src="'+src+'" />');
			
			var oInner = document.createElement("DIV");
			oInner.className = "inner";
			//$(this).append(oInner.cloneNode(true));
			
			var oOver = document.createElement("DIV");
			oOver.className = "over";
			oOver.appendChild(oInner.cloneNode(true));
			
			var oOn = document.createElement("DIV");
			oOn.className = "on";
			oOn.appendChild(oInner);
			
			$(this).append(oOver);
			$(this).append(oOn);
			
			// ensure we don't ever get a 'clicked' border part 1		
			$(this).click(function(){
				var temp = $(this).children().attr("src");
				temp = temp.split("mini_")[1];
				var src = "/_i/banner/xlrg_" + temp;
				$("#lightbox-trigger").attr("href",src);
				$(this).blur();
			});
			// ensure we don't ever get a 'clicked' border part 2		
			$(this).focus(function(){
				$(this).blur();
			});
		});
		
		var isOver;
		
		// add over and on events
		$.each($("#banner-nav > a > img"), function(t){
			$(this).parent().mouseover(function(){
				$(this).children("div.over").show();
			});
			$(this).parent().mouseout(function(){
				$(this).children("div.over").hide();
			});
			$(this).parent().click(function(){
				if(oIsOn) $(oIsOn).hide();
				$(this).children("div.on").show();
				oIsOn = $(this).children("div.on");
			});
		});
		
		// turn our custom nav styles off, then higlight first nav element
		var onDivs = $("#banner-nav").children("a").children("div.on");
		$.each($(onDivs), function(t){
			$(this).hide();
			if(t==0) $(this).show("fast",function(){
				oIsOn = $(this);
			});
		});
		
	},
	
	validateForm : function(){
		var name = $("#yourName");
		var email = $("#emailAddress");
		$(name).removeClass("error");
		$(email).removeClass("error");
		if($("p#form-error")) $("p#form-error").remove();
		if(!$(name).val()||!$(name).val().length){
			var error = document.createElement("P");
			$(error).attr("id","form-error");
			$(error).attr("class","error");
			$(error).html("You must enter your name.");
			$(name).addClass("error");
			$(name).parent().prepend($(error));
			$(error).fadeIn("slow");
			return false;
		}
		if(!$(email).val()||!checkEmail($(email).val())){
			var error = document.createElement("P");
			$(error).attr("id","form-error");
			$(error).attr("class","error");
			$(error).html("Please enter a valid email address.");
			$(email).addClass("error");
			$(email).parent().prepend($(error));
			$(error).fadeIn("slow");
			return false;
		}
		return true;
	}
}

checkEmail = function (sEmail) {
	var at							= "@";
	var dot							= ".";
	var lat							= sEmail.indexOf(at);
	var lstr						= sEmail.length;
	var ldot						= sEmail.indexOf(dot);
	if (sEmail.indexOf(at) == -1) {return false;}
	if (sEmail.indexOf(at) == -1 || sEmail.indexOf(at)==0 || sEmail.indexOf(at)==lstr) {return false;}
	if (sEmail.indexOf(dot) == -1 || sEmail.indexOf(dot)==0 || sEmail.indexOf(dot)==lstr) {return false;}
	if (sEmail.indexOf(at,(lat+1)) != -1) {return false;}
	if (sEmail.substring(lat-1,lat) == dot || sEmail.substring(lat+1,lat+2)==dot) {return false;}
	if (sEmail.indexOf(dot,(lat+2)) == -1) {return false;}
	if (sEmail.indexOf(" ")!=-1) {return false;}
	return true;
}


StringBuffer = function(){
	this.__strings__ = new Array;
}

StringBuffer.prototype.append = function(str){
	this.__strings__.push(str);
}

StringBuffer.prototype.toString = function(str){
	return this.__strings__.join("");
}