/*  
	ajaxLoader plugin
	created		jbenson	2011NOV04 - jsfiddle.net/deepimage/3Y9kx/11/ - closure-compiler.appspot.com/home
	updated
	example use: ajaxLoader.show() or ajaxLoader.hide()
*/
var ajaxLoader=function(a){var c={instances:[],create:function(){this.instances.push(a("<div></div>").hide().addClass("status").appendTo(document.body).css({width:this.width(),height:this.height()}))},width:function(){return a(window).width()},height:function(){return a(window).height()}};return{show:function(){c.create();var b=a([]);a.each(c.instances,function(){b=b.add(this)});b.show()},hide:function(){var b=a([]);a.each(c.instances,function(){b=b.add(this)});b.remove();c.instances=[]}}}(jQuery);


var customcode = '/includes/customcode/',						// to the custom code location
	getCurrentSite = function() { return $('#currentSite').text() },
	getCurrentPage = function() { return $('#currentPage').text() };
	

$(document).ready(function() {

	var newWindow = $('#newwindow'),
		toolTip = $('a.ToolTip');

	if ( newWindow.length ) {
		startwin = function(winid) {
			element = $("#"+winid);
			$('#newwindow_content').html( element.html() );  
			$('#newwindow_title').html( element.attr ("title") );
			newWindow.jqmShow();
		};
	}
	
	if ( toolTip.length ) {
		toolTip.cluetip({local:true, cursor: 'pointer', dropShadow:true, dropShadowSteps: 0});
	}
	
	if (isMobile()) {
		$('.mod_slideshow_slide img').css('height','auto');	
		if (checkLogin()) {
			$('#mLogout').show();
		}
	}
	
	$(document).keyup(function(event) {
	
		if ( (event.keyCode ? event.keyCode : event.which) === 27 ) {				// determines if the 'esc' key was pressed			
			if ( window.location.search.indexOf('edit') != -1 )	{					// edit mode - if query string has attribute 'edit'
				window.location = removeQSParameter(window.location.toString().toLowerCase(),'edit',false);
			} else {																// normal mode - if query string does not contain 'edit'
				window.location = addQSParameter(window.location,'edit');
			}
		}
			
	});
	
});



Right = function(str, n) {
	if (n <= 0) {
		return "";
	} else if (n > String(str).length) {
		return str;
	} else {
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n); 
	}
};
			

// reloads a module on a page, required argument item which should be the module cross id
reloadModule = function(item, successFunction) {

	var  module = $('#'+item)
		,area = module.parent()
		,isEditable = false
		,isAreaEditable = false;
	
	if ( window.location.search.toLowerCase().match('edit') ) {
		isEditable = true;
	}
	
	if( module.find(".module-edit-menu") ) {
		isAreaEditable = true;
	}

	$.ajax({
		 type:'post',url:'/sitemanager/ajax/display_module.cfm'
		,data:{page:getCurrentPage(),module:item,editable:isEditable,areaEditable:isAreaEditable},cache:false
		,success:function(data,textStatus){
			$(data).replaceAll('#'+item);				// update module's  content
			
			if ( $.isFunction(successFunction) ) {
				successFunction(data, textStatus);
			}
			
			if ( typeof(pp) == 'undefined' ) {
				pp = $(area).find("a[rel^='prettyPhoto']");
				if ( pp.length ) {
					pp.prettyPhoto({counter_separator_label:' of ',show_title:false});
				}
			}
					
			if (typeof(bindModuleEditMenuEvents) == 'function') {
				bindModuleEditMenuEvents($(area));
			}
			
			$(document).trigger('reloadModuleEvent', $('#'+item));

		}
	});
	
	if ( area.hasClass('.ui-sortable') ) {				// checks to see if sortable has been binded to current area
		area.sortable('refresh');						// refresh sortables for current area
	}
	
};


/*
	isMobile		return true if a mobile screen size is detected	

	Attributes
	none
		
	History
	2011JUL15 	jbenson		created function
*/
isMobile = function() {
	if ( $(document).attr('width') <= 730) {
		return true;
	}		
	return false;
};
 

/*
	addQSParameter	 	will add name / value pair of choice from query string from within a given url
						also preserving any anchor values found in url
	
	Attributes
	url 		(required) - full url including query string
	parameter 	(required) - name and/or value of query string parameter to add. example: edit or edit=true
	
	History
	2010DEC16 	jbenson		created function
*/
var addQSParameter = function(url,parameter) {

	if ( url.href.indexOf('#') != -1 )
		return url.href.split('#')[0] + ( url.search.length ? '&'+parameter : ( url.href.indexOf('?') != -1 ? parameter : '?'+parameter ) ) + '#' + url.href.split('#')[1]; 
	else
		return url.href + ( url.search.length ? '&'+parameter  : ( url.href.indexOf('?') != -1 ? parameter : '?'+parameter ) );
		 
};


/*
	removeQSParameter	will remove name / value pair of choice from query string from within a given url
	
	Attributes
	url 		(required) - full url including query string
	parameter 	(required) - name of query string parameter to remove
	hasValue 	(optional) - default -> true, if name parameter does not have a value
	
	History
	2010NOV05 	jbenson		created function
	2010DEC16	jbenson		updated function to preserve any anchor values found in url. example: #top
*/
var removeQSParameter = function(url,parameter,hasValue) {  

	hasValue = typeof(hasValue) != 'undefined' ? hasValue : true;
	
	var anchorTag = '';
	var urlparts = url.split('?');
		
	if ( urlparts.length == 2 ) {
	
	    var prefix = encodeURIComponent(parameter);
	    
	    var pars = urlparts[1].split(/[&;]/g);
	    
	    // check for anchor tag
	    if ( pars.indexOf('#') == -1 ){	    	
			anchorTag = '#' + pars.toString().split('#')[1];
			pars = [pars.toString().split('#')[0]];
	    }
        	
	    if ( hasValue ) {
	    	prefix +'=';
	    }
	    
	    for ( var i= pars.length; i-->0; ) {            
	        if ( pars[i].lastIndexOf(prefix, 0) !== -1 ) {
	            pars.splice(i, 1);
	        }
	    }
	            
	    if ( pars.length ) {
	    	return urlparts[0]+'?'+pars.join('&') + anchorTag;
	    }
	    
	    return urlparts[0] + anchorTag;
	    
	}
	
	return urlparts[0] + anchorTag;
	
};


// console.log, refined. Thanks to PF!
debugLog = function(msg) {
	if (typeof(console) != 'undefined' && typeof(console.log) != 'undefined') {
		console.log(msg);
	}
}

// Added for checking to see if a user is logged in from JavaScript
checkLogin = function() {
    var passlogin = false;
    $.ajax({
        type: "POST",
        url: "/mobile/isLoggedIn.cfm",
        data: "",
        success: function(msg){
        	if (msg.trim() == 1) {
        		passlogin = true;
        	}
        	else {
        		passlogin = false;
        	}
        },  // close success
        async: false,   //forces it to wait for a response
        timeout: 5000
    }); //close .ajax call
    return passlogin;
};  //close checkLogin
