var url = $(location).attr('href').split('#');
var url = url[0];
var moveImage = true;
var last = 1;
var zoomMenu;
var stopZoom = false;
var imgHeight = null;
var imgWidth = null;

function getNewLink(regexp,savelink,link){
    newlink = hash = "";
    savelink = savelink.split("?");
    newlink+=savelink[0]+"?";
    savelink = savelink[1].split('#');
    if(!link){
        if(savelink[1]!=undefined)
            return "#"+savelink[1];
        return '';
    }
    savelink = savelink[0].split('&');
    i = 0;
    $.each(savelink,function(index,value){
        if(value.match(regexp) == null){
            i++;
            if(i>1){
                newlink +="&";
            }
            newlink +=value;
            
        }
    });
    return newlink;
};


function nl2br (str, is_xhtml) {   
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';    
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}


function formToArray(form){
    dataAr = new Object();
    form.find('input:not([type=submit]),textarea,select').each(function(){
	dataAr[$(this).attr('name')] = $(this).val();
    });
    return dataAr;
}

function onlyNumber(ob,f){
    ob.bind('focusout.onlyint',function(e){
	if(f){
	    ob.val(parseFloat(ob.val().replace(/[^0-9,\.]/ig,'').replace(',','.')));
	} else
	    ob.val(parseInt(ob.val()));

	if(ob.val() == 'NaN' || ob.val()<0 || ob.val() == '0'){
	    ob.val('');
	    ob.trigger('focusout.intval');
	}	
    });	   
}

function initialVal(ob,s){
    if(ob.attr('type')!='text')
	return null;
    
    var ncolor = '#AAAAAA';
    var color = ob.css('color');
    if(ob.val() == ""){
	ob.addClass('inactive');
	ob.css('color',ncolor);
	ob.val("");
	ob.val(s);
    }
    ob.bind('focusin.intval',function(){
	if(ob.hasClass('inactive')){
	    ob.css('color',color);
	    ob.val("");
	    ob.removeClass('inactive');
	} 
	ob.bind('keydown.escape',function(e){	    
	    if(e.keyCode == 27){
		ob.trigger('focusout.intval');
	    }
	});
	
    });

    ob.bind('focusout.intval',function(){
	ob.blur();
	if(ob.val() == ""){
	    ob.css('color',ncolor);
	    ob.val(s);
	    ob.addClass('inactive');
	}
	ob.unbind('keydown.escape');
    });
    return true;
}

function parsePx(px){
    px = px.substring(0,px.length-2);
    return parseFloat(px);
}

function imageEditActive(imgContainer,image,editHandler){
    if(moveImage){
        imgContainer.css('cursor','move');
        firstX = firstY = null;
        imageX = image.css('left');
        imageY = image.css('top');
        imageX = imageX.substring(0,imageX.length-2);
        imageY = imageY.substring(0,imageY.length-2);
        imageX = parseFloat(imageX);
        imageY = parseFloat(imageY);
        imgContainer.bind('mousemove.moveImage',function(event){
            if(firstX == null && firstY == null){
                firstX = event.pageX;
                firstY = event.pageY;
            }

            deltaX = event.pageX - firstX;
            deltaY = event.pageY - firstY;
            newX = imageX+deltaX;
            newY = imageY+deltaY;
            image.css('left',newX+'px');
            image.css('top',newY+'px');
            editHandler.trigger('movingImageEvent',[newX,newY]);



        });
    }
    imgContainer.unbind('mousedown.activeImgEdit');
    imgContainer.bind('mousedown.deactiveImgEdit',function(){
        imageEditDeActive($(this),image,editHandler);
    });
}
function imageEditDeActive(imgContainer,image,editHandler){
    imgContainer.css('cursor','pointer');
    imgContainer.unbind('mousedown.deactiveImgEdit');
    imgContainer.unbind('mousemove.moveImage');
    imgContainer.bind('mousedown.activeImgEdit',function(){
        imageEditActive($(this),image,editHandler);
    });
}

function zoom(image,imageContainer,zin){
    if(imgHeight == null && imgWidth == null){
        imgHeight = image.height();
        imgWidth = image.width();
    }
    
    if(zin){
        neww = imgWidth+imgWidth/100;
    } else {
        neww = imgWidth-imgWidth/100;
    }
    newh = imgHeight/imgWidth*neww; 
    
    imgHeight = newh;
    imgWidth = neww;
    image.animate({
        width: neww+'px',
        height: newh+'px'
        },10,function(){
        imageContainer.trigger('zoomNewImageDim',[neww,newh]);
            
        if(!stopZoom)
            zoom(image,imageContainer,zin);
    });

}


function activateZoomMenu(zoomM, imageContainer, image){
    zoomin = zoomM.find('.zoomin');
    zoomout = zoomM.find('.zoomout');
    
    zoomin.bind('mousedown.zoom',function(){
        stopZoom = false;
        zoom(image,imageContainer,true);
        
    });
    $(document).bind('mouseup.zoom',function(){
        stopZoom = true;
    });
   
    
    zoomout.bind('mousedown.zoom',function(){
        stopZoom = false;
        zoom(image,imageContainer,false);
    });
  

}

function activateZoom(imageContainer,image,zoom){
    imageWidth = image.width();
    imageHeight = image.height();
    $(document).unbind('keydown.activeZoom');
           
    zoom.prependTo(imageContainer);
    moveImage = false;
    firstY = null;
    imageContainer.bind('mousemove.zoom',function(event){
        if(firstY == null){
            firstY = event.pageY;
        }
        diffY = firstY-event.pageY;
        if(diffY<0 && last == 1){
            zoom.css("background-image","url(_img/zoomout.png)");
            last = -1;
        } else if(diffY>0 && last == -1){
            zoom.css("background-image","url(_img/zoomin.png)");
            last = 1;
        }
        newHeight = imageHeight+imageHeight*diffY/100;
        newWidth = imageWidth+imageWidth*diffY/100;
        imageContainer.trigger('zoomNewImageDim',[newWidth,newHeight]);
        image.width(newWidth);
        image.height(newHeight);
        
    });
    $(document).bind('keyup.deactiveZoom',function(event){
        var code = (event.keyCode ? event.keyCode : event.which);
        if(code == 90){
           deactiveZoom(imageContainer,image,zoom);
        }
        
    });    
}

function deactiveZoom(imageContainer,image,zoom){
    moveImage = true;
    zoom.remove();
    imageContainer.unbind('mousemove.zoom');
    $(document).unbind('keyup.deactiveZoom');
    $(document).bind('keydown.activeZoom',function(event){
        var code = (event.keyCode ? event.keyCode : event.which);
        if(code == 90){
            activateZoom(imageContainer,image,zoom);
        }
    });    
}

function alertUpload(s,err){
    if(!$('#AlertArea').size())
	$('#Wrapper').prepend('<div id="AlertArea"/>');
    
    $('<div class="dataAlert '+err+'">'+s+'</div>').appendTo('#AlertArea').delay(2000).fadeOut(500,function(){
	$(this).remove();
    });
}

function autoSubmit(form,field,parentelem){
    field.bind('change',function(){
        parentelem.css('background-image','url("_img/load.gif")');
        field.fadeOut(500);
        form.trigger('submit');
    });
}

function cleanMessage(element){
    link = $('<a href="mailto:'+element.attr('title')+'@'+element.text()+'">'+element.attr('title')+'@'+element.text()+'</a>');
    link.insertAfter(element);
    element.remove();
}

$(document).ready(function(){
    $('span.messageUsNow').each(function(){
            cleanMessage($(this));
        }); 
    zoomMenu = $('<div id="ZoomMenu"><div class="zoomin"></div><div class="zoomout"></div></div>');

    $('body form .textfield.int input[type=text]').each(function(){
	onlyNumber($(this),false)
    });
    $('body form .textfield.float input[type=text]').each(function(){
	onlyNumber($(this),true)
    });

});

