// EVENTS FUNCTIONS
function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
}

function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}

function DetachEvent(obj, evt, fnc, useCapture){
	if(!useCapture) useCapture = false;
	if(obj.removeEventListener){
		obj.removeEventListener(evt, fnc, useCapture);
	}else if(obj.detachEvent){
		return obj.detachEvent("on"+evt, fnc);
	}else{
		MyDetachEvent(obj, event, fnc);
	}
}

function MyDetachEvent(obj, evt, fnc){
	if(!obj.myEvents) return true;
	if(obj.myEvents[evt]){
		var evts = obj.myEvents[evt];
		for(var i = 0; i < evts.length; i++){
			if(evts[i] == fnc){
				evts.splice(i, 1); // remove unnecessary event from array
				break; // get out of loop;
			}
		}
	}
}

function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}
// END EVENTS FUNCTIONS


// POPUP BANNER FUNCTIONS
var mantinasTools = 
{
	getBrowserWidth: function()
	{
		if(window.innerWidth) {
			return window.innerWidth;
		}
		return document.body.clientWidth;
	}
}
var popupBanner =
{
	init: function()
	{
		var width = mantinasTools.getBrowserWidth() - 980;
		$('#banner-popup2').css(
			{
				'left'	:	Math.round(width) + 'px'
			}
		).show();
		
		$('#banner-popup2 .close a').click(
			popupBanner.close
		);
		
		if($.browser.msie) {
			$(window).scroll(
				popupBanner.fixIE
			).resize(
				popupBanner.fixIE
			);
		}
	},
	fixIE:	function()
	{
		var top = document.documentElement.scrollTop + 140;
		$('#banner-popup2').css(
			{
				'top'	: parseInt(top) + 'px'
			}
		);
	},
	close:	function()
	{
		var id = this.rel.substr(3);
		$.cookie(
			'popupBanner',
			id+'|',
			{
					path: '/', 
					domain: '.liepajniekiem.lv'
			}
		);
		$('#banner-popup2').hide();
		
		return false;
	}
}
// END NEW POPUP BANNER FUNCTIONS

// LINE BANNER FUNCTIONS
var roll = false;
function showLineBanner() {
	roll = setInterval("rollLineBanner()", 30);
    fixLineBanner();
    if (window.attachEvent && (navigator.userAgent.indexOf('MSIE 6') > 0)) {
    	AttachEvent(window, 'scroll', fixLineBanner);
    	document.getElementById('line_banner').style.position = 'absolute';
			document.getElementById('line_banner_close').style.position = 'absolute';
    } else {
			AttachEvent(window, 'scroll', fixLineBanner);
    	document.getElementById('line_banner').style.position = 'fixed';
			document.getElementById('line_banner_close').style.position = 'fixed';
    }
    AttachEvent(window, 'resize', fixLineBanner);
    document.getElementById('line_banner').style.display = 'block';
}

function hideLineBanner(){
	clearInterval(roll);
	if(window.attachEvent){
		DetachEvent(window, 'scroll', fixLineBanner);
	}
	DetachEvent(window, 'resize', fixLineBanner);
	document.getElementById('line_banner').style.display = 'none';
	document.getElementById('line_banner_close').style.display = 'none';
}

function rollLineBanner() {
	$leftPos = parseInt(document.getElementById('line_banner_table').style.left);
    document.getElementById('line_banner_table').style.left = $leftPos-$shift+'px';
    if (Math.abs($leftPos) >= $bWidth) {
    	document.getElementById('line_banner_table').style.left = (-$shift)+'px';
	}
}

function fixLineBanner() {
	var lineBanner = document.getElementById('line_banner');
	var closeButton = document.getElementById('line_banner_close');
	lineBanner.style.left = 0;

	if (lineBanner.style.position == 'absolute') {
		if (window.opera) {
			lineBanner.style.top = parseInt(document.body.scrollTop+document.body.clientHeight - $bHeight) + "px";
			closeButton.style.top = parseInt(document.body.scrollTop + document.body.clientHeight - $bHeight - closeButton.style.height) + "px";
		} else {
			//console.log('here');
			lineBanner.style.top = parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop - $bHeight) + "px";
			closeButton.style.top = parseInt(document.documentElement.clientHeight + document.documentElement.scrollTop - $bHeight - 18) + "px";
			//closeButton.style.top = parseInt(document.documentElement.clientHeight + document.documentElement.scrollTop - $bHeight - closeButton.style.height) + "px";
		}
	}
	//closeButton.style.bottom = lineBanner.style.height + "px";
	lineBanner.style.width = getBrowserWidth()+"px";
}
// LINE BANNER FUNCTIONS

// OTHER FUNCTIONS
function getBrowserWidth() {
	if(window.innerWidth) {
		return window.innerWidth;
	}
	return document.body.clientWidth;
}

function isIE() {
	if (window.innerWidth) {
		return false;
	}
	return true;
}
// END OTHER FUNCTIONS
