/* ----- 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();
	},
	
	
	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;
      }
	},
	
	
}

function popupAd(cookieName, cookieExpires, adUrl, adLink) {
	var COOKIE_NAME = cookieName;
	
	// Check if the above cookie exists, if it doesn't exist, we create the cookie and open the window
	if ($.cookie(COOKIE_NAME) == null) {
		// Create cookie
		$.cookie(COOKIE_NAME, 'true', { path: '/', expires: cookieExpires });
		// Open window
		$.prettyPhoto.open(adUrl,'Annons', 'Detta &auml;r en annons. Klicka p&aring; knappen st&auml;ng till h&ouml;ger f&ouml;r att st&auml;nga ner annonsen.', adLink);
	}
};

/* =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();});
