﻿// filter (dom drag)


var nx, ny;
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

        CalcCurrentPrice(nx);
		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
	    currPricePosition = nx;
	    dragLayerUP()
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

function dragStart()
{
  Drag.start;
}

var obj = null;
var minPricePosition = 0;
var maxPricePosition = 175;
var currPricePosition = 175;
var _currPrice = 0;
//var newfilter = "";
var _priceParamName = "price"

var rVal, gVal, bVal, cCol;

function chgBg() {
	if (kObj.id =="imgSliderPrice") document.bgColor=document.bgColor/*"#" + Color[ind]+gVal+bVal;*/
}

function CalcCurrentPrice(_currPricePosition) 
{
    var _startPrice = parseFloat(document.getElementById("tdPriceLowest").innerHTML.replace(",",""));
    var _endPrice = parseFloat(document.getElementById("tdPriceHighest").innerHTML.replace(",",""));
    var _pixPriceInerval = Math.round(((_endPrice - _startPrice)/maxPricePosition)*100)/100;
     _currPrice = Math.round(_startPrice + (_currPricePosition*_pixPriceInerval));

    if (_currPricePosition == minPricePosition) _currPrice = parseFloat(_startPrice);
    if (_currPricePosition == maxPricePosition) _currPrice = parseFloat(_endPrice);
   
    _priceVal =  _currPrice; 
    document.getElementById("tdPriceCurrent").innerHTML = _priceVal;
    _priceVal =_priceParamName+ "|lt|" + _currPrice;
}

var dragMe=false, kObj, xPos, direction


function dragLayerUP() 
{
     // sendAjaXRequest
     _page = 0
     Combine_Values_And_Send_Req();
}



var LoadingWindowDiv;
var LoadingWindow = null;
var autorunSecondsLoadingWindow = 1500;

function PositionLoadingWindowDiv(LoadingPosTop, LoadingPosLeft)
{
    LoadingWindowDiv = document.getElementById("LoadingWindowDiv")
    LoadingWindowDiv.style.height="auto"
   var _left = (screen.width - 250) / 2
   LoadingWindowDiv.style.left = _left + "px"
}

function CloseLoadingWindow() 
{
	PositionLoadingWindowDiv()
	LoadingWindowDiv.style.display = "none";
}	
	
function OpenLoadingWindow(LoadingPosTop, LoadingPosLeft) {
    window.scrollTo(0, 0);
	PositionLoadingWindowDiv(LoadingPosTop, LoadingPosLeft)
	LoadingWindowDiv.style.display = "";
}

function CloseLoadingWindowBetweenPages() 
{
	CloseLoadingWindow() 
}

function hideShowDiv(_div)
{

    if(document.getElementById(_div).style.display =='none')
    {document.getElementById(_div).style.display='';document.getElementById("imgArrow").src=_baseUrl+'resources/images/search/bigUp.gif';}
    else{
    document.getElementById(_div).style.display='none';
    document.getElementById("imgArrow").src=_baseUrl+'resources/images/search/bigDown.gif';
    }
}

function hideShowDivImg(_div,_img)
{
    if(document.getElementById(_div).style.display =='none')
    {document.getElementById(_div).style.display='';
    document.getElementById(_img).src=_baseUrl+'resources/images/search/arrow_down.gif';
    }
    else{
    document.getElementById(_div).style.display='none';
    document.getElementById(_img).src=_baseUrl+'resources/images/search/arrow_left2.gif';
    }
}

       
       var _total=0;
      
       var myArr=new Array(-1,-1,-1,-1);
       
       
       function clearCheckBoxes(_totalRows)
       {
        for(i=1;i<=_totalRows;i++)
            {
                if (document.getElementById("chkCompare" + i)!=null)
                {
                 document.getElementById("chkCompare" + i).checked=false ;
                      _total=0;
//                  for(i=0;i<=myArr.length-1 ;i++)
//                  {myArr[i]=-1;}
                 }
            }
       }
       
       function InsertItemArr(obj)
       {
        
       var _indexFree =-1;
       var _isExist = false;
         for(i=0;i<=myArr.length-1 ;i++)
        {
          if (myArr[i] == obj.value)
           { 
           
              _isExist = true ;
               break;
            }
          if (myArr[i] == -1)
          {
            _indexFree = i;
            break;
            }
        }
        
        if ((!_isExist) && (_indexFree != -1))
          {myArr[_indexFree]=obj.value;_total+=1;}
          else
          obj.checked = false;
       }
       function RemoveItemArr(_item)
       {
         for(i=0;i<=myArr.length-1 ;i++)
        {
          if (myArr[i] == _item)
          {
            myArr[i]=-1;
            _total-=1;
            break;
            }
        }
       }
       
       function CheckCompare(obj)
       {
          
          if (obj != null)
          {
             if (!obj.checked)
             {  
              RemoveItemArr(obj.value);
              //alert(myArr[0]+","+myArr[1]+","+myArr[2]+","+myArr[3])
             }
             else
             {
               if (_total == 4)
             {
               obj.checked = false;
               alert("ניתן לסמן עד 4 תוצאות להשוואה");
               }
               else
                if (obj.checked) 
                  {
                  InsertItemArr(obj);
                  //alert(myArr[0]+","+myArr[1]+","+myArr[2]+","+myArr[3])
                  }
             }
              
             // alert(_total);  
          }
       }
       function GetCompare(_type)
       {
         var _hasValue = "";
           for (i=0;i<4;i++)
           {
            if (myArr[i]!=-1)
            {
                _hasValue = "true";
                break;
            }
           }
           if (_hasValue =="")
           {
            alert('יש לסמן תוצאות על מנת לבצע השוואה');
            
           }
           else
           {
            var url =  "compare.aspx?type="+_type+getParmaeters();
            //alert(url);
            document.location.href=url;
            }
        }
    function getParmaeters()
    {
        var _rst = "";
        for (i=0;i<4;i++)
        {
            if (myArr[i]!=-1)
            {
                _rst = _rst + "&cid" + (parseInt(i,10) + 1)  + "=" + myArr[i];
            }
        }
        try
        {
            _rst = _rst + "&fdate=" + _ddate + "&tdate=" + _rdate;
        }
        catch(x)
        {
        }
        try {
            _rst = _rst + "&seid=" + _ticks;
        }
        catch (x) {
        }
        return _rst;
    }
    
    function markSelectedFromArray()
    {
        for (i=0;i<4;i++)
        {
            if (myArr[i]!=-1)
            {
                if (document.getElementById("chkCompare" + myArr[i])!=null)
                {
                document.getElementById("chkCompare" + myArr[i]).checked="true";
                }
            }
        }
    }

    function hideShowDivImg2(_div, _img) {
        if (document.getElementById(_div).style.display == 'none') {
            document.getElementById(_div).style.display = '';
            document.getElementById(_img).src = _baseUrl + 'resources/images/icons/arrow_up.png';
        }
        else {
            document.getElementById(_div).style.display = 'none';
            document.getElementById(_img).src = _baseUrl + 'resources/images/icons/arrow_down.png';
        }
    }

    function callSort(_obj, _order) {

        var _sortDir = 1;
        var _sortBy = _obj.value;
        if (typeof (_order) != 'undefined')
            _sortDir = _order
        Sort(_sortBy, _sortDir)
    }
