var websitePath = "http://tringme.com/";
var supportWebsiteURL = "http://support.tringme.com/";

function getRelativePath() {
	var p = window.location.pathname;
	var pos = 0;
	if(-1!=(pos=p.indexOf("tringme.com"))) {
		pos += ("tringme.com").length;
		p = p.substr(pos);
		p = ('/'==p.charAt(0)?p.substr(1):p);
		if(-1!=p.indexOf('/')) {
			p = p.split('/');
			for(var i=0; i<p.length-1; i++)
				p[i] = "..";
			p[p.length-1] = "";
			p = p.join('/');
		} else
			p = "";
	}
	return p;
}

function getAbsolutePath(relativePath) {
	if(0==relativePath.indexOf('http'))
		return relativePath;
	if(0==relativePath.indexOf('#'))
		return relativePath;
	//return websitePath + relativePath;
	return getRelativePath() + relativePath;
}

function getAbsoluteStaticPagesPath(relativePath) {
	if(0==relativePath.indexOf('http'))
		return relativePath;
	if(0==relativePath.indexOf('#'))
		return relativePath;
	//return websitePath + relativePath;
	return websitePath + relativePath;
}

function navigationHeader(id, active, name) {
	if(null==$(id))
		return;
	var nav_obj = { index: {ref: websitePath+ 'whatistringme.html', html: "What is TringMe?", 'class': "small item"},
		products: {ref: websitePath+'howitworks.html', html: 'How It Works?', 'class': "small item"},
		pricing: {ref: websitePath+'checkrates.html', html: 'How Much It Costs?', 'class': "small item" },
		developer: {ref: websitePath+'developers.html', html: 'For Developers', 'class': "small item" },
		support: {ref: supportWebsiteURL, html: 'Support', 'class': "small item"},
		press: {ref: websitePath+'press.html', html: 'Press', 'class': "small item" },
		about: {ref: websitePath+'company.html', html: 'About Us', 'class': "last small item" }
	};

	var logo = new Element('a', {href: websitePath, id: "logo", 'class': "ir", text: "TringMe"});
	logo.inject($(id));

	var tagLine = new Element('div', { id: "tagline", 'class': "ir", text: " "});
	tagLine.inject($(id));
	
	var userAuth = new Element('div', { id: "user_auth" });
	userAuth.inject($(id));
	var supportHeading = new Element('span', { 'class': "support_title" }).set('text','TringMe Support System');
	supportHeading.inject(userAuth);

	var navEl = new Element('div', { id: "nav"});
	for(i in nav_obj) {
		var elProp = nav_obj[i];
		elProp.href = getAbsoluteStaticPagesPath(elProp.ref);
		elProp.ref = null;
		elProp.id = i;
		var el = new Element('a', elProp);
		if(active==i)
			el.addClass('current');
		el.inject(navEl);
	}
	navEl.inject($(id));
}

function footer(id) {
	if(null==$(id))
		return;
	var footerLinks = [{ref: websitePath+"tringmetospp.html#pp", text: "Privacy&nbsp;Policy  | "},
		{ref: websitePath+"tringmetospp.html#tos", text: "TOS | "},
		{ref: websitePath+"company.html", text: "Company | "},
		{ref: "http://blog.tringme.com/", text: "Blog | "},
		{ref: websitePath+"checkrates.html", text: "Check Rates | "},
		{ref: websitePath+"contact.html", text: "Contact Us"}
	];
	var links = new Element('div', {id: "tringme_links"});
	links.inject($(id));
	var copyRight = new Element('div', {id: "tringme_copyright"});
	copyRight.inject($(id));
	var copyRightText = new Element('p', {html: "Copyright &copy; 2008 TringMe. All rights reserved."});
	copyRightText.inject(copyRight);
	var poweredByText = new Element('a', {href: websitePath+"developers.html", html: "Powered by: TringMe API"});
	poweredByText.inject(copyRight);

	var aEl = null;
	for(var i=0; i<footerLinks.length; i++) {
		aEl = new Element('a', {href: getAbsolutePath(footerLinks[i].ref), html: footerLinks[i].text});
		aEl.inject(links);
	}
}

function showHide(evt) {
	var aEl = evt.target;
	if("A"!=aEl.get('tag').toUpperCase())
		aEl = aEl.getParent();
	if('#'==aEl.get('href'))
		evt.stop();
	else
		return true;
	var subUl = aEl.getNext();
	if(subUl.hasClass('visible')) {
		subUl.removeClass('visible');
		return;
	}
	liEls = aEl.getParent().getParent().getChildren('li');
	liEls.each(function (item){
		item.getLast().removeClass('visible');
	});
	subUl.addClass('visible');
}

function fixCodeHeight() {
	var cols = null;
	var rowH = 0;
	var i = 0;
	$$('.code>.paramrow').each(function(item){
		rowH = 0;
		cols = item.getChildren();
		for(i=0; i<cols.length; i++) {
			if(rowH < cols[i].scrollHeight)
				rowH = cols[i].scrollHeight;
		}
		item.setStyle('height', rowH);
	});
}

var selectedYOffset = 0;

function repositionTextForImage() {
	$$('#dynamic-content>div.clearfix').each(function (el, index){
		var imgEl = el.getFirst('img');
		if(null==imgEl)
			return;
		if(imgEl.hasClass('ignore'))
			return;
		var childEl = el.getChildren();
		var h = 0;
		var sh = 0;
		for(var i=0; i<childEl.length; i++) {
			if(childEl[i] == imgEl)
				continue;
			h += childEl[i].offsetHeight;
			if(! isNaN(parseInt(childEl[i].getStyle('margin-top'))))
				h += parseInt(childEl[i].getStyle('margin-top'));
			if(! isNaN(parseInt(childEl[i].getStyle('margin-bottom'))))
				h += parseInt(childEl[i].getStyle('margin-bottom'));
			sh += childEl[i].scrollHeight;
		}
		var hEl = el.getFirst('h3');
		if(! isNaN(parseInt(hEl.getStyle('padding-top'))))
			h -= parseInt(hEl.getStyle('padding-top'));
		if(! isNaN(parseInt(hEl.getStyle('padding-bottom'))))
			h -= parseInt(hEl.getStyle('padding-bottom'));

		var pad = 0;
		if(h<imgEl.offsetHeight) {
			pad = parseInt((imgEl.offsetHeight - h) / 2);
			hEl.setStyle('padding-top', pad);
		} else {
			pad = parseInt((h - imgEl.offsetHeight) / 2);
			imgEl.setStyle('margin-top', pad);
			imgEl.setStyle('margin-bottom', pad);
		}
		//tringmelog(imgEl.get('src') + " : " + imgEl.offsetHeight + ", " + imgEl.scrollHeight + ", " + h + ", " + sh + ", " + pad);
	});
}

function tringmelog(obj) {
	if(window.console && console.log)
		console.log(obj);
	else if(window.opera && opera.postError)
		opera.postError(obj);
}

