/*jslint browser: true, newcap: true, eqeqeq: true */
/*global YAHOO */
YAHOO.namespace("aos.common");

YAHOO.aos.common.Utils = function() {
};

YAHOO.aos.common.Utils.prototype = {

	/**
	* Update article font size
	*/
	changeFontSize: function(size) {
		var documentFontSize = "100%";
		var abTextSizeImage = "_S.gif";

		if (size === "S") {
			documentFontSize = "100%";
			abTextSizeImage = "_S.gif";
		} else if (size === "M") {
			documentFontSize = "120%";
			abTextSizeImage ="_M.gif";
		} else if (size === "L") {
			documentFontSize = "133%";
			abTextSizeImage ="_L.gif";
		}

		var artArea = document.getElementById("AOS_DesignMidCB");

		if (artArea) {
			artArea.style.fontSize = documentFontSize;
		}

		var txtImage1 = document.getElementById("AOS_ArtTextSizeImage");
		if (txtImage1) {
			var mySplitResult = txtImage1.src.split("_");
			txtImage1.src = mySplitResult[0] + abTextSizeImage;
		}
	},

	/**
	* Change from one class to another
	*/
	changeClass: function(element, class1, class2) {
		if (YAHOO.util.Dom.hasClass(element, class1)) {
			YAHOO.util.Dom.removeClass(element, class1);
			YAHOO.util.Dom.addClass(element, class2);
		}
		else if (YAHOO.util.Dom.hasClass(element, class2)) {
			YAHOO.util.Dom.removeClass(element, class2);
			YAHOO.util.Dom.addClass(element, class1);
		}
	},


	/**
	* Add class to last element of specified class in containers with specified class
	*/
	addClassToLastElement: function(containerClass, elementClass, classToAdd) {
		var box, lastDiv, infoBoxes = YAHOO.util.Dom.getElementsByClassName(containerClass);
		for (box in infoBoxes) {
			if (infoBoxes.hasOwnProperty(box)) {
				lastDiv = YAHOO.util.Dom.getLastChildBy(infoBoxes[box], function(child){return(YAHOO.util.Dom.hasClass(child, elementClass));});
				YAHOO.util.Dom.addClass(lastDiv, classToAdd);
			}
		}
	},

	/**
	* Search focus onLoad
	*/
	sf: function() {
		document.AOSf1.AOSs1.focus();
	},

	/**
	* Show hitta map in YAHOO Panel
	*/
	showMapAddress: function(name, address, articleId, plain) {
		var src = "http://www.hitta.se/LargeMap.aspx?var=" + address.replace(/ /g, '+') + "&z=2&toolbars=zoom,controls";
		if (plain) {
			YAHOO.aos.common.utils.showMapPlain(src, articleId);
		} else {
			YAHOO.aos.common.utils.showMap(src, articleId);
		}
	},

	showMapXY: function(name, address, x, y, articleId, plain) {
		/* X is Y and Y is X */
		var src = "http://www.hitta.se/LargeMap.aspx?cx=" + y + "&cy=" + x + "&PointX=" + y + "&PointY=" + x + "&name=" + escape(name.replace(/ /g, '+')) + ", " + address.replace(/ /g, '+') + "&z=2&toolbars=zoom,controls";
		if (plain) {
			YAHOO.aos.common.utils.showMapPlain(src, articleId);
		} else {
			YAHOO.aos.common.utils.showMap(src, articleId);
		}
	},

	showMap: function(src, articleId) {

		var mapPanel = "AOS_MapPanel" + articleId;
		YAHOO.util.Dom.get('AOS_MapContainer').innerHTML = "<div id=\"" + mapPanel + "\"><div class=\"hd\"></div><div class=\"bd\"></div><div class=\"ft\"></div></div>";

		var AOS_MapPanel = new YAHOO.widget.Panel(mapPanel,
			{
				width:"852px",
				height:"580px",
				visible:false,
				draggable:true,
				close:true
			}
		);
		AOS_MapPanel.setHeader("Allt om Stockholm - karta");
		AOS_MapPanel.setBody("<iframe src='" + src + "' width='830' height='500' frameborder='0'></iframe>");
		AOS_MapPanel.setFooter("- i samarbete med hitta.se");
		AOS_MapPanel.render();
		AOS_MapPanel.center();
		AOS_MapPanel.show();
	},

	showMapPlain: function(src, articleId) {
		YAHOO.util.Dom.get('AOS_MapContainer').innerHTML = "<iframe src='" + src + "' width='830' height='500' frameborder='0'></iframe>";
	},

	validateForm: function(form) {
		var errorMessage = "";
		var valid = true;
		if(form['value(name)'].value == null || form['value(name)'].value == "") {
			errorMessage += "Fyll i namn.<br/>";
			valid = false;
		}
		if(form['value(email)'].value == null || form['value(email)'].value == "" || !YAHOO.aos.common.utils.isValidEmail(form['value(email)'].value)) {
			errorMessage += "Fyll i en korrekt e-postadress.<br/>";
			valid = false;
		}
		if(form['value(Personer)'].value == null || form['value(Personer)'].value == "") {
			errorMessage += "Fyll i antal personer.<br/>";
			valid = false;
		}
		if(form['value(Prisklass)'].value == null || form['value(Prisklass)'].value == "0") {
			errorMessage += "Fyll i prisklass.<br/>";
			valid = false;
		}
		if(form['value(Check)'].value !== "FEST") {
			errorMessage += "Du måste skriva FEST i fältet.<br/>";
			valid = false;
		}
		if(!valid) {
			YAHOO.util.Dom.get('AOS_FormErrorMsg').innerHTML = errorMessage;
			YAHOO.util.Dom.get('AOS_SearchInput').focus();
		}
		return valid;
	},

	isValidEmail: function(val) {
		aPos=val.indexOf("@");
		dotPos=val.lastIndexOf(".");
		if (aPos < 1 || dotPos-aPos < 2) {
			return false;
		}
		return true;
	},

	randomText: function() {

		var pathLocationObj = window.location.pathname;
		var siteNum = 100

		if (pathLocationObj != "/restaurangbar/" && pathLocationObj != "/klubbkonsert/" &&
			pathLocationObj != "/scenkultur/" && pathLocationObj != "/shoppingmode/" && pathLocationObj != "/turisthotell/") {
			siteNum = Math.floor(Math.random() * 4) + 1;
		}

		if(pathLocationObj.indexOf("/julistan/") === 0 || pathLocationObj.indexOf("/julbordsguiden/") === 0) {
			YAHOO.util.Dom.addClass('AOS_DesignHeader', 'AOS_DesignHeaderJ1');
		}
		else if(pathLocationObj == "/restaurangbar/" || siteNum == "1") {
			var pathNum = Math.floor(Math.random() * 2) + 1;
			YAHOO.util.Dom.addClass('AOS_DesignHeader', 'AOS_DesignHeaderR' + pathNum);
		}
		else if(pathLocationObj == "/klubbkonsert/" || siteNum == "2") {
			var pathNum = Math.floor(Math.random() * 2) + 1;
			YAHOO.util.Dom.addClass('AOS_DesignHeader', 'AOS_DesignHeaderK' + pathNum);
		}
		else if(pathLocationObj == "/scenkultur/" || siteNum == "3") {
			var pathNum = Math.floor(Math.random() * 2) + 1;
			YAHOO.util.Dom.addClass('AOS_DesignHeader', 'AOS_DesignHeaderS' + pathNum);
		}
		else if(pathLocationObj == "/shoppingmode/" || siteNum == "4") {
			var pathNum = Math.floor(Math.random() * 2) + 1;
			YAHOO.util.Dom.addClass('AOS_DesignHeader', 'AOS_DesignHeaderM' + pathNum);
		}
		else if(pathLocationObj == "/turisthotell/" || siteNum == "5") {
			var pathNum = Math.floor(Math.random() * 2) + 1;
			YAHOO.util.Dom.addClass('AOS_DesignHeader', 'AOS_DesignHeaderT' + pathNum);
		}
	},

	adjustAd: function(placement) {
		var frame = YAHOO.util.Dom.get("AD_" + placement);
		var div = YAHOO.util.Dom.get("div_AD_" + placement);
		if (div && frame && frame.contentWindow.document.body.innerHTML) {
			div.innerHTML = frame.contentWindow.document.body.innerHTML;
		}
	},

	windowOpen : function(url, width, height, name) {
		//var abuseLink='<a href="#" onClick="abWindowOpen(\''+this.ABUSE_URL+"?abuseLink="+abuseLink+"','700','600');return false;\">";

	}

};
YAHOO.aos.common.utils = new YAHOO.aos.common.Utils();
YAHOO.util.Event.onAvailable("AOS_DesignHeader", YAHOO.aos.common.utils.randomText);


function liveBookingTable(restaurantId) {

	if( typeof LBDirect_Popup !== "undefined") {
		LBDirect_Popup('SE-ALLTOMSTOCKHOLM:15779','sv-SE,0,DC5902,CC0099,df,000000,1,,FFFFFF',restaurantId);
	}
	return false;
}


