/*
JSTarget function by Roger Johansson, www.456bereastreet.com
*/
var JSTarget = {
	init: function(att,val) {
		if (document.getElementById && document.createElement && document.appendChild) {
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			for (var i = 0; i < arrLinks.length; i++) {
				oLink = arrLinks[i];
				if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {
					oLink.onclick = JSTarget.openWin;
				}
			}
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
		    var oWin = window.open(this.getAttribute('href'), '_blank');
			if (oWin) {
				if (oWin.focus) oWin.focus();
				return false;
			}
			oWin = null;
			return true;
		}
	},
	/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	*/
	addEvent: function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn]( window.event );}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
JSTarget.addEvent(window, 'load', function() {JSTarget.init("rel","external");});


/* Add Business */

var YellowPages = {

	/* Pre-populate URL with Business name */

	fillAddress: function(){
		$('basic-name-en').observe('change', function(){
			var url = $('basic-name-en').value.replace(/ /g, '-');
			$('basic-url').value = url;
		});
	},

	/* Show custom field for "In business since year" */

	initSinceYear: function(){
		$$('#business-since').each(function(el){
			el.observe('click', function(){
				if (el.value == 'custom') {
					$('business-since-custom').setStyle({ display: 'inline' });
				} else {
					$('business-since-custom').setStyle({ display: 'none' });
				}
			});
		});
	},

	/* Business hours */

	initBusinessHours: function(){

		/* Disable hours */
		$$('#business-hours-week select').each(function(el){
			el.disabled = true;
		});

		/* Change style for custom hours */
		$$('input.check-business-hours').each(function(el){
			el.observe('click', function(){
				if (el.id == 'business-hours-custom') {
					$('business-hours-week').removeClassName('hours-custom');
					$$('#business-hours-week select').each(function(el){
						el.disabled = false;
					});
				} else {
					$('business-hours-week').addClassName('hours-custom');
					$$('#business-hours-week select').each(function(el){
						el.disabled = true;
					});
				}
			});
		});

		/* Show from-to time */
		$$('select.closed-or-open').each(function(el){
			el.observe('change', function(){
				other = $(el.id + '-to');
				if (el.options[el.selectedIndex].value == '') {
					other.setStyle({ display: 'none' });
				} else {
					other.setStyle({ display: 'inline' });
				}
			});
		});
	},

	/* Toggle Farsi */

	toggleFarsi: function(){
		$$('input.farsi-switcher').each(function(el){
			el.observe('change', function(){
				if (el.checked == true) {
					$$('div.lang-en label').each(function(el) {
						el.setStyle({ display: 'block' });
					});
					$$('div.lang-fa').each(function(el){
						el.setStyle({ display: 'block' });
					});
				} else {
					$$('div.lang-en label').each(function(el){
						el.setStyle({ display: 'none' });
					});
					$$('div.lang-fa').each(function(el){
						el.setStyle({ display: 'none' });
					});
				}
			});
		});
	}

}


window.onload=function() {
	Nifty("div.column","bl br");
	Nifty("div.sidebar-container");
	setupZoom();

	if ($('business-hours-hide') && $('business-hours-24') && $('business-hours-custom') && $$('select.closed-or-open')) YellowPages.initBusinessHours();
	if ($('basic-name-en')) YellowPages.fillAddress();
	if ($('business-since')) YellowPages.initSinceYear();
	if ($$('input.farsi-switcher')) YellowPages.toggleFarsi();
}