// JavaScript Document
$(document).ready(function(){ 
	
	/******Feature Gallery ********/
	if($("#FeatureGallary").is('*')){
		FeatureGallery();
	}
	/******End Feature Gallery ********/
	
	InputFormDisplay(".searchBar", "Search");
	InputFormDisplay("#userName", "username");
	InputFormDisplay("#password", "password");
	InputFormDisplay("#reviewBox", "Write a review ...");
	InputFormDisplay("#foreBox", "Post on fore wall ...");
	
	$(".delete").click(function() {
		return AjaxLinkCommand("/AjaxRequests/profile_delete.php", this, "Delete this message?", ".shoutMSG")
	});
	$(".report").click(function() {
		return AjaxLinkCommand("/AjaxRequests/profile_report.php", this, "Report this message?", ".shoutMSG")
	});
});

function InputFormDisplay(inputArea, text){
	$(inputArea).focus(function() {
			if($(inputArea).val() == text) {
				$(inputArea).val("");
			}
			$(inputArea).css("color", "#000000");
		});

	$(inputArea).blur(function() {
		if($(inputArea).val() == "") {
			$(inputArea).val(text);
			$(inputArea).css("color", "#666666");
		}
	});
}

//restrict text in textarea + counter
function restrictText(textid, limit, infobox){

    var textarea = $(textid).val();
    var textlength = textarea.length;

    if(textlength > limit){
        $(infobox).html('Max limit: '+limit+' characters!');
        $(textid).val(textarea.substr(0,limit));
        return false;
    }else {
        $(infobox).html('Characters left: '+ (limit - textlength) +'/'+limit+'.');
        return true;
    }
}

function FeatureGallery(){
	var i = 0;
	$("#FeatureGallary").cycle({ 
		fx:      'scrollHorz', 
		speed:    1500, 
		timeout:  5000,
		pager:  '#FeatureGallaryNav', 
		slideExpr: 'img',
		
		pagerAnchorBuilder: function(idx, slide) { 
			return '<a href="#">&nbsp;</a>'; 
		} ,
		before: function(){
			if(i!=0) {$('#FeatureCaption').fadeOut(1000);}
			},
		after: function(){
			if(i!=0) {$('#FeatureCaption').fadeIn(1000);}				
			$('#FeatureCaption').html($(this).attr('alt'));
			i++;
		}
		
	});
	
	$('#FeatureGallary').mouseover(function() { 
    	$(this).cycle('pause'); 
	});
	$('#FeatureGallary').mouseout(function() { 
    	$(this).cycle('resume'); 
	});	
}
/*	contentarea = output response
	formTag = form element use 'this'
	fileLoc = ajax file location
	method = post || get
	appendInsert = insert || append || prepend*/
function AjaxUpdater(contentArea, formTag, fileLoc, method, appendInsert, msg){
	if(!msg)
		msg = false;
		
	queryStr1 = "";
	formArray = new Array("input", "textarea", "select");
	for (y in formArray){//get form elements
		formObj = formTag.getElementsByTagName(formArray[y]);
		if(formObj){//test if exists
			for (x in formObj)
			  {  
				  try{//handle any errors
					  testNameStr = $(formObj[x]).attr("name");//get form name
					  testValueStr = $(formObj[x]).attr("value");//form value
					  if(testNameStr){//no Undefined elements
						  queryStr1 += testNameStr+"="+escape(testValueStr)+"&";
					  }
					$(formObj[x]).blur();   					  
				  }catch(err){
					  
				  }
			  }
		}
	}
		
	queryStr1 = queryStr1.substr(0, queryStr1.length-1);//format string - no & sign at end

	  $.ajax({
			url: fileLoc,//file to load
			type: method,//method
			data: queryStr1,//data string			
			beforeSend: function(){
				//$(formTag).append("<div class=\"loader\"></div>");
				//$(".loader").css("display", "block");			
				}, //show loading just when link is clicked TEST - alert(queryStr1+tabName);
			success: function(result, statusTxt) {			
				//$(".loader").remove();//remove loading whe	n the process is complete	
				var m = result.split(":r:");
				if(appendInsert == "insert")
					$(contentArea).html(m[0]);
				else if(appendInsert == "prepend")
					$(contentArea).prepend(m[0]);
				else
					$(contentArea).append(m[0]);
				
				if(msg)
					responceMessage(formTag, statusTxt, m[1]);
			},
			error: function(httpR, statusTxt){
				if(msg)
					responceMessage(formTag, statusTxt, m[1]);
			}
		});//close Ajax
	  
	  return false;
}
function AjaxLinkCommand(fileLoc, element, message, parentClass){
	if(confirm(message)){
		$.ajax({
				url: fileLoc,//file to load
				type: "GET",//method
				data: $(element).attr("rel"),//data string			
				beforeSend: function(){
					$(element).closest(parentClass).append("<div class=\"loader\"></div>");
					$(".loader").css("display", "block");			
					}, //show loading just when link is clicked TEST - alert(queryStr1+tabName);
				success: function(result, statusTxt) {			
					//$(".loader").remove();//remove loading whe	n the process is complete	
					var m = result.split(":r:");
					
					if(m[0] == "success")
						$(element).closest(parentClass).slideUp(1000);
						
					$(".loader").remove();
				}
			});//close Ajax
		}
		
		return false;
}
function staticMessageAni(){
	$(".success, .error")
			.show('fast')
			.delay(4000)
			.slideUp(800, function(){
				$(this).remove();
			});
}
function responceMessage(formTag, sTxt, msg){
		$(formTag).prepend(msg);
		$(".success, .error")
			.show('fast')
			.delay(4000)
			.slideUp(800, function(){
				$(this).remove();
			});
}
function formSubmit(id){
	$(id).submit();
	return false;
}

function ajaxFileUpload(){
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});

	$.ajaxFileUpload({
			url:'/AjaxRequests/photo_selector.php',
			secureuri:false,
			fileElementId:'fileUpload',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						$("#selectedFiles").html(data.error);
					}else
					{
						$("#selectedFiles").html(data.msg);
					}
					
				}
			},
			error: function (data, status, e)
			{
				$("#selectedFiles").html(e);
			}
		})	
}
