
//alert('loading core');

// init o
if (typeof(window['o']) == "undefined") {
	o={ a:{}, debug: 0, error : [], require : [], f : [] };
}


/// framework code ----------------------------------------------------------------------\\\\\\\\\\\\\\\
		
	function key() { 
		if(o.error.length>0) { handle(o); };
		if(o.debug) { debug(o); };
		//updatePreloader(); //plugin
		if (o.f.length>0) { // 
		try { 
			var me = o.f.shift(); me(o); 
		} catch (e) { o.error.push(e.message); handle(o); };
		}
	}	
		
	function f(a) {
		_function = a; // a function to add to the cue
		if (typeof _function == 'function') {
			o.f[o.f.length] = _function;
		}
	}
	/*
	function see(o) { 
			try{
				for (var v in o) { if (v!='f'&&v!='error'&&v!='require'&&v!='rate'||o.debug) { jQuery('#status').html(v+'='+o[v]); } }; 
			} catch(e){
				for (var v in o) { if (v!='f'&&v!='error'&&v!='require'&&v!='rate'||o.debug) { document.write(v+'='+o[v]); } };
			}
	};	
		
	function see(a) {
	var newString = '';
		for (var prop in a) { 
			newString += prop+' = '+a[prop]+' %%% '; 
		}
	return newString;
	}	*/

function see(o) {
		alert('object: '+jQuery.toJSON(o));
	}
	
	function trace(a) {
		_string = a;
		_debug = o.debug;
		
		switch (_debug) {
			case 0: break;
			case 1:
			case 2:
				try{ jQuery('#debug').show(); jQuery('#debug').text(_string); } catch(e) { alert(_string); };
			break;
			case 3:
				alert(_string);
			break;
		}
	}
	
	function debug(o) {
		
		switch (o.debug) {
			case 0: break;
			case 1:
				jQuery('#debug').show();
				if (o.f.length>0) {
					jQuery('#debug').html( 'doing :'+o.f.length+': '+ o.f[0] );
				} else {
					jQuery('#debug').html( 'done');
				};
			break;
			case 2:
			case 3:
				if (o.f.length>0) {
					alert('Debug Mode :'+o.f.length+': '+ o.f[0]);
				} else {
					alert( 'done');
				};
			break;
			case 4:
				//console.log(o.f[0].toString());
			break;
		}
	}
	function handle(o) {
		//try{
			jQuery('#status').html( 'error: '+o.error.pop() );
		/*} catch(e){
			try {
				see(o.error);
			} catch(e) {*/
				//alert('error: '+o.error.pop());
			/*}
		}*/
	}
	
function commit(a) {
		var _a = a;
		var _query = '';
		
		_query += 'task=save';
		if (a.id) { _query +='&id='+a.id;}
		if (a.name) { _query +='&name='+a.name;}
		if (1) { _query +='&region1='+a.region1;}
		if (1) { _query +='&region2='+a.region2;}
		if (1) { _query +='&region3='+a.region3;}
		if (1) { _query +='&region4='+a.region4;}
		if (a.published) { _query +='&published='+a.published;}
		 _query +='&mediaTypeId='+a.mediaTypeId;
		 _query +='&mediaType='+a.mediaType;
		
		
		callback = function (data) {
			//alert(jQuery.toJSON(data)); key();
			//var newObj = jQuery.parseJSON(data);
			if (data.success == true) {
				parent.window.location.reload(true);
				//jQuery('#'+o.mediaType).click();
			} else {
				alert('Save did not work: '+ data.error);
				key();
			}
			
		}
		//alert('sending '+_query);
		jQuery.post('/index.php?option=com_bayliner&ajax=1&task=save', 
					_query +'&contentEn='+jQuery.toJSON(a.contentEn)
							+'&contentFr='+jQuery.toJSON(a.contentFr)
							+'&contentEs='+jQuery.toJSON(a.contentEs), 
					callback, 
					"json");
	}
	
function get(a) {
		var _vars = a;
		var _mode = a.mode; delete a.mode;
		var _url = o.serverName;
		
		//alert('get');
		var query = '';
		for (prop in _vars) { query += prop+'='+_vars[prop] + '&'; }
		query += 'ajax=1&option=com_bayliner&lang=en';
		
		//alert(o.client.region);
		
		switch(o.client.region) {
			case '4' : query +=  '&region4=1'; break;
			case '3': query +=  '&region3=1'; break;
			case '2': query +=  '&region2=1'; break;
			case '1': default: query +=  '&region1=1'; break;
			//case '0':  break;
		};

		switch(_mode) {
			case 'object':
				var callback = function (data) {
					//alert('data back is: '+data);
					var newObj = jQuery.parseJSON(data);
					if (!o.objects) { o.objects = {} };
					o.objects[newObj[0].id] = newObj[0];
					//alert(o.objects[newObj[0].id].id);
					key();
				}
			break;
			case 'object.preload':
				var callback = function (data) {
					//alert('data back is: '+data);
					var newObj = jQuery.parseJSON(data);
					if (!o.objects) { o.objects = {} };
					o.objects[newObj[0].id] = newObj[0];
					//alert(o.objects[newObj[0].id].id);
					//key();
				}
			break;
			case 'login':
				var callback = function (data) {
					//alert('data back is: '+data);
					var newObj = jQuery.parseJSON(data);
					o.login = newObj;
					//alert(newObj);
					key();
				}
			break;
			case 'type': 
				var callback = function (data) {
					//alert('object.list callback : '+data);
					var newObj = jQuery.parseJSON(data);
					//alert(newObj[0].mediaType);
					if (!o.types) { o.types = {} };
					o.types[newObj[0].mediaType] = newObj;
					//alert(o.types[newObj[0].mediaType].length);
					key();
				}
			break;
			case 'dealer': 
				var callback = function (data) {
					//alert('object.list callback : '+data);
					var newObj = jQuery.parseJSON(data);
					//alert(newObj[0].mediaType);
					if (!o.dealers) { o.dealers = {} };
					o.dealers = newObj;
					//alert(o.types[newObj[0].mediaType].length);
					key();
				}
			break;
			case 'country': 
				var callback = function (data) {
					//alert('object.list callback : '+data);
					var newObj = jQuery.parseJSON(data);
					//alert(newObj[0].mediaType);
					if (!o.country) { o.country = {} };
					o.country = newObj;
					//alert(o.types[newObj[0].mediaType].length);
					key();
				}
			break;
			case 'type.preload': 
				var callback = function (data) {
					//alert('object.list callback : '+data);
					var newObj = jQuery.parseJSON(data);
					//alert(newObj[0].mediaType);
					if (!o.types) { o.types = {} };
					o.types[newObj[0].mediaType] = newObj;
					//alert(o.types[newObj[0].mediaType].length);
					//key();
				}
			break;
			case 'hits': 
				var callback = function () {}
			break;
			default:
				var callback = function (data) {
					trace(data);
					var newObj = jQuery.parseJSON(data);
					o.a[0]= {}; o.a[0]= newObj;
					key();
				}
			break;
		}
		//alert('sending '+query);
		
		//should makde a cut off for already fetched data
		jQuery.get('/index.php', query , callback, "text");
		
	}
	
/*function remove(key) {
		var _key = key;
		var _url = o.serverName;
		
		var query = 'ajax=1&option=com_bayliner&task=delete&id='+_key;
		var callback = function (data) {
			var newObj = jQuery.parseJSON(data);
			if (newObj.success == true) {
				window.location = 'http://'+_url+'/index.php?option=com_bayliner';//&mediaType='+o.mediaType;
				//jQuery('#'+o.mediaType).click();
			} else {
				alert('Delete did not work: '+ data);
			};
		}
		//alert('sending '+query);
		jQuery.get('http://'+_url+'/index.php', query , callback, "text");
	}
	*/
	
function require (file) {
	if (o.require) { var _require = o.require}
	else { o.require = []; _require = []; };
	var done = 0;
	for (var i=0, il= _require.length; i<il; i++) {
		if (_require[i] == file) { done =1;}
	}
	if (done==0) {
		trace('adding library: '+ file);
		var headID = document.getElementsByTagName("head")[0];         
		var node = document.createElement('script');
		node.type = 'text/javascript';
		node.src =  file;
		headID.appendChild(node);
		o.require.push(file);
	} else {
		trace('skipping library include: '+ file);
		key();
	}
	
}
	
function rewire(value,rewireArray) {
		for (prop in rewireArray) {
			if (!value.indexOf(prop)) {
				value = rewireArray[prop]; 
				//alert(value);
			} 
		}
		return value;
	}		
	
	
function preloadImage(a) {
	_img = a.image;
	_height = 1;
	_width = 1;
	
	if (!o.preloadCount) { o.preloadCount = 1 }
	else { o.preloadCount ++; };
	
	o['pic'+o.preloadCount] = new Image(_height,_width); 
	o['pic'+o.preloadCount].src=_img; 

}


function optional(obj,prop){
	//alert(typeof obj[prop]);
	if (typeof obj[prop] != 'undefined') {
		return obj[prop];
	} else { return ''; }
}


// PLUGINS //	
	/*
initPreloader = function (){
	//alert('here');
	jQuery('body').append('<div id="preloader" style="display:none;"></div>');
	jQuery('#preloader').css({
						position:'absolute',
						height:'5px',
						width:'3px',
						backgroundColor:'#aaa',
						zIndex:'1000',
						top:'0px',
						left:'0px' 
						});
	jQuery('#preloader').hide();

}
function updatePreloader(){
	if (o.f.length>0){
		if (jQuery('#preloader').opacity==0) {jQuery('#preloader').fadeIn(o.speed);}
		// update preloader width
		jQuery('#preloader').animate({ width: (jQuery(window).width()/o.f.length) -18 +'px'}, o.popSpeed);
		jQuery('#preloader').css({ bottom:'0'});
	} else {
		jQuery('#preloader').fadeOut(o.speed);
	}
}
	*/
	
	
//alert('loaded core end');


