$(document).ready(function(){
    //INITIATE SMOOTH SCROLLING
    $.localScroll();
    
    //INITIATE COLORBOX
    $('a[class*=modal]').colorbox();
    $('a[rel*=lightbox]').colorbox();
    $('a[class*=modal-feedback]').colorbox({width:"520px", height:"440px", title:true, iframe:true});
    
});

$(window).load(function(){ // when the window has loaded
// LOAD SLIDING PANEL CONTENT VIA AJAX
$("#nav-panel").before("<div id='panelwrap'></div>"); // create empty panel container div
var loader = "<div id='loader'>LOADING CONTENT</div>"; // specify loading animation variable
var closebtn = "<a id='closebtn'>close</a>"; // specify close button variable
var overlay = "<div id='overlay'></div>"; // specify close button variable
$('a[rel*=panel]').click(function(e){ // find a tag whose rel attribute begins exaclty with the string 'panel'
	e.preventDefault(); // cancel default hyperlink behaviour
	//$("body").prepend(overlay);
	$("#panel").prepend(loader); // prepare loding animation
	//$("#overlay").css('display', 'block');
	$("#loader").slideDown("normal"); // fade in loading animation
	$("#closebtn").remove();
	var myDIV = " #" + $(this).attr("rel").substring(5);
	var myURL = $(this).attr("href")+ myDIV; // get href, and extract anchor DIV whose name comes after panel (eg. #panelDIVNAMEHERE)
	$('a[rel*=panel]').removeClass('selected'); // remove css class from all tabs
	$(this).addClass('selected'); // add css class to selected tab
	closePanel();
	$("#panelwrap").load(myURL,function(){ // load content via ajax
		// $("#util").addClass('hidden'); // hide utility nav
		$("#panelwrap").slideDown("normal"); // after content is loaded, slide the panel down
		$("#loader").slideUp("normal", function() { // fade out the loading animation
			$(this).remove(); // after the loading animation has faded out, remove it
		});
		$("#panelwrap " + myDIV).after(closebtn); // prepare loding animation
		// Close panel by pressing close button
		$("a#closebtn").click(function(){
			closeFunction();
		});
		$("#overlay").click(function(){
			closeFunction();
		});
	});
});

// Close panel function
function closePanel() { 
	$("#panelwrap").slideUp("normal", function(){ // slide the panel up
		$("#panelwrap").hide(); // after the panel has slid upward, hide it before new content is loaded into it
	});
    return false;
};
// Close panel function
function closeFunction() { 
	closePanel();
	$('a[rel*=panel]').removeClass('selected');
	// $("#util").removeClass('hidden'); // show utility nav
	$("#closebtn").remove(); // remove close button
	$("#overlay").remove(); // remove close button
};

// Close panel by pressing the 'esc' key
$(document).keyup(function(event){
	if (event.keyCode == 27) {
		closeFunction();
	};
});// end ajax page loading widget

// AUTOERASE TEXTFIELDS
$("input[rel='erase']").each(function() {  
    $(this).val($(this).attr("title"));  
    if($.trim($(this).val()) == "")  
    $(this).val($(this).attr("title"));  
    $(this).focus(function() {  
        if($(this).val() == $(this).attr("title")) $(this).val("");
    })  
    .blur(function() {  
        if($.trim($(this).val()) == "") $(this).val($(this).attr("title"));
    });  
});// end autoerase textfields

//JQUERY UI DIALOG REMOTE LOADING
$(function (){
	$('a[rel*=dialog]').click(function() {
		var myURL = $(this).attr("href")+ " #modalcontent"; // get hyperlink and append anchor
		var myTitle = $(this).attr("title");
		var dialog = $('<div style="display:hidden" title="'+myTitle+'"></div>').appendTo('body');
		// load remote content
		dialog.load(myURL, {}, function (responseText, textStatus, XMLHttpRequest) {
			dialog.dialog({modal: true, bgiframe: true, resizable: false, width: 460});
		});
		//prevent the browser to follow the link
		return false;
	});
});

//JQUERY CUSTOM CAPTIONS
$('.withcaption').each(function(i) {
        var photo = $('img', this);
        var photoHeight = photo.attr('height');
        var photoWidth = photo.attr('width');
        var caption = $(photo).attr('title');
        $(photo).wrap("<div class='photowrap'></div>");
        $('.photowrap',this).append("<div class='captionbox'>" + caption + "</div>");
        var captionHeight = $('div.captionbox', this).outerHeight();
        $(this).height(photoHeight);
        $(this).width(photoWidth);
        $(this).hover(function() {
        $('.captionbox', this).stop().animate({marginTop : -captionHeight+1}, 300);
            }, function() {
            $('.captionbox', this).stop().animate({marginTop : '-2.3334em'}, 300);
        });
    }); // end caption
    
//
}); // end on window load

/* The following functions are used by tinyMCE, the HTML editor, for inserting media objects */
function writeFlash(p) {
	writeEmbed(
		'D27CDB6E-AE6D-11cf-96B8-444553540000',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'application/x-shockwave-flash',
		p
	);
}

function writeShockWave(p) {
	writeEmbed(
	'166B1BCA-3F9C-11CF-8075-444553540000',
	'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
	'application/x-director',
		p
	);
}

function writeQuickTime(p) {
	writeEmbed(
		'02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
		'video/quicktime',
		p
	);
}

function writeRealMedia(p) {
	writeEmbed(
		'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'audio/x-pn-realaudio-plugin',
		p
	);
}

function writeWindowsMedia(p) {
	p.url = p.src;
	writeEmbed(
		'6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
		'application/x-mplayer2',
		p
	);
}

function writeEmbed(cls, cb, mt, p) {
	var h = '', n;

	h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
	h += typeof(p.id) != "undefined" ? ' id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? ' name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? ' width="' + p.width + '"' : '';
	h += typeof(p.height) != "undefined" ? ' height="' + p.height + '"' : '';
	h += '>';
	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';
	
	h += '<object  type="' + mt + '"';
	h += typeof(p.src) != "undefined" ? ' data="' + p.src + '"' : '';
	h += typeof(p.id) != "undefined" ? ' id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? ' name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? ' width="' + p.width + '"' : '';
	h += typeof(p.height) != "undefined" ? ' height="' + p.height + '"' : '';
	h += '>';

	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';

	h += '</object></object>';

	document.write(h);
}
/* end of tinyMCE media functions */
