/*
 * File to use solely on the fadder Epost site for PLAN
 */

var fadderPost={
    
    objs:{},
    bigLimit:150,
    smallLimit:15,    
    spanClass:'message',
    
    fetchObjects:function(){
    	$('label').each(
		    function(){
			obj=$('#'+$(this).attr('for'));
			if(obj.size()!=0){		    
			    obj.bind('keyup', fadderPost.lengthCheck);
				obj.bind('change', fadderPost.lengthCheck);
			}
	    }			
	);
    },
    
    triggerObjects:function(){
    	$('label').each(
		    function(){
			obj=$('#'+$(this).attr('for'));
			if(obj.attr('type')=='file'){
				return false;
			}
			if(obj.size()!=0){		  
  
			    obj.trigger('change');
			}
	    }			
	);
    },
    
    lengthCheck:function(){
    	try{
			obj=$(this);
			if(obj.attr('type')=='file'){
				fadderPost.checkIfImage();
				fadderPost.triggerObjects();
				return false;
			}
			if(typeof(obj.attr('length'))=='undefined'){
			    return true;
			}
			fieldType=String(this.type).toLowerCase();
			fieldValue='';	
			switch(fieldType){
			    case 'textarea':		
				fieldValue=obj.val();
				limit=fadderPost.bigLimit;
			    break;
			    case 'text':
				fieldValue=this.value;
				limit=fadderPost.smallLimit;
			    break;
			    default:
				return false;
			    break;
			}
			//hardcode full length	
			maxnum=obj.attr('length').split('|')[0];
			if(fadderPost.usingImages){
				maxnum=obj.attr('length').split('|')[1];
			}
			//console.log((fieldValue.length - 20)+':'+maxnum);	
			if((fieldValue.length + limit) > maxnum){
			    if(obj.parent().find('span.'+fadderPost.spanClass+'').size()==0){
					dims=obj.offset();
					//+obj.width()+15
					//obj.parent().append('<span class="'+fadderPost.spanClass+'" style="display:none; position: absolute; width: 18em;top:'+(dims.top)+'px;left: '+(dims.left+obj.width()+15)+'px">Nu har du bara&nbsp;<i></i>&nbsp;tecken kvar</span>').find('span.'+fadderPost.spanClass+'').slideDown();		
					obj.parent().append('<span class="'+fadderPost.spanClass+'" style="display:none;">Nu har du bara&nbsp;<i></i>&nbsp;tecken kvar</span>').find('span.'+fadderPost.spanClass+'').slideDown();		
			    }
			    leftChars=String(maxnum-fieldValue.length);
			    leftChars=leftChars>0?leftChars:'0';
			    spanObject=obj.parent().find('span.'+fadderPost.spanClass);
			    obj.parent().find('span.'+fadderPost.spanClass+' i').text(leftChars);
			    if(fieldValue.length>maxnum){
					obj.val(String(obj.val()).substr(0,maxnum));
			    }
			}
			else{
			    //obj.parent().find('span.'+fadderPost.spanClass+'').slideUp(null,function(){$(this).remove();});
  			    obj.parent().find('span.'+fadderPost.spanClass+'').slideUp(500, function(){if($(this).size()>0){$(this).remove();}});
			}	
			return true;	
		}
		catch(e){
		}
    },
    
    checkIfImage:function(){
    	fadderPost.usingImages=false;
    	$('input[@type=file]').each(function(){if($(this).val()!=''){fadderPost.usingImages=true;}});
    	$('div.imageupload img').each(function(){if($(this).attr('src').indexOf('bild_dummy')==-1){fadderPost.usingImages=true;}});
    	//alert(fadderPost.usingImages);
    },
    
    init:function(){
    	$('p.responsetext').each(
    		function(){
    			if($(this).text()==''){
    				//$(this).parent().removeClass('responsetext').find('span').remove();
    				$(this).remove();
    			}
    		}
    	
    	);
    	fadderPost.checkIfImage();
		fadderPost.fetchObjects();
    }
}

function init(){
    fadderPost.init();
}

$(document).ready(init);
