var echo = function(txt){try{console.log(txt);}catch(e){}};
var core = {
	common : {
		init     : function(){
			$('.do-dialog, #btn_microsite')
			.click(buildExecuteDialog);
			/*setInterval(function(){
				if( location.hash.length == 0 ) return;
				if( location.hash.replace(/^.*#/, '') !== oldhash ){
					pagehash( location.hash.replace(/^.*#/, '') );
				}
			},100);*/
			
			$.historyInit(pageload);
			
		},
		finalize : function(){
			//buildExecuteDialog();
			try{
			if( $.browser.msie && $.browser.version < 7 ){
				if(document.documentElement && document.documentElement.scrollTop){
					document.documentElement.scrollTop = 0;
				}
				else if (document.body && document.body.scrollTop){
					document.body.scrollTop = 0;
				}
			}
			}catch(e){}
		}
	}
}
$(window).load(function(){
try{
	if( $.browser.msie && $.browser.version < 7 ){
		if(document.documentElement && document.documentElement.scrollTop){
			document.documentElement.scrollTop = 0;
		}
		else if (document.body && document.body.scrollTop){
			document.body.scrollTop = 0;
		}
	}	
}catch(E){}
});
var oldhash = null;
pagehash = function( hash ){
	oldhash = hash;
	
	$.historyLoad(oldhash);
}
pageload = function( hash ){
	echo( hash.substr(1) );
	var myregexp = new RegExp("pressureulcers/");
	var match = myregexp.exec(hash);
	
	if( match != null ){
		if( jqdialog == null ){
			buildExecuteDialog(hash);
		}
		else{
			var iframe = jqdialog.find('iframe')[0];
			iframe = iframe.contentDocument || iframe.contentWindow.document;
			
			//iframe.location.replace('http://stoppain.dev3.dev.informationexperts.com/'+hash.substr(1));
			iframe.location.replace('http://www.stoppain.org/'+hash.substr(1));
		}
	}
}

var jqdialog = null;
var buildExecuteDialog = function( hash ){

	$(document).scrollTop(0);
	$(window).scrollTop(0);
	$('body, html').css('overflow', 'hidden');
	$('select').hide();
	jqdialog = $('<div class="dialog">').appendTo('body');
	var url = null;
	if( typeof hash === "string" ){
		url = hash.replace(/^.*#/, '').substr(1);
	}
	else{
		if( typeof this.href == 'undefined'){
			url = 'pressureulcers/index.asp';
		}else{
			url = this.href;
		}
	}
	
	var myregexp = new RegExp("pressureulcers/");
	result = url.split(myregexp);	
	var hash = null;
	
	if( result[1] === 'index.asp' ){
		hash = '/pressureulcers/';
	}
	else{
		hash = '/pressureulcers/' + result[1];
	}
	location.hash = hash;
	
	jqdialog
	.dialog({
		bgiframe: true,
		autoOpen: false,
		height: 520,
		width: 725,
		modal: true,
		zIndex : 10000,
		title : 'Preventing and Caring for Pressure Ulcers',
		position: ['50%', 25],
		close: function() {
			$('body, html').css('overflow', '');
			$('select').show();
		},
		open: function(event, ui) {
			
			jqdialog.empty().append('<iframe scrolling="no" frameborder="0" allowtransparency="1" src="'+url+'"></iframe>');

			var p = $(this).parents('.ui-dialog');
			
			p
				.removeClass('ui-corner-all')
				.find('.ui-corner-all')
					.removeClass('ui-corner-all')
			.end()
				.find('.ui-dialog-title')
					.addClass('ir')
			.end()
				.find('.ui-dialog-titlebar-close')
					.after('<a href="" id="btn_expand" class="ir"><span class="">Expand</span></a>');
					
			p.find('#btn_expand')
			.toggle(function(){
				// help to get around a jquery bug for firefox and opera
				var div = $('<div style="position:absolute;top:0;left:0;width:100%;height:100%;">').appendTo('body');
				var h = div.height();
				div.remove();
				
				var dh = jqdialog.height();
				
				// for what ever reason the dialog box in IE
				// is getting an incorrect or correct height to start
				// so we need to account for the title bar in all other
				// browsers
				if( $.browser.msie ){
					dh = dh+37;
				}
				
				div.remove();

				if( typeof $(this).data('height') == 'undefined' ){
					$(this).data('height', dh);
				}
			
				jqdialog
					.dialog('option', 'width', '100%')
					.dialog('option', 'height', '100%')
					.dialog('option','position',['left','top'])
						.height( h-53 );
						
				$(this).addClass('active');
			},function(){
				jqdialog
					.dialog('option', 'width',  725)
					.dialog('option', 'height', 520)
					.dialog('option','position',['50%', 25])
					.height($(this).data('height'));
					
				$(this).removeClass('active');
			});
				
			try{
				jqdialog.find('iframe').load(function(){
					$(this).contents().find('.close').click(function(){
						jqdialog.dialog('close');
						return false;
					});
				})
			}catch(e){
				console.log(e);
			}
		},
		resizeStart: function(event, ui) {
			jqiblock = $('<div class="iblock">').appendTo( jqdialog );
		},
		resizeStop: function(event, ui) {
			jqiblock.remove();
		},
		dragStart: function(event, ui) {
			jqiblock = $('<div class="iblock">').appendTo( jqdialog );
		},
		dragStop: function(event, ui) {
			jqiblock.remove();
		}
	}).dialog('open');
	
	return false;
}

UTIL = {
  fire : function(func,funcname, args){
    var namespace = core;
 
    funcname = (funcname === undefined) ? 'init' : funcname;
    if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
      namespace[func][funcname](args);
    } 
  }, 
 
  loadEvents : function(){
    var bodyid = document.body.id;
    // hit up common first.
    UTIL.fire('common');
    // do all the classes too.
    $.each(document.body.className.split(/\s+/),function(i,classnm){
      UTIL.fire(classnm);
      UTIL.fire(classnm,bodyid);
    });
    UTIL.fire('common','finalize');
  } 
};
 
// kick it all off here 
$(document).ready(UTIL.loadEvents);