
/** Note: Height is already defined by the javascript */
var mmDynamicInput = Class.create({
    initialize: function(strElementId,strDefaultMessage,strSubmitButton){
    	
        /** Set the required class variables */
        this.strSubmitButton = null;
        this.intScaleDefault = 20;
        this.strDefaultMessage = strDefaultMessage;
        this.objTextbox = $(strElementId);
        
        /** Check to see if form has a dedicated submit button to ignore on the blur */
        if(typeof strSubmitButton == 'string' && typeof $(strSubmitButton) == 'object'){
			this.strSubmitButton = strSubmitButton;
        }
        
        /** Set the relevant styles for the textbox */
        this.objTextbox.setStyle({'overflow-y':'hidden',
                                  'height':this.intScaleDefault+'px'});
        
        if(this.objTextbox.getValue() == ''){
			this.objTextbox.update(this.strDefaultMessage);
        }else{
			this.objScale(0);
        }
        
        /** Set all the required event observers */
        this.objTextbox.observe('focus',this.elementFocus.bind(this));
        this.objTextbox.observe('blur',this.elementReset.bind(this));
        this.objTextbox.observe('input',this.objRescale.bind(this));
        this.objTextbox.observe('propertychange',this.objRescalePC.bind(this));
    },
    elementFocus: function(event){
        if(this.objTextbox.value == this.strDefaultMessage){
            this.objTextbox.update('');
        }
        this.objScale(this.intScaleDefault);
    },
    checkFocus: function(){
        if(!this.objTextbox.focus()){
            this.elementReset();
        }
    },
    elementReset: function(event){
    	
    	/** Get the new target / focus element and check if it is a submit button */
    	var target = event.explicitOriginalTarget||document.activeElement;
		var strNewFocus = target ? target.id||target.tagName||target : '';
		
		if(this.strSubmitButton != strNewFocus){
			if(this.objTextbox.value == ''){
	            this.objTextbox.update(this.strDefaultMessage);
	            this.objTextbox.style.height=this.intScaleDefault+'px';
	        }else{
	            this.objScale(0);
	        }
		}else{
			//this.objTextbox.disable();
		}
    },
    objRescale: function(event){
        this.objScale(this.intScaleDefault);
    },
    objRescalePC: function(event){
        if(event.propertyName.toLowerCase()=='value'){
            this.objScale(this.intScaleDefault);
        }
    },
    objScale: function(intAddHeight){
        if(this.objTextbox.scrollHeight>this.objTextbox.clientHeight){
            this.objTextbox.style.height=(this.objTextbox.scrollHeight+intAddHeight)+"px";
        }else{
            this.objTextbox.style.height="10px";
            this.objTextbox.style.height=(this.objTextbox.scrollHeight+intAddHeight)+"px";
        }
    }
});

var mmAjaxCall = Class.create({
    initialize: function(elementId,appParameters,postData,ajaxSettings){
        
        this.objWorkArea = $(elementId);
        
        this.defaultSettings = $H({
            loading: false,
            loading_message: 'Processing Request ...',
            output_result: false,
            success_function: false,
            log_errors: false,
            display_errors: true
        });
        
        this.defaultParameters = $H({
            app_key: '',
            session_key: '',
            request_key: ''
        });
        
        this.strDefaultUrl = 'http://'+document.location.host+'/ajax/index.php';

        /** Set the App parameters and mix with the defaults */
        this.appParameters = (typeof appParameters == 'undefined') ? this.defaultParameters : this.defaultParameters.merge(appParameters);
        
        this.postData = postData;

        /** Set the settings and mix with the defaults */
        this.ajaxSettings = (typeof ajaxSettings == 'undefined') ? this.defaultSettings : this.defaultSettings.merge(ajaxSettings);
        
        this.strRequestUrl = this.strDefaultUrl+'?'+this.appParameters.toQueryString();
        
        this.ajaxCall();
    },
    ajaxCall: function(){
        
        new Ajax.Request(
            this.strRequestUrl,{
                method: 'post',
                parameters: this.postData != null ? this.postData.toQueryString() : '',
                onCreate: this.ajaxLoading.bind(this),
                onSuccess: this.ajaxSuccess.bind(this),
                onFailure: this.ajaxFailure.bind(this)
        });
    },
    ajaxLoading: function(){
        if(this.ajaxSettings.get('loading')){
            /** loading Box Required */
            this.objWorkArea.update(this.ajaxSettings.get('loading_message'));
        }
    },
    ajaxSuccess: function(transport){
        this.objResponse = transport.responseJSON;
        this.processResult();
    },
    ajaxFailure: function(){
        
    },
    processResult: function(){

        /** Check that status and app_key are ok */
        if(this.objResponse.status && this.objResponse.app_key == this.appParameters.get('app_key')){
            
            /** The response is ok, output the results is required */
            if(this.ajaxSettings.get('output_result')){
                /** Display the result */
                this.objWorkArea.update(this.objResponse.response_data);
            }
            
            if(typeof this.ajaxSettings.get('success_function') == 'function'){
				var functionCaller = this.ajaxSettings.get('success_function');
				functionCaller(this.objResponse.response_data);
            }
            
        }else{
            /** Error response has been found, do relevent task with error */
            if(this.ajaxSettings.get('display_errors')){
                /** Display the error */
                this.objWorkArea.update(this.objResponse.error_message);
            }
            
            if(this.ajaxSettings.get('log_errors')){
                /** Log the error */
            }
        }
    }
});

var mmLikeUpdate = Class.create({
    initialize: function(buttonElementId,sessionKey){
    	
    	this.buttonElementId = buttonElementId;
    	
    	this.sessionKey = (typeof sessionKey == 'undefined') ? '{SESSION_KEY}' : sessionKey;
        
        this.likeDetails = $H({
            related_id: ($(this.buttonElementId).readAttribute('like_related') == '') ? 0 : $(this.buttonElementId).readAttribute('like_related'),
            type_id: ($(this.buttonElementId).readAttribute('like_type') == '') ? 0 : $(this.buttonElementId).readAttribute('like_type'),
            like_id: ($(this.buttonElementId).readAttribute('like_id') == '') ? 0 : $(this.buttonElementId).readAttribute('like_id')
        });
        
        this.processRequest();
	},
	processRequest: function(){
		
		new mmAjaxCall(
            this.buttonElementId,
            $H({
                app_key: '612d7852f8c9976b926463f0516fc570b5ec1aa4',
                session_key: this.sessionKey
            }),
            this.likeDetails,
            $H({
                success_function: this.updateLikeButton.bind(this)
            })
        );
	},
	updateLikeButton: function(newLikeId){
		
		/** Check to see what way the button was set **/
		if(this.likeDetails.get('like_id') != 0){
			
			//Switch back to like button 
			$(this.buttonElementId).update('Like');
			$(this.buttonElementId).writeAttribute('like_id','');
		}else{
			
			//Switch back to not like button
			$(this.buttonElementId).update('Dislike');
			$(this.buttonElementId).writeAttribute('like_id',newLikeId);
		}
	}
});

var mmFriendRequest = Class.create({
    initialize: function(buttonElementId,friendId,connectionId){
    	
    	this.buttonElementId = buttonElementId;
    	
    	this.sessionKey = (typeof sessionKey == 'undefined') ? '{SESSION_KEY}' : '';
        
        this.requestDetails = $H({
            friend_id: friendId,
            connection_id: connectionId
        });
        
        this.processRequest();
	},
	processRequest: function(){
		
		new mmAjaxCall(
            this.buttonElementId,
            $H({
                app_key: 'a46979b8c61020532855f4d6a68ee20e9e3b89c4',
                session_key: this.sessionKey
            }),
            this.requestDetails,
            $H({
                success_function: this.updateRequestButton.bind(this)
            })
        );
	},
	updateRequestButton: function(newFriendId){
        
		//Switch back to like button 
		$(this.buttonElementId).update('Friend requested');
		$(this.buttonElementId).writeAttribute('onclick','');
	}
});

var mmImageGallery = Class.create({
    initialize: function(galleryElementId,textElementId,arrPhotos){
    	
    	this.galleryElementId = galleryElementId;
    	this.textElementId = textElementId;
    	this.arrPhotos = arrPhotos;
    	
    	//Prepair the first image on the screen
    	this.currentImage = 0;
    	
    	setInterval(this.nextImage.bind(this),8000);
	},
	nextImage: function(){
		
		this.currentImage++;
		
		if(typeof this.arrPhotos[this.currentImage] == 'undefined'){
			this.currentImage = 1;
		}
		
		//Set the new image in a fade in / out sinario
		$(this.galleryElementId).fade({duration:0.5,from:1.0,to:0.0});
		setTimeout(this.prepairImage.bind(this),500);
		$(this.galleryElementId).appear({delay:0.6,duration:1,from:0.0,to:1.0});
	},
	prepairImage: function(){
		
		//Set the new background style etc
		$(this.galleryElementId).setStyle({backgroundImage:'url(images/welcome/'+this.arrPhotos[this.currentImage]['photo']+')'});
		
		//Set the text size, text color and position
		$(this.textElementId).setStyle({
			color: this.arrPhotos[this.currentImage]['textColor'],
			fontSize: this.arrPhotos[this.currentImage]['textSize'],
			top: this.arrPhotos[this.currentImage]['textPositionTop'],
			left: this.arrPhotos[this.currentImage]['textPositionLeft'],
			width: this.arrPhotos[this.currentImage]['textWidth'],
		});
		
		//Set the new text
		$(this.textElementId).update(this.arrPhotos[this.currentImage]['text']);
	}
});

