/* ----- NOTES -----------------------------------------------------

Globala funktioner

------------------------------------------------------------------ */

var Marsta = {}

Marsta.global = {
	
	
	init : function () {
		this.latesttabs();
		this.subpagetabs();
		this.admintabs();
		this.tablestripes();
		this.openPoll();
		this.animatepollbars();
		this.appendupdatelink();
		this.buttons();
		this.shadowbox();
	},
	
	
	latesttabs : function () {
		$('#latest-box ul.tabs-fx').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
	},
	
	
	subpagetabs : function () {
		$('#subpage ul.tabs-fx').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
	},
	
	
	admintabs : function () {
		$('#clients-wrapper ul.tabs-fx').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
	},
	
	
	tablestripes : function () {
		$(".tb-stripes tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
		$(".tb-stripes tr:even").addClass("stripe");
	},
	
	
	openPoll : function () {
		$("div.poll input.vote-btn")
			.livequery('click', function(event) {
			var url = $('div.poll a.result-btn').attr('href');
			Shadowbox.open({
				title:		'Resultat för webbfråga',
				player:		'iframe',
				content:	url,
				height:		355,
				width:		500
			});
			return true;
		});
		
	},
	
	
	animatepollbars : function () {
		$("li span.pollbar").each(function() {
			var w = $(this).width();
			var wp = $(this).css("width");
			$(this).width(0).animate({width: w }, 500, function() {
				$(this).width(wp);
			});
		});
	},
	
	
	appendupdatelink : function () {
		var updlink = '<p class="arrow-4"><a href="javascript:window.location.reload();">Uppdatera resultat</a></p>';
		$("div.poll-info").append(updlink);
	},
	
	
	buttons : function () {
		var url = document.location.toString();
		
		if (url.match('#comments-form-add')) {
			Marsta.global.smoothScroll($("#comments-form-add"), 15, 500);
		} else if (url.match('#comments-form-report')) {
			Marsta.global.smoothScroll($("#comments-form-report"), 15, 500);
		} else if (url.match('#article-comments')) {
			$("#article-comments div.paging a").click(function() {
				Marsta.global.smoothScroll($("#article-comments"), 15, 500);
			});
			Marsta.global.smoothScroll($("#article-comments"), 15, 500);
		}
		
		$("#article-comments dt.top a").click(function() {
			Marsta.global.smoothScroll($("#article-comments"), 15, 500);
			return false;
		});
	},
	
	
	smoothScroll : function(target, addOffset, speed) {
      if (target.length) {
        var targetOffset = target.offset().top;
        targetOffset -= addOffset;
        $('html, body').animate({scrollTop: targetOffset}, speed);
        return false;
      }
	},
	
	
	shadowbox : function () {
		
		Shadowbox.skin.markup = '<div id="sb-container">' +
									'<div id="sb-overlay"></div>' +
									'<div id="sb-wrapper">' +
										'<div id="sb-title">' +
											'<div id="sb-title-inner"></div>' +
											'<div id="sb-author"></div>' +
										'</div>' +
										'<div id="sb-body">' +
											'<div id="sb-body-inner"></div>' +
											'<div id="sb-loading">' +
												'<a onclick="Shadowbox.close()">{cancel}</a>' +
											'</div>' +
										'</div>' +
										'<div id="sb-desc">' +
											'<div id="sb-desc-inner"></div>' +
										'</div>' +
										'<div id="sb-info">' +
											'<div id="sb-info-inner">' +
												'<div id="sb-counter"></div>' +
												'<div id="sb-nav">' +
													'<a id="sb-nav-close" title="Stäng" onclick="Shadowbox.close()">Stäng</a>' +
													'<a id="sb-nav-next" title="Nästa" onclick="Shadowbox.next()">Nästa</a>' +
													'<a id="sb-nav-play" title="{play}" onclick="Shadowbox.play()">{play}</a>' +
													'<a id="sb-nav-pause" title="{pause}" onclick="Shadowbox.pause()">{pause}</a>' +
													'<a id="sb-nav-previous" title="Föregående" onclick="Shadowbox.previous()">Föregående</a>' +
												'</div>' +
												'<div style="clear:both"></div>' +
											'</div>' +
										'</div>' +
									'</div>' +
								'</div>'
		;
		
		var options = {
			animSequence:		'wh',
			continuous:			true,
			counterType:		'skip',
			handleUnsupported:	'remove',
			overlayOpacity:		0.85
		};
		
		Shadowbox.init(options);
		
	}
	
}

/* =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)) ? 'external' : 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]);
		}
	}
};
// Look fore class external
JSTarget.addEvent(window, 'load', function(){JSTarget.init();});