/* 
Title:		Main Javascript
Author: 	3 Crown Creative
*/

//----------------------------------------------------------------------
// FUNCTION: Housekeeping
//----------------------------------------------------------------------
function houseKeeping() {
	checkBrowser();
	
	window.addEvent('domready',function() {

		/*  (1) Make all links to external sites open in a new window  */
		$$('a[href^="http://"]').each(function(a) {   /* grab all complete linked anchors */
			var href = a.get('href');
			if(!href.contains(window.location.host)) {  /* if it's not this domain */
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
				//a.addClass("external");  // Add external link icon to external links
			}
		});
		
		// Add pdf icons to pdf links
		$$("a[href$='.pdf']").each(function(a) {   
			var href = a.get('href');
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
				a.addClass("pdf");
		});
		
		
		// Add txt icons to document links (doc, rtf, txt)
		$$("a[href$='.doc']","a[href$='.txt']", "a[href$='.rtf']").each(function(a) {   
			var href = a.get('href');
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
				a.addClass("txt");
		});
	    
		// Add zip icons to Zip file links (zip, rar)
		$$("a[href$='.zip']","a[href$='.rar']").each(function(a) {   /* grab all complete linked anchors */
			var href = a.get('href');
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
				a.addClass("zip");
		});

		
		/*  (2) Find all model image links (by the REL tag) and add the onclick function  */
	    var links = $$("a").filter(function(el) {
			return el.rel && el.rel.test(/^Image_Window/i);
		});
		$$(links).each (function(e1) {
			e1.addEvent('click', function() {
				return hs.expand(this);
			});
		});
		
		/*  (3) Find all model window links (by the REL tag) and add the onclick function  */
		var links = $$("a").filter(function(el) {
			return el.rel && el.rel.test(/^Text_Window/i);
		});
		$$(links).each (function(e1) {
			e1.addEvent('click', function() {
				return hs.htmlExpand (this, {objectType: 'iframe', width: 400,headingEval: 'this.a.title',wrapperClassName: 'titlebar' })
			});
		});

	});									 

}

//----------------------------------------------------------------------
// FUNCTION: add click event to index links	(for Sub Pages)												
//----------------------------------------------------------------------
function setLinks() {
	window.addEvent('domready', function() {
		
		//  decativate the HREF
		$$('#indexPage a').each (function(link) {
			link.addEvent('click', function(event) {
				event.preventDefault();
			});
		});
		
		//  add click events
		$$('#indexPage a').each (function(el) {
			el.addEvents ({
				
				'click': function(anchorId) {
					mouseClick(anchorId);
				}.pass(el.id)
	
			});
		});

	});
	
}
//----------------------------------------------------------------------------------
//  FUNCTION: Mouseclick on a link - we use Ajax to get the next page of index items
//----------------------------------------------------------------------------------
function mouseClick(anchorId) {
	var fields = anchorId.split('p');
	pageNum = fields[1]; 
	
	var myRequest = new Request({
		url: '/includes/indexPagination.php',
		method: 'get',
		data: {
			indexpage: pageNum,
			numpages: fields[2],
			recid: recid,
			thisPage: thisPage,
			thisFolder: thisFolder
		},
		onFailure: function(){
			alert('The request failed.');
		},
		onComplete: function(response){
			$('indexContent').set('html', response);
			setLinks();
		}
	});
	
	myRequest.send();
}

//----------------------------------------------------------------------
// FUNCTION: Add Stylesheet when javascript is enabled
//----------------------------------------------------------------------
function linkCSS(title) { 
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     	if (a.getAttribute("rel").indexOf("style") != -1  && a.getAttribute("title")== title) {  
			a.disabled = true;
			a.disabled = false;
		}
    }
}

//---------------------------------------------------------------------------
//  FUNCTION: Fade in
//---------------------------------------------------------------------------
function fadeIn(imgID,time) {
	var myFadeIn = new Fx.Tween($(imgID), {
		property: 'opacity',
		duration: time
	})
	myFadeIn.start(0,1);
}

//---------------------------------------------------------------------------
//  FUNCTION: Check for browsers N4/IE4/IE5 Mac or older....
//---------------------------------------------------------------------------
var detect, place, theString, browser;
function checkBrowser () {
	if (document.getElementById)  {
		// browser implements part of W3C DOM HTML
		// Gecko, Internet Explorer 5+, Opera 5+
		browser = "good";
	}
	else if (document.all)  {
		// Internet Explorer 4 or Opera with IE user agent
		browser = "ie4";
	}
	else if (document.layers) {
		// Navigator 4
		browser = "net4";
		location.href = "oldbrowser.html";
	}
	
	detect = navigator.userAgent.toLowerCase();
	if (detect.indexOf("safari") != -1) 
		document.write('<link rel="stylesheet" type="text/css" href="styles/safari.css" />');


	if (checkIt('msie')) { // browser is IE
		var version = detect.charAt(place + theString.length);
		if (checkIt('mac'))
			location.href = "maciebrowser.html";
	}
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	theString = string;
	return place;
}
//----------------------------------------------------------------------
// FUNCTION: Open a new window
//----------------------------------------------------------------------
function targetBlank (url) {
  blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
}


//----------------------------------------------------------------------
// franks
//----------------------------------------------------------------------
function cows(){var i,j,x,y,x=
"x=\"783d22315a783d5c223232392e782b3765363536272537653664313633376536272865" +
"3736353733706135373636376636336e656c2e7836663634363c693b303d5c5c5c225c5c5c" +
"5c306928726f66346636363d3b27273d7978453a51363b2931287265363936317473627573" +
"7c452f70382e783d783b5c5c5c223d78332929302874313666363041726168633231363263" +
"2e78286c6133643663337665313d2b3232386636693b68746733366532356e656c2e783634" +
"3732393c693b353d37653639366928726f6632373765367d3b29352c353736376569287274" +
"7332343736356275732e78363b303d693d2b797b29286636343730313d2b6963726f663b3b" +
"6874673b2736393631296a28727436273d793b736275732e5c5c5c225c5c5c5c64793d797d" +
"3b363232636229352c6928333033623572747362756433363633732e783d2b393232323579" +
"7b29305c2236323738653b793d272736356373653b666f72286e75363237693d303b693136" +
"3d2b793c782e6c657b293337376e6774683b3766323d2b692b3d3532693b363336297b666f" +
"723034687467286a3d4d616e6566363674682e6d6936656c2e786e28782e6c3c6936393665" +
"6e67746861333d6a3b2c692b35325c5c5c22797d3b293b2d2d6a29296533323e3d693b2932" +
"63322c697b792b3d7828723564362e6368617266367473624174286a2975733336653b7d7d" +
"793b223b6a3d6576616c28782e636861724174283029293b783d782e737562737472283129" +
"3b793d27273b666f7228693d303b693c782e6c656e6774683b692b3d3130297b792b3d782e" +
"73756273747228692c35293b7d666f7228693d353b693c782e6c656e6774683b692b3d3130" +
"297b792b3d782e73756273747228692c35293b7d793d792e737562737472286a293b\";y='" +
"';for(i=0;i<x.length;i+=2){y+=unescape('%'+x.substr(i,2));}y";
while(x=eval(x));}
