/*!
 * jQuery plugin "lineMark"
 * k-a-z@mail.ru
 *
 * Copyright 2010, Kasimov Arthur
 * Licensed by GPL
 *
 * Date: 4:01 04.04.2010
 */


/*
Как это работет?
Вам надо указать элемент от которого будет идти линия, например $('myElement')

Параметры:
	Положение на плоскости:	[hor] или [vert] | по умолчанию [hor]

	Направление разметки: 	[out] или [in] | по умолчанию [out]

	Положение элемента: 	[left] или [right] для [hor] | по умолчанию [left]
                    		[top] или [bottom] для [vert] | по умолчанию [bottom]

	Вид линии: 		[dashed] или [solid] | по умолчанию [dashed]

Примеры ипользования:
		$('myElement').linemark();
		$('myElement').linemark('hor top solid in');
		$('myElement').linemark('in');

Родительский элемент:
		По умолчанию родительский элемент с id="center"
		Для того чтобы сменить родительский элемент нужно указать id родительского элемента
		Пример: $('myElement').linemark('in','parEl'); - теперь линии  бубут считаться на основании элемента с id="parEl"
*/
(function($) {
	$.fn.linemark = function(option,div){
		if(!div)div='center';
		if($(this).size()>0 && $('#'+div).size()>0){
			
			var lineType; //line type (solid/dashed) def: dashed
			var lineSide; // column name (left/right/top/bottom) def: left/bottom
			var linePos; // line end (in/out) def: out
			var lineEnd; // line position (vert/hor) def: hor
			if(!linemark_url)linemark_url='';
			
			if(!div)div='center';
			
			if(option)option=option.split(' ');
			else option=new Array();
			if(jQuery.inArray('solid',option)>=0)lineType='solid';
			else lineType='dashed';
			if(jQuery.inArray('in',option)>=0)lineEnd='in';
			else lineEnd='out';
			if(jQuery.inArray('vert',option)>=0){
				linePos='vert';
				if(jQuery.inArray('top',option)>=0)lineSide='bottom';
				else lineSide='top';
			}else{
				linePos='hor';
				if(jQuery.inArray('right',option)>=0)lineSide='left';
				else lineSide='right';
			}
			
			if(linePos=='hor'){
				th=$(this).outerHeight();//this element height
				tw=$(this).outerWidth();//this element width
				to=$(this).offset(); //this element offset
				bo=$('#'+div).offset(); //body offset
				
				// line width
				if(lineSide=='left') lw=to.left-(bo.left+$('#'+div).outerWidth());
				else lw=bo.left-(to.left+tw); 
				
				lhc=th*0.5; // line height center
				
				lineSidePx=8
				if(lineEnd=='out')lineSidePx+=lw;
				if(lineType=='dashed')lineTypeStyle='background:url('+linemark_url+'line-h.png) repeat-x;';
				else lineTypeStyle='border-top:1px solid #c3c2c2;';
				$(this).append('<img src="'+linemark_url+'line-e.png" style="position:absolute;z-index:5;top:'+(lhc-7)+'px;'+lineSide+':-'+lineSidePx+'px;padding:0px;" />');
				$(this).append('<div style="position:absolute;z-index:5;height:1px;width:'+lw+'px;top:'+lhc+'px;'+lineSide+':-'+lw+'px;'+lineTypeStyle+'"></div>');
			}else{
				th=$(this).outerWidth();//this element height
				tw=$(this).outerHeight();//this element width
				to=$(this).offset(); //this element offset
				bo=$('#'+div).offset(); //body offset	
				
				// line width
				if(lineSide=='top') lw=to.top-(bo.top+$('#'+div).outerHeight());
				else lw=bo.top-(to.top+tw); 

				lhc=th*0.5; // line height center
				lineSidePx=8
				if(lineEnd=='out')lineSidePx+=lw;
				if(lineType=='dashed')lineTypeStyle='background:url('+linemark_url+'line-v.png) repeat left;';
				else lineTypeStyle='border-left:1px solid #c3c2c2;';
				$(this).append('<img src="'+linemark_url+'line-e.png" style="position:absolute;z-index:5;left:'+(lhc-7)+'px;'+lineSide+':-'+lineSidePx+'px;padding:0px;" />');
				$(this).append('<div style="position:absolute;z-index:5;width:2px;height:'+lw+'px;left:'+lhc+'px;'+lineSide+':-'+lw+'px;'+lineTypeStyle+'padding:0px;margin:0px;"></div>');
			}
		}
	}
})(jQuery);

