/**
 // ***************************************************************************************
 //   CLASS|TEMPLATE:
 //   /js/application/application.js
 //
 //   PURPOSE:
 //   This file provides application level variables and functions
 //
 //   teagan [10.26.2009]
 // ***************************************************************************************
 */

// create namespace
Ext.ns('Slush');    
 
/**
 * Creates the Application 
 * @constructor
 */	
Slush.app = function() {
    // do NOT access DOM from here; elements don't exist yet
 
    // private variables
 
    // private functions
 
    // public space - the returned object here will become Slush.app
    return {
		
		// ---------------------------------------------
        // public properties, e.g. strings to translate
		mappings_com: Slush.mappings_com,
		ext_path: Ext.BLANK_IMAGE_URL.split('/resources/images/default/s.gif')[0],
  
  
        // public methods
		// --------------------------------------------------
        init: function() {
            
			// initialize quick tips
			Ext.QuickTips.init();

			// Listen for all store exception events
			Ext.data.DataProxy.addListener('exception', function(proxy, type, action, options, res) {
			    if (type === 'remote') {
			        Ext.Msg.show({
			            title: 'REMOTE EXCEPTION',
			            msg: res.message,
			            icon: Ext.MessageBox.ERROR,
			            buttons: Ext.Msg.OK
			        });
			    }
			});
			
			// Custom validation type for confirmation fields
			Ext.apply(Ext.form.VTypes, {
				 confirm: function(val, field) {
					if (field.originalField) {
						var origVal = Ext.getCmp(field.originalField).getValue();
						return (val == origVal);
					}
					return true;
				 }
				,confirmText: 'Orginal and Confirmation values must match.'
			})
			
			// redirect old browsers. we hate ie. especially ie6
			if (Ext.isIE6) {
				//window.location = 'index.cfm';
			};
			
        } // eo init
        

		// -------------------------------------------------------
		// add Bookmark
		,addBookmark: function(url, title){

			// firefox - mozilla						
			if (Ext.isGecko) {
			    window.sidebar.addPanel(title, url, "");
				
			// opera
			} else if(Ext.isOpera) {
			    var elem = document.createElement('a');
			    elem.setAttribute('href',url);
			    elem.setAttribute('title',title);
			    elem.setAttribute('rel','sidebar');
			    elem.click();
				
			// ie
			} else if(Ext.isIE) {
			    window.external.AddFavorite(url, title);
				
			// Chrome
			} else if (Ext.isChrome){
				Ext.MessageBox.alert('Info','Chrome users: Visit the slush and then press ctrl+D to bookmark (Command+D for macs).');
			}
		}
        

		// -------------------------------------------------------
		// increment the users slush count
		,addSlushCount: function(){			
			Slush.Session.slushCount = parseInt(Slush.Session.slushCount) + 1;
			Ext.fly('user_info_slushes_count').dom.innerHTML = Slush.Session.slushCount;
		}
        

		// -------------------------------------------------------
		// build the html to display a given kibble amount
		// returns a string containing the html markup to represent the kibble value
		,getKibbleHtml: function(kibble){
			var  imgPath 	= 'images/page/'
				,imgAry		= []
				,starAdmin 	= 'admin_star.png'
				,starLvl1 	= 'user_bronzestar.png'
				,starLvl2 	= 'user_silverstar.png'
				,starLvl3 	= 'user_goldstar.png'
				,starLvl4 	= 'user_redstar.png'
				,countLvl1
				,countLvl2
				,countLvl3
				,countLvl4
				,html
				,i
				,kbl 		= kibble;

			// count stars by level
			countLvl4 = Math.floor(kbl / Slush.kibble.level4);
			kbl = kbl % Slush.kibble.level4;
			countLvl3 = Math.floor(kbl / Slush.kibble.level3);
			kbl = kbl % Slush.kibble.level3;
			countLvl2 = Math.floor(kbl / Slush.kibble.level2);
			kbl = kbl % Slush.kibble.level2;
			countLvl1 = Math.floor(kbl / Slush.kibble.level1);
			kbl = kbl % Slush.kibble.level1;
			
			// build an array of images
			for (i=0; i<countLvl4; i++){
				imgAry.push('<img src="' + imgPath + starLvl4 + '">');
			}
			for (i=0; i<countLvl3; i++){
				imgAry.push('<img src="' + imgPath + starLvl3 + '">');
			}
			for (i=0; i<countLvl2; i++){
				imgAry.push('<img src="' + imgPath + starLvl2 + '">');
			}
			for (i=0; i<countLvl1; i++){
				imgAry.push('<img src="' + imgPath + starLvl1 + '">');
			}

			// build html return var - show 5 highest stars as large stars and remaining stars as small dots
			/*
	 			<div class="user_kibble_stars">
					<img src="images/page/user_silverstar.png">
					<img src="images/page/user_silverstar.png">
					<img src="images/page/user_silverstar.png">
					<img src="images/page/user_silverstar.png">
					<img src="images/page/user_bronzestar.png">
					<div class="user_kibble_dots">
						<img src="images/page/user_bronzestar.png">
						<img src="images/page/user_bronzestar.png">
					</div>
				</div>
				<span class="user_kibble_plus">+499</span>
			 */
			html = '<div class="user_kibble_stars">';
			for (i=0; i<imgAry.length && i<5; i++){
				html = html + imgAry[i];
			}
			html = html + '<div class="user_kibble_dots">';
			for (i=5; i<imgAry.length; i++){
				html = html + imgAry[i];
			}
			html = html + '</div>';
			html = html + '</div><span class="user_kibble_plus">+' + kbl + '</span>';
			
			// return html
			return html;
		}
        

		// -------------------------------------------------------
		// increment the users slush count
		,login: function(userid, username, roles, slushCount, newCount){

			// Welcome user
			Ext.onReady(function() {						
				Ext.example.msg(
					 {title:'Sign in', anchor:'t', pause:2}
					,'Welcome back ' + username + '! <br>' + newCount + ' new slushes since your last visit.'
				);
			});
			
			// session globals
			Slush.Session.isUserLoggedin 	= true;
			Slush.Session.userId 			= userid;
			Slush.Session.userName 			= username;
			Slush.Session.roles 			= roles;
			Slush.Session.slushCount 		= slushCount;
								
		}


		// -------------------------------------------------------
		// visit a slush and track the visit count
		,visitSlush: function(slushId, url){		
			window.open(url);
			
			// update views
			Ext.Ajax.request({   
				 waitMsg: 'Adding...'
				,url: Slush.app.mappings_com + 'slush/sites.cfc'
				,params: {
					method: 'addView',
					returnFormat: 'JSON',
					record_id: slushId
				} 
				,success: function(response){   
					Slush.slushSearchStore.reload();				
				}
				,failure: function(response){
				}                     
			}); // eo ajax request
			   
		} // eo visitSlush

		// -------------------------------------------------------
		// visit the user page of the person who created this slush
		,visitUser: function(username){
			window.location = username;
		}

    }; // end return
	
}(); // end of app - the parens() here cause the anonymous function to execute and return

//The above code has already executed, so we can access the init method immediately:
Slush.app.init();
 
// end of file
