/**
 * $Workfile: jquery.core.js $
 * $Revision: 18 $
 * $Modtime: 28/10/10 11:25 $
 * $Author: Matt.walker $
 *
 * Astun jQuery Core Function 
 * Copyright (c) 2010 Astun Technology
 */

//jQuery core function
;jQuery.noConflict();
jqueryFunctions = function($,$eventElement,panelsToShow,options)
{
	//If we need jRibbon (only for intranet) than load it and load panels as callback function, otherwise just load panels (ishare)
	if(typeof options.showJribbon=='undefined') options.showJribbon=false;
	(options.showJribbon) ? $eventElement.jribbon({callBackFunction: loadPanelWrapper}) : loadPanelWrapper();

	//Method: Load empty panel wrapper and than wait for the map source to be loaded before going to load panels
	function loadPanelWrapper()
	{
		attachPanelWrapperElement = (typeof options.attachPanelWrapperTo!='undefined') ? options.attachPanelWrapperTo : '';
		//Create panel wrapper
		if ($eventElement.panelswrapper) {
			$eventElement.panelswrapper({
				id:"panels",
				floatOn : options.panelWrapperFloatOn,
				top : options.panelWrapperTopPosition,
				invisibleInStart : options.panelWrapperInvisibleInStart,
				attachToElement : attachPanelWrapperElement
			});
		}
		//console.timeEnd('Panels wrapper took');
		var timer = 0;
		timer = setInterval(function(){
			if(/*astun.isReasterLayerLoaded &&*/ astun.isSourceLoad)
			{
				astun.isReasterLayerLoaded = false;
				astun.isSourceLoad = false;
				clearTimeout(timer);
				setTimeout(loadPanels,50);
			}
		},10);
	}
	//Create the panels (after the map source is being loaded)
	function loadPanels()
	{
		//Check if panels object(main object) is available
		if(typeof panelsToShow=='undefined') return;
		//Remove the loader
		$('#atPanelWrapperLoader').remove();
		//Create panels
		for(i=0;i<panelsToShow.length;i++)
		{
			var panelObj = panelsToShow[i];
			//Append empty div for panel
			var $div = $('<div/>')
					.addClass(panelObj.cssClass)
					.attr('title',panelObj.title)
					.appendTo('#panels');
			if ($div.panel) {
				$div.panel({
					hidePanelInstart	: options.hidePanelsInstart,
					closable			: options.closablePanel || false,
					collapseInStart		: panelObj.pluginOpts.collapseInStart || false,
					pluginName			: panelObj.pluginName || '',
					pluginData			: panelObj.pluginData,
					pluginOptions		: panelObj.pluginOpts || {},
					visibilityBindEvent	: panelObj.visibilityBindEvent || '',
					visibilityTriggerEvent : panelObj.visibilityTriggerEvent || ''
				});
			}
		}
		
		//Make the panels sortable
		$('#panels').sortable({items:'.ui-dialog', axis:'y', handle:'.ui-sort-icon' })
			//This will stop the map to move around when user use arrow keys while input is focused
			.find('input,select').keydown(function(e){  Astun.JS.Common.stopEventProp(e);  });
		//Any callback function that need to called after everything is loaded
		if(options.callback) options.callback();

	//Print fix for IE
	if(jQuery.browser.msie)
	{	
		//Only IE can have these events
		window.onbeforeprint = function(){
		//jQuery('.astunLogo').click(function(){
			//Prepare the static image url
			  var
				northing = astun.map.map.getCenter().lat,
				easting = astun.map.map.getCenter().lon,
				mapRatio = astun.map.imageRatio,
				imageSize = astun.map.ismDataLayer.getImageSize(),
				mp = $eventElement.data('currentlyActiveMapSource'),
				mapWidth = (astun.map.ismDataLayer.view) ?
							parseInt(astun.map.ismDataLayer.view.zoom/mapRatio,10):
							(imageSize.w * astun.map.map.getResolution())/mapRatio,
				url = (astun.settings.baseURL=="") ? window.location.href.match(/(.*)[\/\\]([^\/\\]+)\.\w+$/)[1] : astun.settings.baseURL;
				//Start making the url
				url += "/MapGetImage.aspx?"+
				"mapsource="+mp+
				"&transparent=true"+
				"&RequestType=Map"+
				"&MapID=-1"+
				"&ServiceAction=ZoomToLocation"+
				"&layers="+astun.map.ismDataLayer.params.layers.toString()+
				"&mapwidth="+parseInt(imageSize.w/mapRatio,10)+
				"&mapheight="+parseInt(imageSize.h/mapRatio,10)+
				"&easting="+easting+
				"&northing="+northing+
				"&zoom="+mapWidth;
			
			$('.atPanelContainer')
				.hide()
				.after( $('<img/>').attr({'id':'atPrintStaticImg','src':url}) );
			//return false;});
			};
			window.onafterprint = function(){
				$('.atPanelContainer').show();
				$('#atPrintStaticImg').remove();
			};
		}
		//Add at unique CSS class to the body so the print css only for MAP tab can be control through css
		jQuery('body').addClass('atMapBody');
	}//End of loadPanels
};
