$(document).ready(function() { // As soon as the document loads, do this...
	doQuickly();
	loadScripts();
});

function doQuickly() {
	killFrames();
}

function killFrames() {
	// If the page is loaded in a frame, bypass the frame and reload in the top of the browser.
	if (top != self) {
		top.location.href = self.location.href
	}
}

function loadScripts() {
	// Load script files and jQuery plugins separate from the HTML. 
	// http://plugins.jquery.com/project/xLazyLoader
	$.xLazyLoader({
		js: [
		'/resources/preview/includes/jquery.url.packed.js',
		'/resources/preview/includes/jquery.ifixpng2.js',
		'/resources/preview/includes/jquery.bgiframe.min.js',
		'/resources/preview/includes/superfish.js'
		],
		/*
		css: [''],
		img: [''],
		name: '',
		*/
		success: function(){
			highlightCurrentPage(); 
			customizeBySection(''); 
			customizeByPage();
			modifyHtml();
		},
		error: function(){
			
		},
		each: function(url){
			
		}
	
	});
}

function highlightCurrentPage() {
	// Mark up the current page's section link in the navigation
	// http://plugins.jquery.com/project/url_parser
	$('#navigation a').each(function() {
		var navUrlSection = jQuery.url.setUrl(this.href).segment(0);
		if(navUrlSection === jQuery.url.setUrl(location.href).segment(0)) {
			$(this).parent('li').addClass('selected');
		}
	});
	// Mark up the current page's link in the subnavigation
	$('#subnavigation a').each(function() {
		if(this.href === jQuery.url.attr('source')) {
			$(this).parent('li').addClass('selected');
		}
	});
}

function customizeBySection(sectionName) {
	// Mark up the body tag with the section name (top-level directory) to uniquely style child tags.
	// http://plugins.jquery.com/project/url_parser
	if(sectionName.length > 0) {
		$('body').addClass(sectionName);
	} else {
		$('body').addClass(jQuery.url.segment(0));
	}
}

function customizeByPage() {
	// Customize specific pages in various ways.
	switch (jQuery.url.attr('file')) {
		case 'hotels_motels-test.html':
			markupLinks();
			break;

		default:
					
	}
}

/*
function loadHomepageFlash() {
	// Load the homepage Flash with swfobject.
	var flashvars = {dataFile: 'includes/monoslideshow.xml', showRegistration: 'false', showLogo: 'false'};
	var params = {bgcolor:'#ffffff',wmode:'opaque'};        
	var attributes = {};
	swfobject.embedSWF("includes/monoslideshow.swf", "placeholder", "560", "315", "9.0.0", false, flashvars, params, attributes);
}
*/

function modifyHtml() {
	// For each list, mark up the first and last items.
	$('ul,ol,dl').each(function() {
		$(this).children('li:first,dt:first').addClass('first');
		$(this).children('li:last,dt:last').addClass('last');
	});
	
	//loadHomepageFlash();
	$('#navigation-menu').superfish().find("ul").bgIframe({opacity:false}); // add this bit for bgIframe support only

	$.ifixpng('http://www.visitkure.com/resources/.template/images/spacer.gif');
	$('#copyright img, #mainContent, #contentWrapper, #copyright .text, #logo, #navigation a').ifixpng();
	
	//markupLinks();
}

//Dreamweaver Behaviors
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function markupLinks() {
	var fileTypes = {			
		htm: 'docWEB',
		php: 'docWEB',
		asp: 'docWEB',
		jsp: 'docWEB',
		pdf: 'docPDF',
		doc: 'docWOR',
		pag: 'docWOR',
		rtf: 'docWOR',
		txt: 'docWOR',
		xls: 'docXLS',
		csv: 'docXLS',
		ppt: 'docPPT',
		key: 'docPPT',
		zip: 'docZIP',
		gz:  'docZIP',
		pkg: 'docZIP',
		rar: 'docZIP',
		sit: 'docZIP',
		jpg: 'docIMG',
		gif: 'docIMG',
		png: 'docIMG',
		bmp: 'docIMG',
		tif: 'docIMG',
		psd: 'docIMG',
		svg: 'docIMG',
		eps: 'docIMG',
		ai:  'docIMG',
		mov: 'docVID',
		mp4: 'docVID',
		mpg: 'docVID',
		rm:  'docVID',
		swf: 'docVID',
		wmv: 'docVID',
		flv: 'docVID',
		asf: 'docVID',
		avi: 'docVID',
		mp3: 'docMUS',
		m3u: 'docMUS',
		mpa: 'docMUS',
		ra:  'docMUS',
		wav: 'docMUS',
		wma: 'docMUS'
	};
	var fileDescriptions = {
		docWEB: '',
		docPDF: 'PDF document',
		docWOR: 'word-processing document',
		docXLS: 'spreadsheet document',
		docPPT: 'slideshow document',
		docZIP: 'zipped file',
		docIMG: 'image file',
		docVID: 'video file',
		docMUS: 'music file'
	};
	$('#mainContent a').each(function() {
 
		// get a jQuery object for each anchor found
		var $a = $(this);
		
		// get the href attribute
		var href = $a.attr('href');
		// remove query strings because they could contain periods
		var hrefAndQueries = href.split('?');
		
		// get the extension from the href
		var hrefArray = hrefAndQueries[0].split('.');
		var extension = hrefArray[hrefArray.length - 1];
		extension = extension.slice(0,3);
		
		var protocol = hrefArray[0].slice(0,7);
		switch (protocol) {
			case 'mailto:':
				$a.addClass('mailto');
				$a.append('<span class="accessible">. ' + 'email' + '</span>');
				break;
			case 'https:/':
				$a.addClass('secure');
				$a.append('<span class="accessible">. ' + 'secure site' + '</span>');
				break;
			default:
var documentdotdomain = 'visitkure';
				if (href.match(/^http/) && !href.match(documentdotdomain)) {
					$a.addClass('external');
					$a.append('<span class="accessible">. ' + 'external site' + '</span>');
				} else {
					$a.addClass('internal');				
					var fileType = fileTypes[extension];
					var fileDescription = fileDescriptions[fileType];
					if (fileType) {
						$a.addClass(fileType);
						$a.append('<span class="accessible">. ' + fileDescription + '</span>');
					}
				}
				break;				
		}
	});
}