var MYAPP = MYAPP || {};
var _gaq = _gaq || [];//google analytics
(function ($, MYAPP) {
	var actions = {};
	var queuedAction = null;//queuedAction for execute after edad control ok
    MYAPP.ui  = MYAPP.ui || {};
//    MYAPP.urls = MYAPP.urls || {};
//    MYAPP.urls.ajaxless = '/';

    MYAPP.swfs = {};
    // UTILITIES

	MYAPP.ui.doAction = function (actionData) {
		actions[actionData.action].fn(actionData.data);
	};

	MYAPP.ui.registAction = function (actionConfig) {//actionConfig.(actionName, fn)
		actions[actionConfig.actionName] = {
			fn : actionConfig.fn
		};
	};

	//AJAX CONTENT CONFIGURATION
//	MYAPP.ui.ajaxContent = {
//		home:				{fijo:'cover'		, data:{containerId:'container-cover'}},
//		origen:				{fijo:'origen'		, data:{containerId:'container-origen', clouseButton:true}},
//		rones:				{fijo:'rones'		, data:{containerId:'container-rones', clouseButton:true}},
//		registro:			{fijo:'registro'	, data:{containerId:'container-registro'}},
//		music:				{fijo:'music'		, data:{containerId:'container-music'}},
//		'fiesta-imagen':	{fijo:'fiesta'		, data:{containerId:'container-fiesta'}},
//		'galerias':			{fijo:'galerias'	, data:{containerId:'container-galerias'}}
//	};

	MYAPP.ui.ajaxContent = {
		home:				{fijo:'cover'},
		origen:				{fijo:'origen'	, clouseButton: true},
		rones:				{fijo:'rones'	, clouseButton: true},
		registro:			{fijo:'registro'},
		music:				{fijo:'music'	},
		'fiesta-imagen':	{fijo:'fiesta'	},
		'galerias':			{fijo:'galerias'}
	};

    //queue an action to execute later with runQueuedAction
	MYAPP.ui.setQueuedAction = function(fn){
		queuedAction = fn;
	};

	//run a queued action and clear
	MYAPP.ui.runQueuedAction = function (){
		if (typeof queuedAction === 'function'){
			queuedAction();
			queuedAction = null;
			return true;
		} else {
			return false;
		}
	};

	MYAPP.ui.registAction({actionName:'load-url-interna', fn: (function (subActions){
		
		MYAPP.utils.listExplorer(subActions, function (subAction){
			subAction.$container = null;
		});

		function loadContent(subActionName) {
			var subAction = subActions[subActionName];
			//cache jquery container
			if (!subAction.$container){
				subAction.$container = MYAPP.ui.$ajaxmaincontainer.find('#container-'+subAction.fijo);
			}

			//if content isnt load then load
			if (subAction.$container.children().length === 0){
				//ver si se setean variables js de posicionamiento para recordar posicion
				subAction.$container.load(MYAPP.urls.webrootrelative+'/home/ajax'+subAction.fijo+'content/',function (){
					//alert (RON.urls.ajaxless);
				});
			}

			//hide other flash
			$('.ajaxContainers').removeClass('container-visible').trigger('stop.MYAPP');

			//pause other flashs

			//set visible
			subAction.$container.addClass('container-visible');
			//continue or start flash

			//manage cerrar button:

			//subAction.clouseButton ? $('#menu-cerrar').show() : $('#menu-cerrar').hide();
			_gaq.push(['_trackEvent', 'ajax-view', 'load-url-interna', subActionName]);
		}
		return loadContent;

	})(MYAPP.ui.ajaxContent), config: {}});

	MYAPP.ui.registAction({actionName:'load-gallery-pool', fn: (function (){
		var galleryPools = {fijo:'gallerypool', pools:{}};//galleryPoolsCache
		function loadContent(data) {
			if (typeof galleryPools.pools[data] === 'undefined') {galleryPools.pools[data]= {id:data}};
			var galleryPool = galleryPools.pools[data];
			//cache jquery container
			if (!galleryPool.$container){
				galleryPool.$container = MYAPP.ui.$ajaxmaincontainer.find('#container-pool');
			}

			//clear cachedData
//			if (galleryPool.expirationTime >= Date.getDate()){
//				galleryPool.contentOk = false;
//				galleryPool.content   = null;
//			}
			
			//if content isnt load then load else redraw
			if (!galleryPool.contentOk || (galleryPool.$container.children().length === 0)){
				galleryPool.$container.load(MYAPP.urls.webrootrelative+'/home/ajax'+galleryPools.fijo+'content/'+galleryPool.id,function (){
					galleryPool.$container.addClass('container-visible');
				});
			}

			//hide other flash
			$('.ajaxContainers').removeClass('container-visible').trigger('stop.MYAPP');;

			//pause other flashs

			//set visible
			//galleryPool.$container.addClass('container-visible');
			//continue or start flash
			_gaq.push(['_trackEvent', 'ajax-view', 'load-gallery', 'id-'+data]);
		}
		return loadContent;

	})(MYAPP.ui.ajaxContent), config: {}});
	
	MYAPP.ui.registAction({actionName:'viewgalleryitem', fn: function (sharedString){//shareOnFacebookRelative
		_gaq.push(['_trackEvent', 'view', 'foto-video', sharedString]);			
	;}});
	
	MYAPP.ui.registAction({actionName:'downgalleryitem', fn: function (sharedString){//shareOnFacebookRelative
		_gaq.push(['_trackEvent', 'download', 'foto-video', sharedString]);			
	;}});	
	
	MYAPP.ui.registAction({actionName:'fbShare', fn: function (sharedString){//shareOnFacebookRelative
		_gaq.push(['_trackEvent', 'share', 'facebook', sharedString]);			
	;}});

	MYAPP.ui.registAction({actionName:'twShare', fn: function (sharedString){//shareOnTwitterRelative
		_gaq.push(['_trackEvent', 'share', 'twitter', sharedString]);
	;}});

    //MENU CONFIGURATION
    MYAPP.ui.menuOptions = {
        selected : null,
        options :{
            home:{fijo:'cover',elementId:'menu-cover',doAction : function (){MYAPP.ui.doAction({action: 'load-url-interna', data: 'home'})}},
            origen:{fijo:'origen',elementId:'menu-origen',doAction : function (){MYAPP.ui.doAction({action: 'load-url-interna', data: 'origen'})}},
			rones:{fijo:'rones',elementId:'menu-rones',doAction : function (){MYAPP.ui.doAction({action: 'load-url-interna', data: 'rones'})}},
			registro:{fijo:'registro',elementId:'menu-registro',doAction : function (){MYAPP.ui.doAction({action: 'load-url-interna', data: 'registro'})}},
			music:{fijo:'music',elementId:'menu-music',doAction : function (){MYAPP.ui.doAction({action: 'load-url-interna', data: 'origen'})}}/*,
			//hidden : show content but don't' change menu
			'fiesta-imagenes': {fijo:'fiesta',elementId:this.hiddenElements}*/
        }

    };
    MYAPP.ui.menuOptions.selected = MYAPP.ui.menuOptions.options.home;//no cambiar
    MYAPP.utils.listExplorer(MYAPP.ui.menuOptions.options,function (menuOption){
        menuOption.$element = null;
        //menuOption.$container = null;
    });
    
    MYAPP.ui.$ajaxmaincontainer = null;

	//MENU ANCHOR SETTINGS
	MYAPP.ui.menuAnchorSet = function (){
        MYAPP.utils.listExplorer(MYAPP.ui.menuOptions.options, function (menuOption){
			var $element = $('#menu-'+menuOption.fijo);
			if ($element.length !== 0) {
				//menu clicks
				$element.click({menuOption: menuOption.fijo}, function (e) {
					MYAPP.ui.selectMenuOption(e.data.menuOption);
					e.preventDefault();
					e.stopPropagation();
				});
				//anchor rewrites
				menuOption.$element = $element  ;
				$element.find('a').attr({href:'#' /*+ menuOption.fijo*/});
			}

        });
	}

    //MENU BEHAVIOR
    MYAPP.ui.selectMenuOption = function (menuOptionName) {//call after document ready
//        var key;
		var menuOption = MYAPP.ui.menuOptions.options[menuOptionName];

		//unset other menus
		$('#menu li').removeClass('currentmenu');
		//set menu
		$('#menu-'+menuOption.fijo).addClass('currentmenu');
		menuOption.doAction();
    };

	//flash2js api
	MYAPP.flashAPI = {};
	MYAPP.flashAPI.doAction = MYAPP.ui.doAction;

	//ACTIONS
	MYAPP.flashAPI.registAction = MYAPP.ui.registAction;



    //DOCUMENT READY
    $(document).ready(function () {
		var $menuCerrar = $('#menu-cerrar');

		//ACTIONS DECLARATION
		//MYAPP.flashAPI.registAction({actionName:'load-url-interna',fn: MYAPP.flashAPI});

		MYAPP.flashAPI.registAction({actionName:'load-url-ext',fn:function (url){
//				window.location = url;				
				window.open(url, '_BLANK');
				_gaq.push(['_trackEvent', 'view', 'load-url-ext', url]);
		}});
//		MYAPP.flashAPI.registAction({actionName:'load-gallery-pool',fn:function (gpId){
//				alert('load gallery : '+gpId);
//		}});
        //JQUERY CACHING
        MYAPP.ui.$ajaxmaincontainer = $('#content');

        //MENU PREPROCCESS
		MYAPP.ui.menuAnchorSet();

		//MAIN LOGO ==> HOME ACTION
		$('#ron-logo').click(function () {
			MYAPP.ui.selectMenuOption('home');
		});

//		//CLOSE BUTTON => HOME ACTION
		//anchor rewrites
//		$menuCerrar.find('a').attr({href:'#'});
//		$menuCerrar.click(function (e) {
//			MYAPP.ui.selectMenuOption('home');
//			$menuCerrar.hide();
//			e.preventDefault();
//			e.stopPropagation();
//		});

		//DEFAULT MENU SELECTION
        //MYAPP.ui.selectMenuOption(MYAPP.ui.menuOptions.selected);
		//MYAPP.flashAPI.doAction({'action':'load-gallery-pool','data':'1'})
    });
})($, MYAPP);

//FACEBOOK
MYAPP.namespace('modules.fbmodule');

MYAPP.modules.fbmodule = (function (){
		// dependencies
		// private properties
		// private methods
		// end var
	// optionally one-time inti proceudres

	// public API
	return {
		injectFBLike: function ($container,url){
			var MYAPPFBLike = '<iframe src="http://www.facebook.com/plugins/like.php?href='+encodeURI(url)+'&amp;layout=button_count" scrolling="no" frameborder="0" style="height: 62px; width: 100%" allowTransparency="true"></iframe>';
			$container.html(MYAPPFBLike);
		},
		changeFBLikeUrl: function ($iframe,url){
			var MYAPPFBLikeUrl = 'http://www.facebook.com/plugins/like.php?href='+encodeURI(url)+'&amp;layout=button_count';
			$iframe.attr("src",MYAPPFBLikeUrl)
			return $iframe;
		}
	};
}());
