	/**
	* Funkcja obsługująca selecta
	* Autor: Wojciech Rybka
	*
	* Informacja:
	*    Funkcja zaznacza i pobiera wartości value i text z selecta,
	*    Ważne! parametr [value i text] podajemy jako tablicę.
	*
	* select:
	*    Jako drugi parametr podajemy tablicę [value lub text] zależy według czego chcemy zaznaczać
	*
	*    Przykłady:
	*	  $("#select").handleSelect({option:"select", value:["3","2","1"]});
	*	  $("#select").handleSelect({option:"select", value:[user_id.toString()]});
	*     $("#select").handleSelect({option:"select", text:["ddddd","aaa"]});
	*
	* removeSelection:
	* 	Czyści atrybut selected z opcji i wybiera pierwszą z góry opcję.
	*
	* getSelValue:
	* getSelText:
	*    Zwraca tablicę value lub textów z zaznaczonych opcji
	*
	*    Przykłady:
	*     $("#select").handleSelect({option:"getSelValue"}); -> return array
	*     $("#select").handleSelect({option:"getSelText"}); -> return array
	*
	*/
	$.fn.extend({
		handleSelect : function(params) {
			var option = params.option;
			var sel_value = params.value;
			var sel_text = params.text;
			var arrayValue = new Array();
			var $select = $(this);

			switch (option) {
				// Select option //
				case "select" :
					$select.children("option").each(function(i) {
						var cur_val = $select.children("option").eq(i).attr('value');
						var cur_text = $select.children("option").eq(i).text();

						if (sel_value != undefined && $.inArray(cur_val, sel_value) == 0) {
							$select.children("option").eq(i).attr("selected", "selected");
						}

						if (sel_text != undefined && $.inArray(cur_text, sel_text) == 0) {
							$select.children("option").eq(i).attr("selected", "selected");
						}
					});
					break;

				// Remove selection //
				case "removeSelection" :
					$select.children("option").each(function(i) {
						$(this).removeAttr("selected");
					});
					break;

				// Get selected value //
				case "getSelValue" :
					$select.children("option").each(function(i) {
						var sel_option = $select.children("option").eq(i).attr('selected');
						if(sel_option == true) {
							arrayValue.push($select.children("option").eq(i).val());
						}
					});
					return arrayValue;
					break;

				// Get selected text //
				case "getSelText" :
					$select.children("option").each(function(i) {
						var sel_option = $select.children("option").eq(i).attr('selected');
						if(sel_option == true) {
							arrayValue.push($select.children("option").eq(i).text());
						}
					});
					return arrayValue;
					break;
			}
		}
	});

	/**
	* Funkcja obsługująca checkboxa
	* Autor: Wojciech Rybka
	*
	* info:
	*    Dla poprawnego działania funkcji checkboxy powinny mieć nadany id.
	*
	* getState:
	*    Zwraca tablicę checkboxów gdzie klucze to id chechBoxa i wartość to true lub false
	*    funkcja przyjmuje jako selektor: idki lub klasy można podawać więcej po przecinku.
	*
	*    Przykłady:
	*    $("#check2").handleCheckBox({option: "getState"}); -> return array[id] = state;
	*    $("#check1, #check2").handleCheckBox({option: "getState"});*
	*
	* setChecked:
	* removeChecked:
	*    Ustawia i kasuje podanym checkboxom atrybut checked.
	*
	*    Przykłady:
	*    $("#check1, #check3").handleCheckBox({option: "setChecked"});
	*    $(".checkB").handleCheckBox({option : "removeChecked"});
  	*/
	$.fn.extend({
 		handleCheckBox : function(params) {
 			var option = params.option;
			var stateArray = new Array();

 			switch (option) {
 				// Get value //
 				case "getState" :
 					$($(this)).each(function() {
						var id = $(this).attr('id');
						var state = $(this).attr('checked');
						stateArray[id] = state;
 					});
 					return stateArray;
 					break;

				// Set checked //
 	 			case "setChecked" :
 	 				$($(this)).each(function() {
						$(this).attr({"checked" : "checked"});
 					});
 	 				break;

 	 			// Set unchecked //
 	 			case "removeChecked" :
 	 				$($(this)).each(function() {
						$(this).removeAttr("checked");
 					});
 	 				break;
 			}
 		}
 	});


	/**
	* Funkcja obsługująca input typu radio
	* Autor: Wojciech Rybka
	*
	* getSelected_id:
	* getSelected_name:
	*    Zwraca tablicę radio gdzie klucze to id lub name zależy od wybranej opcji zaznaczonego Radio i wartość to value z Radio
	*    funkcja przyjmuje jako selektor: idki lub klasy można podawać więcej po przecinku.
	*
	*   Przykłady:
	*   var radio = $(".radioB").handleRadio({option : "getSelected_id"});
	*	var radio = $(".radioB").handleRadio({option : "getSelected_name"});
	*
	* setChecked:
	* removeChecked:
	*    Ustawia lub kasuje zaznaczenie podanym w selektorze funkcji radio
	*    Jeżeli podano klasę jako selektor to zaznaczy pierwszy element
	*
	*    Przykłady:
	*    $("#rad1, #check3").handleCheckBox({option: "setChecked"});
	*    $(".checkB").handleCheckBox({option : "setUnchecked"});
	*/

	$.fn.extend({
		handleRadio : function(params) {
			var option = params.option;
			var radioArray = new Array();

			switch (option) {
				// Get value //
				case "getSelected_id" :
				case "getSelected_name" :

					$($(this)+':checked').each(function(){
						var name_id = (option == "getSelected_name") ? $(this).attr('name') : $(this).attr('id');;
						radioArray[name_id] = $(this).val();
					});
					return radioArray;
					break;

				case "setChecked" :
					$($(this)).each(function(i) {
						$(this).eq(i).attr({"checked" : "checked"});
					});
					break;

				case "removeChecked" :
					$($(this)).each(function(i) {
						$(this).eq(i).removeAttr("checked");
					});
					break;
			}
		}
	});


// === return selected option in select ===
// example: var menuTypeText = $("#menu_type").selectedOption();
//			console.log (menuTypeText[0].text)
//			OR
//			selectedGallery[0]['value']
//
jQuery.fn.selectedOption = function() {

	obj = $(this);

	function checkIsSelect()
	{
		//TODO: sprawdzić czy obj jest selectem
	}

	function checkOptionLength()
	{
		//TODO: sprawdzić czy length jest > 0
	}

	function getSelectedOption(obj)
	{
		//var options_array = new Array({"value": "", "text": ""});
		var options_array = new Array();

		$(obj).children('option').each(function()
		{
			if ($(this).attr('selected') === true)
			{
				options_array.push({value: $(this).attr('value'), text: $(this).text()});
			};
		});
		return options_array;
	}

	return getSelectedOption(obj);
};

//=== check dom element end return length OR execute param function  ===

jQuery.fn.checkDomElement = function(func) {

	var oSelector = $(this);
	var oSel = {};

	if (oSelector.length > 0 ) {
		oSel = $(this);
	} else {
		return false;
	}

	if (arguments.length == 0) {
		var func = function(oSel) {
			return $(oSel).length;
		};
	};

	return func(oSel);
};

// ==== get id from string 'xx_id'

function getIdFromString (mystring)
{
	if (mystring){
		var myarray = mystring.split(/_/);
		return myarray[1];
	}else{
		return "";
	}
};

// ==== query_string ==== //

function queryStReplacer(key, newVal) {
	var query = window.location.search.substring(1);
	var queryArray = query.split("&");
	var q = "";
	var keyExist = false;

	for (i=0; i<queryArray.length; i++) {
		splitQuery = queryArray[i].split("=");

		if (splitQuery[0] == key) {
			keyExist = true;
			splitQuery[1] = newVal;
		}

		if (i>0) q += "&";
		q += splitQuery[0]+"="+splitQuery[1];
	}
	if (q != "") {amp = "&";} else {amp = "";}
	if (keyExist == false) { q += amp+key+"="+newVal; }

	return q;
}

/* ========== jump to anchor ================ */

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent) {
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
			break;
			obj = obj.offsetParent;
		}
	} else if(obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function moveToAnchor(anchor) {
	if (document.layers){
    	scrollTo(0, document.anchors[anchor].y);
    } else if(document.all) {
    	eval('document.all.' + anchor+ '.scrollIntoView()');
	} else {
    	var obj=document.getElementById(anchor);
        var posY=findPosY(obj);
        self.scrollTo(0,posY);
	}
}

/* ========== ESCAPE HTML ==========
 * Example:
 * $.escape(value)
 */
(function() {
	jQuery.escape = function jQuery$escape(s) {
		if (s == null) return '';
		return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
	};
})();


