// modal window w & h
var w = ''; 
var h = '';
var root_URL = "http://www.kathleendargis.com/stage/"
		
function addMovie(title, source, autoplay){
	// add video title
	$("#mod_wrap h2").html(title);

			jwplayer("mod_content").setup({
				flashplayer: "swf/player-licensed.swf",
				file:root_URL + source,
				autostart: autoplay,
				image: "",
				height: h,
				width: w, 
				bufferlength: 5
			});
}


function addHTML(source){

$.get(source, function(data) {
  $('#mod_content').html(data);
  //alert('Load was performed.');
  var addr = "info@kathleendargis.com";
  $(".write_email").html('<a href="mailto:"'+ addr +'">'+ addr +'</a>')
});

}


function setDimensions(el){
	var dims = el.attr('rel')
	dims = (dims.split(";"));
	for (i=0; i < dims.length; i++){
			//loop through array, find "width"
		if(dims[i].search('width') != -1){ // if this array el contains width, 
			w = dims[i].substring(6)
		} else if(dims[i].search('height') != -1){ // if this array el contains height 
			h = dims[i].substring(7)
		}
	}
}


function hashCheck(){

	var hash=location.hash
	if(hash != ''){
	var el ='';
		hash = hash.substring(1)
		hash = hash.replace("&", "/"); // our custom directory characters with actual directory char
		hash = hash.replace("%20", " "); // replace with actual whitespace
		//console.log(hash);
		$('.video').each(function(){
			if($(this).attr('href') == hash){
				el = $(this);
				setDimensions(el);
				var vid_src = el.attr('href');
				var vid_title = el.attr('title');
				Modal.show(w, h, 'addMovie("' + vid_title + '", "' + vid_src + '", true)');
				return false
			}
		});
	}
}



$(document).ready(function(e) {
						   
	def_doc_title = $('title').html();						   
						   
	$('.video').click(function(ev){
		ev.preventDefault();
		//get and set video dimensions
		setDimensions($(this));
		var vid_src = $(this).attr('href');
		var vid_title = $(this).attr('title');
		Modal.show(w, h, 'addMovie("' + vid_title + '", "' + vid_src + '", true)');
		var hash_src = vid_src.replace("/", "&")
		window.location.hash = hash_src;
		$('title').html(vid_title);
		return false;
		
	});

	setTimeout("hashCheck()", 800); 
	// make sure we're loaded and then fire
	
	
$('.html_page').click(function(ev){
	ev.preventDefault();
 	var html_src = $(this).attr('href');
	setDimensions($(this));
	Modal.show(w, h, 'addHTML("' + html_src +'")');
	return false;
});

});
