/*
 * y.js - jquery helper
 * v 26.08.2010 - y_toolong with words;
 * 				  allchildren()
 * 
 * 
 * 29.09.2010 - msg()
 * 
 * 
 * 01.10.2010 - msg fix allowed_chars
 * 
 * 15.11.2010 - y_toolong ie fixed
 * 
 * 02.12.2010 - y_get_controller
 * 
 * 06.12.2010 - get_controller
 * 
 * 
 */
y_x=0;
msgids=0;
function var_dump(data,addwhitespace,safety,level) {
        var rtrn = '';
        var dt,it,spaces = '';
        if(!level) {level = 1;}
        for(var i=0; i<level; i++) {
           spaces += '   ';
        }//end for i<level
        if(typeof(data) != 'object') {
           dt = data;
           if(typeof(data) == 'string') {
              if(addwhitespace == 'html') {
                 dt = dt.replace(/&/g,'&amp;');
                 dt = dt.replace(/>/g,'&gt;');
                 dt = dt.replace(/</g,'&lt;');
              }//end if addwhitespace == html
              dt = dt.replace(/\"/g,'\"');
              dt = '"' + dt + '"';
           }//end if typeof == string
           if(typeof(data) == 'function' && addwhitespace) {
              dt = new String(dt).replace(/\n/g,"\n"+spaces);
              if(addwhitespace == 'html') {
                 dt = dt.replace(/&/g,'&amp;');
                 dt = dt.replace(/>/g,'&gt;');
                 dt = dt.replace(/</g,'&lt;');
              }//end if addwhitespace == html
           }//end if typeof == function
           if(typeof(data) == 'undefined') {
              dt = 'undefined';
           }//end if typeof == undefined
           if(addwhitespace == 'html') {
              if(typeof(dt) != 'string') {
                 dt = new String(dt);
              }//end typeof != string
              dt = dt.replace(/ /g,"&nbsp;").replace(/\n/g,"<br>");
           }//end if addwhitespace == html
           return dt;
        }//end if typeof != object && != array
        for (var x in data) {
           if(safety && (level > safety)) {
              dt = '*RECURSION*';
           } else {
              try {
                 dt = var_dump(data[x],addwhitespace,safety,level+1);
              } catch (e) {continue;}
           }//end if-else level > safety
           it = var_dump(x,addwhitespace,safety,level+1);
           rtrn += it + ':' + dt + ',';
           if(addwhitespace) {
              rtrn += '\n'+spaces;
           }//end if addwhitespace
        }//end for...in
        if(addwhitespace) {
           rtrn = '{\n' + spaces + rtrn.substr(0,rtrn.length-(2+(level*3))) + '\n' + spaces.substr(0,spaces.length-3) + '}';
        } else {
           rtrn = '{' + rtrn.substr(0,rtrn.length-1) + '}';
        }//end if-else addwhitespace
        if(addwhitespace == 'html') {
           rtrn = rtrn.replace(/ /g,"&nbsp;").replace(/\n/g,"<br>");
        }//end if addwhitespace == html
        return rtrn;
     }//end function var_dump











function toolong(str,len) {
	
}

y_ids=0;
function y_id(obj) {
	if (!(obj.attr("id"))) {
		obj.attr("id","y_id_"+y_ids);
		y_ids++;
	}
}

function y_def(what) {
	
	if (what != undefined) 
		if (what != null) 
			if (what != "null")
				if (what != "")
					if (what != "undefined") 
			return true;
	
	return false;
}
function y_html(what) {
	x=false;
	if (!(y_def(what))) 
		return false;
	
	if ($(this).html() != null)
			if ($(this).html() != undefined)
					if ($(this).html() != "")
							x=true;
	return x;
							
}
function y_ha(e,a) {
	//alert( " "+a+"='"+e.attr("a")+"' ");
	return " "+a+"='"+e.attr(a)+"' ";
}
function y_create_element(e,filter_name) {

	if (y_def(e)) {	
	s="<"+filter_name;
	s+=y_ha(e,"id")+y_ha(e,"class")+y_ha(e,"style")+y_ha(e,"alt")+y_ha(e,"src")+y_ha(e,"alt")+y_ha(e,"href")+y_ha(e,"title")+y_ha(e,"width")+y_ha(e,"height");
	s+=">"+e.html()+"</"+filter_name+">";
	return s;
	//return "<"+filter_name+" id='"+element.attr("id")+"' class='"+element.attr('class')+"' style='"+element.attr("style")+"' alt='"+element.attr('alt')+"' src='"+element.attr("src")+"' title='"+element.attr("title")+"'>"+element.html()+"</"+filter_name+">";
	} else {
	return "";	
	}
	
}
var y_allowed_chars ="qwertyuiopasdfghjklzxcvbnm1234567890_";
function y_cut_special_chars(txt,replace,allowed_chars) {//replace with number
	r="";
	if (replace == undefined) {
		replace = true;
	}
	if (allowed_chars == undefined) {
		allowed_chars = y_allowed_chars;
	}
	for(i=0;i<(txt.length);i++) {
		ch="";
		for (j=0;j<(allowed_chars.length);j++) {
			if (txt[i]==allowed_chars[j]) {
				ch=txt[i];
			}
		}
		if (ch != "") {
			r+=ch;
		} else {
			if (replace) {
				r += "_"+i;
			}
		}
		
	}
	return r;
}
function y_get_controller(base_url,href) {
	controller = '';
	if (href != undefined) {
	t=href.split(base_url);
	if (t.length > 1) {
		if (t[1].length > 0) {
			t2 = t[1].split('/');
			if (t2.length > 0) {
				controller = t2[0];
			}
		} 
	}
	}
	return controller;
}
function y_get_this_controller(base_url) {
	return y_get_controller(base_url,document.location.href);
}

//$("body").html($("body").html()+"<div id='y_temp'></div>");
(function($){
	
	
	
	$.fn.extend({
		
		/*
		 * 
		 */
		y_shadows: function(options) {
			
		/*
		 *  tworzy cienie zaleĂĹĄÄšĹnie od poĂĹĄĂÂoĂĹĄÄšĹenia kursora
		 *  
		 *  text-shadow: range range blur color;
		 * 	box-shadow: range range blur color;
		 * 
		 */
			
			var defaults = {
				color:"black",
				blur:5,
				range:20,
				text:true,
				box:false
			};
		
		options =  $.extend(defaults, options);
		

	lol = $(this);
	$(document).mousemove(function(e) {
		mx=e.pageX;
		my=e.pageY;
		maxw = $(document).width();
		maxy = $(document).height();
		lol.each(function() {
			
			p=$(this).position();
			x=p.left;
			y=p.top;
			sx = -Math.round(options.range*((mx-x)/(maxw)));
			sy = -Math.round(options.range*((my-y)/(maxy)));
			if (options.text)
			$(this).css({'text-shadow': ''+sx+'px '+sy+'px '+options.blur+'px '+options.color});
			if (options.box==true) {
			$(this).css({'-webkit-box-shadow': ''+sx+'px '+sy+'px '+options.blur+'px '+options.color});
			$(this).css({'-moz-box-shadow': ''+sx+'px '+sy+'px '+options.blur+'px '+options.color});
			$(this).css({'box-shadow': ''+sx+'px '+sy+'px '+options.blur+'px '+options.color});
			
			}
			
		});
	});

/*
 * 
 */


		return this.each(function() {
			
			});

},
y_all_click_by_anchor: function(options) { 
	
	var defaults = {
			anchor:"a:first"
		};
	
	options =  $.extend(defaults, options);
	
	$(this).each(function() {
		return 	$(this).click(function() {
			
			
			if (y_def($(this).children().children().children(options.anchor).attr("href"))) 
			href=$(this).children().children().children(options.anchor).attr("href");
			if (y_def($(this).children().children(options.anchor).attr("href"))) 
			href=$(this).children().children(options.anchor).attr("href");
			if (y_def($(this).children(options.anchor).attr("href"))) 
			href=$(this).children(options.anchor).attr("href");
			document.location.href = href;
			/*
			
			
			if (y_def($(this).children().children().children().children(options.anchor).html())) {
				alert("lol1");
				$(this).children().children().children().children(options.anchor).click();
			}
			
			if (y_def($(this).children().children().children(options.anchor).html())) {
				alert("lol2");
				$(this).children().children().children(options.anchor).click();
			}
			
			if (y_def($(this).children().children(options.anchor).html())) {
				alert($(this).children().children(options.anchor).html());
				$(this).children().children(options.anchor).click();
			}
			if (y_def($(this).children(options.anchor).html())) {
				alert("lol4");
				$(this).children(options.anchor).click();
			}
			
	
			
			*/
			
				//an.click();
			
			
			
				
		});
	});
},
y_img_to_bg: function(options) { 
	
	var defaults = {
			to:"div",
			style:""
		};
	
	options =  $.extend(defaults, options);
	
	return $(this).each(function() {
		
		h = "";
		
		$(this).children("img").each(function() {
			h += "<"+options.to+" title='"+$(this).attr("title")+"' style='background: url("+$(this).attr("src")+");"+options.style+"'></"+options.to+">";
		});
		$(this).children().children("img").each(function() {
			h += "<"+options.to+" title='"+$(this).attr("title")+"' style='background: url("+$(this).attr("src")+");"+options.style+"'></"+options.to+">";
		});
		$(this).children().children().children("img").each(function() {
			h += "<"+options.to+" title='"+$(this).attr("title")+"' style='background: url("+$(this).attr("src")+");"+options.style+"'></"+options.to+">";
		});
		$(this).children().children().children().children("img").each(function() {
			h += "<"+options.to+" title='"+$(this).attr("title")+"' style='background: url("+$(this).attr("src")+");"+options.style+"'></"+options.to+">";
		});
		
		
		$(this).html(h);
		
		
	});
},

y_filter: function(options) { 
	
	var defaults = {
			filter:"img"
		};
	
	options =  $.extend(defaults, options);
	
	return $(this).each(function() {
		
		h = "";
		if (!($(this).attr("id"))) {
			$(this).attr("id","y_id_"+y_x);
			y_x++;
		}
	//	alert("#"+$(this).attr("id")+" "+options.filter);
		$("#"+$(this).attr("id")+" "+options.filter).each(function() {
			h += y_create_element($(this),options.filter);
		});
		          
		
	/*	$(this).children().children().children().children(options.filter).each(function() {
			h += y_create_element($(this),options.filter);
		});
		$(this).children().children().children(options.filter).each(function() {
			h += y_create_element($(this),options.filter);
		});
		$(this).children().children(options.filter).each(function() {
			h += y_create_element($(this),options.filter);
		});
		$(this).children(options.filter).each(function() {
			h += y_create_element($(this),options.filter);
		});
		
		*/
		
		
		if (options.filter != "") {
			$(this).html(h);
		}
		
		
		
	});//cnt - w jednej grupie ile elementow
},y_group: function(options) {
	var defaults = {
			selector:"div",
			cnt: 3
		};
	
	options =  $.extend(defaults, options);
	
	z=0;
	$(this).each(function() {
		$(document).append("");
		ct=0;
		ctlen = Math.floor(($(this).children().length) / options.cnt);
		//alert(ctlen);
		for(i=0;i<ctlen;i++) {
			
			$("#y_temp").append("<"+options.selector+" id='y_group_"+z+"_"+i+"'></"+options.selector+">");
			//alert("<"+options.selector+" id='y_temp_"+i+"'></"+options.selector+">");
		}
		i=0;
		$(this).children().each(function() {
			
			$("#y_group_"+z+"_"+Math.floor((i/options.cnt))).append($(this));
			i++;
		});
		
		$(this).html($("#y_temp").html());
		$("#y_temp").html("");
		z++;	
		
	});
	
	
},
y_toolong: function(options) {
	
	var defaults = {
			len: 50,
			length: -1,
			words: true,
			ears: '()',
			bigears: new Array('',''),
			middle: '..',
			gettext: false,
			title:true
		};
	
	options =  $.extend(defaults, options);
	
	
	 
	 
	len = options.len;
	if (options.length > 0) {
		len = options.length;
	}
	 
	  $(this).each(function() {
		  
	  if (options.gettext) {
		  str = $(this).html();
	  } else {
		  str = $(this).text();
	  }
	  
	  
	  str2="";
	  if (str.length > len) {
		  
		  
		  if (options.words) {
		  i=0;
		  clen = 0;
		  words = str.split(" ");
		  
		  if (words.length > 1) {
		  for(var w in words) {
			  clen += words[w].length + 1;
			  if (clen < len) {
				  str2 += words[w]+" ";
			  }
			  
		  }
		  } else {
			  
			  for (i=0;i<len;i++) {
			      str2 += str.charAt(i);
			    }
		  }
		  
		  } else { 
			  
	    for (i=0;i<len;i++) {
	      str2 += str.charAt(i);
	     
	    }
	   
		  }
	    
	    
	    
	    //return str2+"(..)";
	    $(this).html(str2+options.bigears[0]+options.ears.charAt(0)+options.middle+options.ears.charAt(1)+options.bigears[1]);
	    if (!($(this).attr("title"))) {
	    	if (options.title) {
	    	$(this).attr("title",str);
	    	}
	    }
	  } else {
	    $(this).html(str);
	  }
	  
	  });
	},
	
y_split: function(options) {
		
		var defaults = {
				element: "span"
			};
		
		options =  $.extend(defaults, options);
		
		$(this).each(function () {
			h = "";
			$(this).children().each(function() {
					h += $(this).html();
			});
			
			$(this).html("<"+options.element+">"+h+"</"+options.element+">");
		});
	},
	
	allchildren: function(selector) {
		sel="*";
		if (selector) {
		sel=selector;	
		}
		y_id($(this));
		return $("#"+$(this).attr("id")+" "+sel);
	},
	get_controller: function(base_url) {
		return y_get_controller(base_url,$(this).attr('href'));
	},
	
	msg: function(options) {
	
		var defaults = {
			title: "title",
			content: "content",
			showEffect: "slide",
			hideEffect: "explode",
			showEffectSpeed: 0,
			hideEffectSpeed: 0,
			autoHide: 0,
			once: false,
			_class: "msg",
			top: 0,
			left: 0,
			callback: function() {}
		};	
		
		options =  $.extend(defaults, options);
		
		
		$(this).each(function () {
			msgids++;
			if (options.once) {
			if ($.cookie("y_msg_"+y_cut_special_chars(options.title)+((options.content).length+(options.title).length*2)) == "1") {
				return false
			}
			
				$.cookie("y_msg_"+y_cut_special_chars(options.title)+((options.content).length+(options.title).length*2),"1", { expires: 365 });
			}
			$(this).append("<div id='msg_"+msgids+"' class='"+options._class+"' style='position: absolute;display: none;z-index: "+(10000+msgids)+"'><div class='"+options._class+"_title'>"+options.title+"</div><div class='"+options._class+"_content'>"+options.content+"</div></div>");
			pos = $(this).position();
			$("#msg_"+msgids).css('left',pos.left+options.left);
			$("#msg_"+msgids).css('top',pos.top+options.top);
			if (options.autoHide > 0) {
				setTimeout(function(){
					$("#msg_"+msgids).click();
				},options.autoHide);
			}
			$("#msg_"+msgids).click(function() {
				$(this).hide(options.hideEffect,null,options.hideEffectSpeed,options.callback);
				
			});
			$("#msg_"+msgids).show(options.showEffect,null,options.showEffectSpeed);
		});
		
			
			
		}
	

	

});

})(jQuery);

/* all from bottom must to top */


y_sortsave_init = function(selector) {
	
	$(selector).each(function() {
		
		sel = $(this).attr("id");
		hsel = "#"+sel;
		
		s = $.cookie("y_sort_"+sel);
		if (s != null) {
			
		q = s.split(',');
		
		htm = "";
		for (var i in q) {
			if (q[i].length > 2) {
			
		    htm = htm + "<div id='"+q[i]+"' class='"+$("#"+q[i]).attr("class")+"' style='"+$("#"+q[i]).attr("style")+"'>"+$("#"+q[i]).html()+"</div>";
			}
		}
		
		
		
		$(hsel).html(htm);
		
		
		
		
		
		}
		
		
		
		
	
		$(this).sortable({
			
			handle: '.y_handle',
			
			change: function() {
			
			asel=$(this).attr("id");
			ahsel = "#"+asel;
			  
			   setTimeout(function() {
				   
				   s = "";
				   $.cookie("y_sort_"+asel, null);   
					
			   $(ahsel).children().each(function() {
				   n = $(this).attr("id");
				  
				   if (n.length > 2) {
			          s += $(this).attr("id")+",";
				   }
			   });
			   
			   $.cookie("y_sort_"+asel, s, { path: '/', expires: 365 });
			   },1000);
			}
		});
	});
	
	
	
}
