/* expect to get a little lost because a bunch of the function is dynamically writen when called */
var loggedin = false;
var thisUrl = 'http://www.themodernform.com';

//window.onbeforeunload = function () {
//alert('bye');
//getAjaxPost('eval',thisUrl+'/cgi-bin/tmf_new.cgi');
//}

function scrollRoll(m,vis) {
	if (thisMain != m) {
		document.getElementById('scroll'+m).src = 'IMAGES/CONTENT/small_'+m+'_'+vis+'.jpg';
	}
}

function clickButton(m,s,a) {
	if (a == 'dot') {
		if (thisSub != s) {
			var lastDot = document.getElementById('dot'+thisSub);
			if (lastDot) {
				lastDot.className = 'button image_round';
			}
			document.getElementById('dot'+s).className = 'button_on image_round';
			//thisMain = m;
			//thisSub = s;
			$('#main_content').fadeOut(500,function(){swapContent(m,s,a);});
		}
	} else if (a == 'scroll') {
		if (thisMain != m) {
			//document.getElementById('scroll'+thisMain).className = 'scroll_content radius_small';
			//document.getElementById('scroll'+m).className = 'scroll_content_on radius_small';
			var lastClick = document.getElementById('scroll'+thisMain);
			if (lastClick) {
				lastClick.src = 'IMAGES/CONTENT/small_'+thisMain+'_off.jpg';
			}
			document.getElementById('scroll'+m).src = 'IMAGES/CONTENT/small_'+m+'_on.jpg';
			document.getElementById('loading_txt').innerHTML = m;
			//thisMain = m;
			//thisSub = s;
			$('#main_content').fadeOut(500,function(){swapContent(m,s,a);});
		}
	} else {
		$('#main_content').fadeOut(500,function(){swapContent(m,s,a);});
	}
	thisMain = m;
	thisSub = s;
	//alert(m+':'+s+':'+a);
	//$('#main_content').fadeOut(500,function(){swapContent(m,s,a);});
	//http://stackoverflow.com/questions/198892/img-onload-doesnt-work-well-in-ie7
}

function rollText(div,text) {
	document.getElementById(div).innerHTML = text;
}

function makeMail(e) {
	var eA = new Array();
	eA = e.split('');
	return 'mailto:'+eA.reverse().join('');
}

function bookmark() {
	var title = 'The Modern Form'; //document.title; 
	var url = thisUrl; //document.location.href;
	if (window.sidebar) { //firefox bookmark functionality
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { //ie favorite functionality
		window.external.AddFavorite( url, title); 
	} else if(window.opera){ //opera virtual sidebar link
		a = document.createElement("A");
		a.rel = "sidebar";
		a.target = "_search";
		a.title = title;
		a.href = url;
		a.click();
	}
}

function delicious() {
	window.open('http://www.delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent('http://www.themodernform.com')+'&title=The%20Modern%20Form', 'delicious','toolbar=no,width=550,height=550'); return false;
}

function login(l,p) {
	if (l && p) {
		getAjaxPost('eval',thisUrl+'/cgi-bin/tmf_new_login.cgi','l='+l+'&p='+p);	
	} else {
		login_disp();
	}
}
function login_disp() {
	if (loggedin) {
		document.getElementById('login_fields').style.display = 'none';
		loggedin = false;
	} else {
		document.getElementById('login_fields').style.display = 'inline';
		document.getElementById('login_l_f').value = '';
		document.getElementById('login_p_f').value = '';
		document.getElementById('login_l_f').focus();
		loggedin = true;
	}
}

function init() {
	//document.getElementById('zoom').onclick = function () { alert(thisMain+':'+thisSub); };
	document.getElementById('button_email').setAttribute('href',makeMail('moc.mrofnredomeht@guod'));
	document.getElementById('button_bookmark').onclick = function () { bookmark(); };
	document.getElementById('button_delicious').onclick = function () { delicious(); };
	document.getElementById('button_linkedin').setAttribute('href','http://www.linkedin.com/pub/doug-griffith/29/453/ba3');
	document.getElementById('button_xing').setAttribute('href','https://www.xing.com/profile/Doug_Griffith');
	if (!isStatic) {
		swapContent(thisMain,thisSub,'init');
	}
	makeOverlay();
	makeDiv();
	//alert('poo');
	
}

function swapContent(m,s,a) {
	//alert(m+':'+s+':'+a);
	document.getElementById('loading').style.visibility = 'visible'; 
	getAjaxPost('eval',thisUrl+'/cgi-bin/tmf_new_ajax.cgi','m='+m+'&s='+s+'&a='+a);	
}

function myVoid() {}

/* ### overlay ### */

function overlay(vis,typ,data) {
	e=document.getElementById('overlay');
	e.style.visibility = vis;
	document.getElementById('popBigContent').innerHTML = ""; 
	if (vis == 'visible') {
		document.getElementById('popup').style.display='block'; 
		if (typ == 'iframe') {
			createiFrame(data+'&m='+thisMain+'&s='+thisSub);
		} else {
			document.getElementById('popBigContent').innerHTML = '<img src="IMAGES/CONTENT/zoom_'+thisMain+'_'+thisSub+'.jpg" id="main_image_zoom" onLoad="fadeImageIn(\'#main_image_zoom\');" />';
		}
	} else {
		document.getElementById('popup').style.display='none';
	}
}

function createiFrame(url) {
	var el = document.createElement("iframe");
	el.setAttribute('id', 'ifrm');
	el.setAttribute('width', '744px');
	el.setAttribute('height', '428px');
	el.setAttribute('scrolling', 'no');
	el.setAttribute('frameBorder', 0);
	el.setAttribute('marginWidth', 0);
	el.setAttribute('marginHeight', 0);
	el.style.position = "absolute";
	el.style.display = "block";
	el.style.top = '50px';
	el.style.left = '50px';
	document.getElementById('popBigContent').appendChild(el);
	el.setAttribute('src', 'http://www.'+url);
}

function makeOverlay() {
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'overlay');
	newdiv.setAttribute('class', 'overlay');
	document.body.appendChild(newdiv);
}

function makeDiv() {
	var divTag1 = document.createElement('div');
	divTag1.setAttribute('id', 'popup');
	divTag1.setAttribute('class', 'popBig');
	var divTag2 = document.createElement('div');
	divTag2.setAttribute('id', 'popBigContent');
	divTag2.setAttribute('class', 'popBigContent');
	var divTag3 = document.createElement('div');
	divTag3.setAttribute('class', 'popCloseImage');
	var imgTag1 = document.createElement('img');
	imgTag1.setAttribute('src', 'IMAGES/blanket_close.png');
	imgTag1.onclick = function (){ overlay('hidden'); } 
	divTag1.appendChild(divTag2);
	divTag3.appendChild(imgTag1);
	divTag1.appendChild(divTag3);
	//document.body.appendChild(divTag1);
	document.getElementById('wrapper').appendChild(divTag1);
}

function fadeImageIn(whichDiv,close) {
	$(whichDiv).fadeIn(500);
	if (close) {
		setTimeout(function() { $(whichDiv).fadeOut(500); },3000);
	}
}

function stars(x) {
	for(i=1; i<6; i++) { 
		if (x >= i) document.getElementById('r'+i).src = "IMAGES/RATESTARS/star1.gif";
	}
}

function noStars() {
	for(i=1; i<6; i++) { 
		document.getElementById('r'+i).src = "IMAGES/RATESTARS/star0.gif";
	}
}

function rate(r) {
	getAjaxPost('eval',thisUrl+'/cgi-bin/tmf_new_ajax.cgi','r='+r);
	document.getElementById('rating').innerHTML = "thanks!";
}

function contact(n,e,m) {
	if (m == '') {
		document.getElementById('message_notify').value='message field is empty';
		return false;
	} else if (e.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		getAjaxPost('eval',thisUrl+'/cgi-bin/tmf_new.cgi','s=contact&n='+n+'&e='+e+'&m='+m);
		document.getElementById('email_notify').innerHTML = '<b>message sent - thank you</b>';
	} else {
		document.getElementById('email').value='invalid email address';
		return false;
	}
}
