function initCookies() {
	var referrer = new String(document.referrer);
	referrer = (referrer.indexOf(document.location.host) != -1)? '<direct>': document.referrer;
	var uri = new URI(document.location);
	var adReferrer = uri.getData('bn');
	if(!adReferrer) {
		adReferrer = '<direct>';
	}
	if(!Cookie.read('ref1')) {
		setCookie('ref1', referrer);
		setCookie('ref2', '<first access>');
	}
	else if(referrer != 'local') {
		setCookie('ref2', referrer);
	}
	if(!Cookie.read('ad_ref1')) {
		setCookie('ad_ref1', adReferrer);
		setCookie('ad_ref2', '<first access>');
	}
	else if(adReferrer != 'local') {
		setCookie('ad_ref2', adReferrer);
	}
}

function setCookie(name, value) {
	var date = new Date();
	date = new Date(date.getFullYear(), date.getMonth() + 3, date.getDate());
	document.cookie = name + '=' + escape(value) + '; expires=' + date.toGMTString();
}

function initLinksTarget() {
	$$('a[rel=nofollow]').setProperty('target', '_blank');
}

function initExamplePage() {
	$$('ul.images a').addEvent('click', function() {
		var rel = this.getProperty('rel').replace('#', '');
		window.open('examples/viewer.htm#' + rel, "Example", "width=1050,height=650,toolbar=no,status=no,scrollbars=yes,menubar=no,resizable=yes");
		return false;
	});
}

function initLiveSupport() {
	$$('.live-support-link').addEvent('click', function(event) {
		event.stop();
		openChatPopup($(this).getProperty('href'));
	});
}

function openChatPopup(url) {
	var chatPopup = window.open(url, 'CHAT', 'width=584,height=490,toolbar=no,status=no,scrollbars=no,menubar=no,resizable=no');
	if(chatPopup) {
		try {
			chatPopup.focus();
		}
		catch(e) {};
	}
}

function openChatHistoryPopup(url) {
	var chatHistoryPopup = window.open(url, 'HISTORY', 'width=598,height=571,toolbar=no,status=no,scrollbars=no,menubar=no,resizable=no');
	if(chatHistoryPopup) {
		try {
			chatHistoryPopup.focus();
		}
		catch(e) {}
	}
}

function parentWindowOpen(href) {
	var opener;
	if(window.opener) {
		if(window.opener.opener) {
			opener = window.opener.opener;
		}
		else {
			opener = window.opener;
		}
		opener.location = href;
	}
	else {
		opener = window.open(href);
	}
	try {
		opener.focus();
	}
	catch(e) {};
}

function isDst(date) {
	var summerDate = new Date(date.getFullYear(), 6, 1);
	return summerDate.getTimezoneOffset() == date.getTimezoneOffset();
}

function getDstOffset() {
	var d = new Date();
	var winter = new Date(d.getFullYear(), 0, 1);
	var sommer = new Date(d.getFullYear(), 6, 1);
	var winterOffset = -winter.getTimezoneOffset();
	var sommerOffset = -sommer.getTimezoneOffset();
	return sommerOffset - winterOffset;
}

function getGmtOffset() {
	var d = new Date();
	var winter = new Date(d.getFullYear(), 0, 1);
	var sommer = new Date(d.getFullYear(), 6, 1);
	var winterOffset = -winter.getTimezoneOffset();
	var sommerOffset = -sommer.getTimezoneOffset();
	return (winterOffset < sommerOffset)? winterOffset: sommerOffset;
}

function setGmtOffset(elementId) {
	var gmtOffsetElement = $(elementId);
	if(gmtOffsetElement) {
		gmtOffsetElement.setProperty('value', getGmtOffset());
	}
}

function getPageScroll() {
	var pageScroll;
	if(self.pageYOffset) {
		pageScroll = self.pageYOffset;
	} else if(document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		pageScroll = document.documentElement.scrollTop;
	} else if(document.body) { // all other Explorers
		pageScroll = document.body.scrollTop;
	}
	return pageScroll;
}

function getPageSize() {
	var xScroll, yScroll;
	var windowWidth, windowHeight;
	if(window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if(document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	if(self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if(document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if(document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth) {
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight, xScroll, yScroll);
	return arrayPageSize;
}

function detectSafariVersion() {
	if(navigator.appVersion.indexOf("Safari") != -1) {
		if(!window.getComputedStyle) document.body.className += " safari2";
		else document.body.className += " safari3";
	}
}

function detectBrowser() {
    var cssFix = function() {
        var u = navigator.userAgent.toLowerCase(),
        addClass = function(el,val){
        if(!el.className) {
            el.className = val;
        } else {
            var newCl = el.className;
            newCl+=(" "+val);
            el.className = newCl;
        }
    },
    is = function(t){return (u.indexOf(t)!=-1)};
    addClass(document.getElementsByTagName('html')[0],[
        (!(/opera|webtv/i.test(u))&&/msie (\d)/.test(u))?('ie ie'+RegExp.$1)
        :is('firefox/2')?'gecko ff2'
        :is('firefox/3')?'gecko ff3'
        :is('gecko/')?'gecko'
        :is('chrome/')?'chrome'
        :is('opera/9')?'opera opera9':/opera (\d)/.test(u)?'opera opera'+RegExp.$1
        :is('konqueror')?'konqueror'
        :is('applewebkit/')?'webkit safari'
        :is('mozilla/')?'gecko':'',
        (is('x11')||is('linux'))?' linux'
        :is('mac')?' mac'
        :is('win')?' win':''
    ].join(" "));
	}();
}

/* new in markup4u */
function initSupport() {
	if(!$('support')){
		return;
	}
	var ce = $("contact-email");
	var ct = $("contact-text");
	//ce.value = "Email:";
	//ct.value = "Notes:";
	supField(ce);
	supField(ct);
	if(!Browser.Engine.trident){
		$$('#support-file-holder span.file').setStyle("opacity", 0);
		var b = $$("#support ul")[0];
		b.setStyle("opacity", 0.8);
	}
}

function supField(a){
	a._value=a.value;
	a.onblur=function(){
		this.value.trim();
		if(this.value==""){
			this.value=this._value;
		}
	};
	a.onfocus=function(){
		if(this.value==this._value){
			this.value="";
		}
	};
}

function m4uSmoothScroll(){
	new SmoothScroll( {
		offset :  {
			x : 0, y : 0}
		, wait : false, duration : 500
	});
}

function update_profile() {
	if($('update_profile'))
	{
		//$('update_profile').addEvent('click', function(){$('form_update_profile').submit();return false;});
		supField($('password'));
		supField($('password-re-enter'));
	}
}

function resizeTextArea(){
	$$("textarea").setStyle("resize", "none");
	$$("#nswitch").setStyle("display", "block");
	$$("#preview").setStyle("overflow-x", "hidden");
	$$("#nswitch").addEvent("click", function() {
		if(!this.flag){
			this.height = parseInt(this.getPrevious().getStyle('height'));
			if($('post')){
				$$("#post").tween("height", 380);
			}else{
				$$("#notes").tween("height", 380);
			}
			this.setStyle("background-position", "0 -10px");
			this.flag = true;
		}else{
			if(!this.height)
			{
				this.height = 150;
			}
			if($('post')){
				$$("#post").tween("height", this.height);
			}else{
				$$("#notes").tween("height", this.height);
			}
			this.setStyle("background-position", "0 0");
			this.flag = false;
		}
		return false;
	});
}

function initTabset(){
	$$("#tabset a").removeEvents("click");
	$$("#tabset a").addEvent("click", function() {
		if(!$(this.parentNode).hasClass("active")) {
			$$("#tabset li").removeClass("active"); $$("#tabset a").each(function(a) {
				$(a.href.substr(a.href.indexOf("#") + 1)).setStyle("display", "none")}
			); $(this.href.substr(this.href.indexOf("#") + 1)).setStyle("display", "block"); $(this.parentNode).addClass("active")}
		return false}
	);
	window.onresize = function() {
		fixView();
	};
	fixView();
}

function fixView() {
	var a;
	if(window.innerHeight){
		a = window.innerHeight;
	}else{
		a = document.documentElement.clientHeight;
	}
	$$("#preview").setStyle("height", a - 65);
	$$("#html").setStyle("height", a - 65);
	$$("#css").setStyle("height", a - 65);
}

window.addEvent('domready', function() {
	initLinksTarget();
	initExamplePage();
	initLiveSupport();
	detectBrowser();
	detectSafariVersion();
	initCookies();
	/* new in markup4u */
	initSupport();
	m4uSmoothScroll();
	update_profile();
	resizeTextArea();
	$$("ul.icons a").addEvent("click", function() {
		window.open(this.href, "Example", "width=1024,height=665,toolbar=no,status=no,scrollbars=yes,menubar=no,resizable=yes"); return false}
	);
	if($("tabset")){
		initTabset();
	}
	$$("a[rel$=external]").set("target", "_blank");
	var a = "4px";
	$$("h2 span").setStyles({
		"border-radius" : a, "-moz-border-radius" : a, "-webkit-border-radius" : a}
	);
})
