/**
 * Get search date range 'yyyy-mm-dd,yyyy-mm-dd':
 * date dropDown or pick
 * @return string 
 */
function getSearchDateRange()
{
	var dateRange;
	
	if( $('#dropDownChoice') && $('#dropDownChoice').attr('checked') ) {
		dateRange=$('#searchDateDropDown').val();
	} else if( $('#pickDateChoice') && $('#pickDateChoice').attr('checked') ) {
		dateRange=$('#firstDateId').val()+','+$('#secondDateId').val();
	} else {
		dateRange='null';
	}
 	return dateRange;
}

/**
 * Update variable through Ajax(json)
 * 
 * @param url string
 * @param id int
 * @param status bool,but can be int
 * @return bool
 */
function updateVariableAjax(url,id,value)
{	
	return updateVariableFieldAjax(url,id,value,null);
}

/**
 * Update variable through Ajax(json) + field name
 * 
 * @param url string
 * @param id int
 * @param status bool,but can be int
 * @param name string|null field name for update
 * @return bool
 */
function updateVariableFieldAjax(url,id,value,name)
{	
	var resultStatus=false;
	var data={variable:value,id:id};
	if( name != null ) {
		data.name=name;
	}
	$.getJSON(url,data,function(json){
	    if( json.status == true ) {
	        resultStatus=true;
	    } else {
	        alert(json.result);
	    }
	})
	return resultStatus;
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

/**
 * Send json data (array) through ajax.
 * 
 * @param dataArray JSON 
 * @param type string (add,remove etc.)
 * @return bool
 */
function setDataAjax(url,dataArray,type)
{
	var resultStatus=false;
	$.ajaxSetup({dataType:"json"});
    $.post(url,{data:$.toJSON(dataArray),todo:type},function(json){
    	if( json.status == true ) {
	        resultStatus=true;
	    } else {
	        alert(json.result);
	    }
    });
    return resultStatus;
}

function adPreview(element,adId)
{
	var iframe;
	$(element).hover(function() { 
		if(!iframe) {
			iframe=document.createElement('iframe');
			$(iframe).attr({src:'/preview/ad?adId='+adId,frameBorder:0,scrolling:"no"})
				     .css({display:"block",position:"absolute",zIndex:"3000",border:0});
			$(iframe).bind('load',function() {
				var objSizes;
				if(iframe.contentWindow.document.getElementById('adBlock')) {
					objSizes=iframe.contentWindow.document.getElementById('adBlock');
				} else {
					objSizes=iframe.contentWindow.document.body;
				}

				var widthI=objSizes.clientWidth;
				var heightI=objSizes.clientHeight;
				//alert('width:'+widthI+' height:'+heightI);
				//alert( iframe.contentWindow.document.getElementById('adBlock').clientHeight  );
				$(iframe).css({width:widthI,
						        height:heightI});
			});
			$(iframe).appendTo(element);	
			
		} else {
			$(iframe).css({display:'block'});
		}
	},function () {
		$(iframe).css({display:'none'});
	});
}

function SelectBoxes(left,right,options) {
	//Init variables
	this.left=left;
	this.right=right;
	this.postUrl='#';
	this.leftRightAction='add';
	this.rightLeftAction='remove';
	this.outComeAttrs=new Object();
	this.fillUrl="#";
    this.titleId=null;
	this.dropDown=null;
	
    var _selfObject=this;
	
	if(options.postUrl) {
		this.postUrl=options.postUrl;
	}
	
	if(options.fillUrl) {
		this.fillUrl=options.fillUrl;
	}
	
	if(options.leftRightAction) {
		this.leftRightAction=options.leftRightAction;
	}
	
	if(options.rightLeftAction) {
		this.rightLeftAction=options.rightLeftAction;
	}
	if(options.outComeAttrs) {
		this.outComeAttrs=options.outComeAttrs;
	}
	if(options.titleId) {
		this.titleId=options.titleId;
	}
	if(options.dropDown) {
		this.dropDown=options.dropDown;
        $(this.dropDown).bind('change',function(){
            var currentValue = $(this).val();
            if (currentValue) {
                _selfObject.fillBoxes({id:currentValue});
            } else {
                _selfObject.fillBoxes();
            }
        });
	}
	this.moveElementLeftRight=function() {
		this._moveElementFromTo(this.left, this.right, this.leftRightAction);
	};
	this.moveElementsLeftRight=function() {
		this._moveElementsFromTo(this.left, this.right, this.leftRightAction);
	};
	this.moveElementRightLeft=function() {
		this._moveElementFromTo(this.right, this.left, this.rightLeftAction);
	};
	
	this.moveElementsRightLeft=function() {
		this._moveElementsFromTo(this.right, this.left, this.rightLeftAction);
	};
	
	this.moveElementsFromTo=function(from,to,action) {
		this._moveElementFromTo(from,to,action);
	};
	
	this.fillBoxes=function(postData) {
		$(this.left).html('');
	    $(this.right).html('');
	    
		if(!postData) {
			return;
		}
		
		$.post(this.fillUrl, postData, function(json) {
	    	 	$(json.leftBox).each(function() {
	    	 		$(_selfObject.left).append( _selfObject._getOptionForAppend(this) );
		        });
		        $(json.rightBox).each(function() {
		            $(_selfObject.right).append( _selfObject._getOptionForAppend(this) ); 
		        });        
		    }, "json");
		
	};
	
	this._changeBlocks=function(dataArray,type) {
	   $.post(this.postUrl,{data:$.toJSON(dataArray),todo:type}, function(json) {
         if(json.status!=true) {
             alert(json.result);
         }
       },'json');
	};
	
	this._moveElementFromTo=function(from,to,action) {
		var data=new Array();
		   $('option:selected',from).each(
		    function(){
		        $(to).append(this);
		        data[data.length]=_selfObject._getOptionOutComeAttrs(this);
		    }
		);
	    this._changeBlocks(data,action);
	};
	
	this._moveElementsFromTo=function(from,to,action) {
		var data=new Array();
	    $('#'+from+' option').each(
	    function(){
	        $('#'+to).append(this);
	        data[data.length]=this._getOptionOutComeAttrs(this);
	    }
	    );
	    this._changeBlocks(data,action);
	};
	
	this._getOptionOutComeAttrs=function(option) {
		var obj=new Object();
		for(var i=0;i<this.outComeAttrs.length;i++) {
			obj[this.outComeAttrs[i]]=$(option).attr(this.outComeAttrs[i]);
		}
		return obj;
	};
	
	this._getOptionForAppend=function(jsonObject) {
		var str="<option ";
		for(var i=0;i<this.outComeAttrs.length;i++) {
			str+=this.outComeAttrs[i]+"="+jsonObject[this.outComeAttrs[i]]+" ";
		}
		str+=" >";
		if(this.titleId) {
			str+=" "+jsonObject[this.titleId]+" - ";
		}
		str+=jsonObject.title+"</option>";
		return str;
	}
    if(options.dropDownPreselectedValue) {
        $(this.dropDown).val(options.dropDownPreselectedValue);
        $(this.dropDown).change();        
    }

}

