/*
	Common functions
*/

function openTerms() {
	window.open('press/index/terms','','width=450,height=600,scrollbars=1');
	return false;
}

function playerReady(obj) {
	player = document.getElementById(obj.id);
	var $link = $("div#Highlight div.article-selector ul li.selected a");
	if ($link.length==1)
	{
		url = $link.attr("href");
		player.sendEvent("LOAD", url);
	}
}

function checkDate(date) {
    entry = date;
    valid = (reHunLong.test(entry) || (reHun.test(entry)));
    if (valid) {
        delimChar = (entry.indexOf("-") !== -1) ? "-" : " ";
        delim1 = entry.indexOf(delimChar);
        delim2 = entry.lastIndexOf(delimChar);
		if (delimChar === "-") {
			yr = parseInt(entry.substring(0, delim1), 10);
			mo = parseInt(entry.substring(delim1 + 1, delim2), 10);				
			day = parseInt(entry.substring(delim2 + 1), 10);			
		} else {
			yr = parseInt(entry.substring(0, delim1 - 1), 10);	
			mo = entry.substring(delim1 + 1, delim2).toLowerCase();
			mo = parseInt(jQuery.inArray(mo, _Lang.Months) + 1, 10);
			day = parseInt(entry.substring(delim2 + 1, entry.length - 1), 10);			
		}
        if (yr < 100) {
			today = new Date();
            currCent = parseInt(today.getFullYear() / 100, 10) * 100;
            threshold = (today.getFullYear() + 15) - currCent;
            if (yr > threshold) {
                yr += currCent - 100;
            } else {
                yr += currCent;
            }
        }
        testDate = new Date(yr, mo - 1, day);
        if (testDate.getDate() === day) {
            if (testDate.getMonth() + 1 === mo) {
                if (testDate.getFullYear() === yr) {
                    return true;
                }
            }
        }
    }
    return false;
}


function getDateFormatted(date) {
	year = date.getFullYear();
	month = date.getMonth() + 1;
	if (month < 10) {
		month = "0" + month;
	}
	day = date.getDate();
	if (day < 10) {
		day = "0" + day;
	}
	return year + '' + month + '' + day;
}

/*
	Glossary words layer
*/

function findGlossaryWords(jsonObj, container, onetime) {
	
	if (onetime === null) {
		onetime = false;
	}
		
	$("p", $(container)).each(function () {
		$(this).html(($(this).html().replace(regex, '$1<span class="annotation">$2</span>$3')));
	});
	
	$("p", $(container)).each(function () {
		
		var $parent = $(this),
			$layer = $("#FloatingLayer"),
			$inner = $(".inner", $layer);
		
		$("span.annotation", $(this)).each(function (i) {
			
			if (onetime) {
				$("span.annotation:contains(" +  $(this).text() + "):not(:first)", $parent).replaceWith($(this).text());	
			}
			
			var timeoutID;
			
			$(this).mouseover(function (event) {
				
				var offset = $(this).offset(),
					offsetleft = offset.left,				
					parentoffset = $parent.offset(),
					markerwidth = 0,
					breakline = false,
					linelower = false,
					rightpos,
					parentright,
					leftpos = offsetleft - 70,	// default position
					markeroffset,
					diff,
					padding,
					rowheight = parseInt($(this).css("line-height"), 10);

				$inner.html("<p>" + glossaryJSON.words[$(this).text().toLowerCase()] + "</p>");

				// Checking line break
				
				if (!ie7) {
					if ($(this).height() > rowheight) {
						breakline = true;
						if (!$("span.marker", $(this)).length) {
							$(this).wrap("<span class=\"marker\"></span>");
						}
						markerwidth = $("span.marker").width();
						markeroffset = $("span.marker").offset();
						leftpos = markeroffset.left - 70;
						offsetleft = markeroffset.left;
						$(this).unwrap();
						if (event.pageY > parseInt(markeroffset.top, 10) + parseInt(rowheight, 10)) {
							linelower = true;
						}
					}
				}
				
				rightpos = leftpos + $layer.width();
				parentright = parentoffset.left + $parent.width();

				// Overflowing the left side

				if (leftpos < parentoffset.left) {
					leftpos = parentoffset.left;
					$layer.css("left", leftpos).find(".spike").css("left", offsetleft - parentoffset.left + ($(this).width() / 2) - 8);
				} else {
					$layer.css("left", leftpos).find(".spike").css("left", 70 + ($(this).width() / 2) - 8);			
				}
						
				// Overflowing the right side
						
				if (rightpos > parentright) {					
					diff = leftpos;
					padding = parentright - (leftpos + 70);	
					leftpos = leftpos - (rightpos - parentright);
					if (breakline) {
						diff = markeroffset.left - leftpos + (padding / 2) - 8;						
						if (linelower) {
							$(".spike", $layer).css("left", (markerwidth - parseInt(padding, 10)) / 2 - 8);	
						} else {
							$(".spike", $layer).css("left", diff);
						}
					} else {
						diff = diff - leftpos + 70 + ($(this).width() / 2) - 8;
						$(".spike", $layer).css("left", diff);
					}
					
					$layer.css("left", linelower ? offset.left : leftpos);
					
				}
				
				// Overflowing the top side
				
				if ($(window).scrollTop() > (offset.top - $layer.height())) {
					if (breakline) {
						if (linelower) {
							$layer.css("top", offset.top + 10 + $(this).height());
						} else {
							$layer.css("top", offset.top + 10 - parseInt(rowheight, 10) + $(this).height());
						}
					} else {
						$layer.css("top", offset.top + 10 + $(this).height());						
					}
					$(".spike", $layer).addClass("top");
				} else {
					if (linelower) {
						$layer.css("top", offset.top - 10 + parseInt(rowheight, 10) - $layer.height());
					} else {
						$layer.css("top", offset.top - 10 - $layer.height());	
					}
				}						
				
				// Timed fade in
				timeoutID = window.setTimeout(function () {
					$("#FloatingLayer").show();
				}, 450);
				
			}).mouseout(function () {
				window.clearTimeout(timeoutID);
				$layer.hide();
				$inner.empty();
				$(".spike", $layer).removeClass("top right");
			});			
		});	
	});			
}

function formatContent() {
	$("div.article-content p > img, div.form-info p > img, " + 
			"div.article-content p > a > img, div.form-info p > a > img").each(function () {
		var $parentp = $(this).parents('p'),
			$this = $(this),
			width = $this.attr("width"),
			align = $this.attr("align"),
			float = $this.attr("style"), // || "float: left",
			caption = $this.attr("alt"),
			imgclass = " media-half";
		
		if (width == 496) {
			imgclass = " media-full";
		} else if (width == 231 || width == 235) {
			imgclass = " media-half";
		} else if (width == 142 || width == 146) {
			imgclass = " media-third";			
		} else {
			imgclass = " media-auto";	
		}
		
		if (imgclass != " media-full") {
			if (align == "right" || (float && float.indexOf("right") != -1)) {
				imgclass += " media-right";
			} else {
				imgclass += " media-left";				
			}
		}
		
		var $div = $('<div class="media' + imgclass + ' noautolabel"></div>');
		$div.append($this.parent('a').length === 1 ? $this.parent('a') : $this);
		$div.find('img').replaceWith($('<img src="' + $this.attr("src") + '" width="' + width + '"/>'));
		if ($this.attr("alt") != "") {
			$div.append($('<div class="caption break"></div>').text($this.attr("alt")));
		}
		$parentp.before($div);
		$this.remove();
	});
}

function contentSelectorToggler() {
	var content_id = $('form#contentselector select#contents').val();
	var url = $('form#contentselector').attr('action');
	$('#contentselector-content').load(url + '?cid=' + content_id, function() {
		formatContent();
	});
}

/*
	Events week animations
*/

// Close event row

$.fn.closeWeek = function () {
	$("dl", $(this)).animate({
		height: 0
	}, 300, function() {
		$(this).parents("ul.active").removeClass("active");
		$(this).removeAttr("style");
	});	
	return $(this);
}

// Open event row

$.fn.openWeek = function() {
	$(this).addClass("active");
	var height = $(this).height() - 106;
	$(this).show().find("dl").css("height", "0px").animate({
		height: height
	}, 300, function() {
		$(this).removeAttr("style");					
	});	
	return $(this);
}

// Format table

$.fn.formatTable = function() {
		
	$(this).each(function(){
		
		var $table = $(this);	
		
		$table.attr("cellpadding", 0).attr("cellspacing", 0).addClass("wjs").wrap('<div class="table-wrap">');
		if ($table.find("thead").length == 0 && !$(this).hasClass('fejlecnelkulitablazat')){
			$table.prepend("<thead></thead>");
			$table.find("thead").append($table.find("tbody tr:first"));		
		}
		
		$table.find("tr td").each(function(i){
			if(!isNaN($(this).text().replace(/ /g, ""))){$(this).addClass("num");}
			if ($(this).attr("colspan") > 1){$(this).addClass("center");}
		});			
		
		$table.each(function(i){
			$(this).find("tbody tr:even").addClass("even");
			$(this).parent().prepend('<span class="thead-bg"></span><span class="tbody-bg"></span>');
			var theadRowNum = 0,
				theadColNum = 0;									
			$(this).find("thead tr").each(function(j){
				theadRowNum++;
				if ($(this).find("td").length > theadColNum){
					theadColNum = $(this).find("td").length;
				}
			});
			$(this).find("thead *").each(function(i){
				if ($(this).is("td")){															   											   			
					if ($(this).attr("rowspan") == $(this).parent().parent().find("tr").length){
						theadRowNum > 1 ? $(this).parent().addClass("last-child rowspan") : $(this).parent().addClass("last-child");
					}
					if ($(this).attr("colspan") == theadColNum){
						$(this).addClass("last-child");
					}
				}
			});			
			$(this).find("tr:last-child").each(function(){
				$(this).addClass("last-child").find("td:last-child").addClass("last-child");
			});				
			if ($(this).find("thead").length > 0) { 
				$(this).parent().find("span.thead-bg").css("height", $(this).find("thead").height());
				$(this).find("thead").addClass("trans");
			} else {
				$(this).parent().css("padding-top", 31).find("span.thead-bg").remove(); 
			}			
		});	
	});
}

// Floating layer

$.fn.floatingLayer = function(width) {

	var width = width || 200;
	$(this).each(
		function(i){

			var $layer = $("#FloatingLayer"),
				$inner = $(".inner", $layer);
				
			$(this).mouseover(function(){

				var offset = $(this).offset(),
					content = $(this).next("dd").html();
					
				if (content != null) {
					$inner.html(content);
					if ($(window).width() < (offset.left + $layer.width())) {
						$layer.css("left", offset.left - $layer.width() + width);						
						$(".spike", $layer).addClass("right");
					} else {
						$layer.css("left", offset.left - 20);		
					}								
					if ($(window).scrollTop() > (offset.top - $layer.height())) {
						$layer.css("top", offset.top + 10 + $(this).height());
						$(".spike", $layer).addClass("top");
					} else {
						$layer.css("top", offset.top - 10 - $layer.height());
					}
					$layer.show();
				}
			}).mouseout(function(){
				$layer.hide();
				$(".inner", $layer).empty();
				$(".spike", $layer).removeClass("top right");
			});
		}
	);			

};



/*
	Gallery handler
*/

$.fn.scrollable = function (mode,event) {
	
		var mode = mode || "init",	// init || auto || next || previous || click
			event = event || false;	
		
		function checkloaded (obj) {
			var img = new Image();
			img.onload = function() {
				var frame = $("div.gallery-content").offset();
				var position = $("div.gallery-content img").offset();
				var linkpos = position.left - frame.left + $("div.gallery-content img").outerWidth() - $("div.gallery-content > a").outerWidth();			
				$("div.gallery-content > a").css("left", linkpos);
			}
			 img.src = obj.src;
		}		
		
		$("div.gallery-content img").each(function(){
			checkloaded(this);
		});
					
		if (mode=="init") {
									
			if (ie || (ff && ffv<3.6)) {
				$("div.gallery-container ul li.selected").each(function () {
					$(this).find("span.t").css({ "background": "none" }).append('<img src="Assets/Common/Images/Fallback/Thumbnail_shadow_top.png" width="' + $(this).width() + '" height="16" /><span class="tick"></span>');
					$(this).find("span.b").css({ "background": "none" }).append('<img src="Assets/Common/Images/Fallback/Thumbnail_shadow_bottom.png" width="' + $(this).width() + '" height="16" />');
				});
			}			
			
			if($(this).length>0) {
				$(this).find("div.gallery-pager a.next").click(function(event){ event.preventDefault(); $("div.gallery-container ul").scrollable("next",event); });
				$(this).find("div.gallery-pager a.previous").click(function(event){ event.preventDefault(); $("div.gallery-container ul").scrollable("previous",event); });
				$(this).find("div.gallery-container ul").scrollable("auto");
				$(this).find("div.gallery-container ul li a").each(function(i){ $(this).click(function(event){ event.preventDefault(); $(this).scrollable("click",i); }); });
				$(this).find("div.gallery-content span.gallery-photo img").each(function(){
					$(this).click(function(event) {
						var pos = $(this).offset(),
							picleft = pos.left,
							picwidth = $(this).width(),
							picright = pos.left + picwidth,							
							mouseX = event.pageX;
						if (mouseX - picleft < picwidth / 2) {
							if (!$("div.gallery-pager a.previous").hasClass("previous-disabled") && $("div.gallery-container ul li.selected").index() > 0) {
								$("div.gallery-container ul").scrollable("previous",event);
							}							
						} else {
							if (!$("div.gallery-pager a.next").hasClass("next-disabled")) {
								$("div.gallery-container ul").scrollable("next",event);
							}
						}
					});
					
					$(this).parent().find("a.next").click(function(event) {
						event.preventDefault();
						if (!$("div.gallery-pager a.next").hasClass("next-disabled")) {
							$("div.gallery-container ul").scrollable("next",event);
						}						
					});
					
					$(this).parent().find("a.next").mouseover(function(event) {
						event.preventDefault();
						event.stopPropagation();
						$(this).show();
					});
					
					$(this).parent().find("a.next").mouseout(function(event) {
						event.preventDefault();
						event.stopPropagation();
						$(this).hide();
					});					
										
					$(this).parent().find("a.previous").click(function(event) {
						event.preventDefault();
						if (!$("div.gallery-pager a.previous").hasClass("previous-disabled") && $("div.gallery-container ul li.selected").index() > 0) {
							$("div.gallery-container ul").scrollable("previous",event);
						}							
					});					
					
					$(this).parent().find("a.previous").mouseover(function(event) {
						event.preventDefault();
						event.stopPropagation();
						$(this).show();
					});
					
					$(this).parent().find("a.previous").mouseout(function(event) {
						event.preventDefault();
						event.stopPropagation();
						$(this).hide();
					});
					
					$(this).mouseover(function(event) {
						var pos = $(this).offset(),
							$parent = $(this).parent(),
							picleft = pos.left,
							picwidth = $(this).width(),
							picright = pos.left + picwidth,							
							mouseX = event.pageX;
						if (mouseX - picleft < picwidth / 2) {
							if ($("div.gallery-container ul li.selected").index() > 0) {
								$parent.find("a.previous").show();
							}
							$parent.find("a.next").hide();							
						} else {
							if ($("div.gallery-container ul li.selected").index() < $("div.gallery-container ul li").length - 1) {
								$parent.find("a.next").show();
							}							
							$parent.find("a.previous").hide();							
						}
					}).mouseout(function(event) {
						var $parent = $(this).parent();
						$parent.find("a.previous").hide();
						$parent.find("a.next").hide();						
					}).mousemove(function(event) {
						var pos = $(this).offset(),
							$parent = $(this).parent(),
							picleft = pos.left,
							picwidth = $(this).width(),
							picright = pos.left + picwidth,							
							mouseX = event.pageX;
						if (mouseX - picleft < picwidth / 2) {
							if ($("div.gallery-container ul li.selected").index() > 0) {
								$parent.find("a.previous").show();
							}
							$parent.find("a.next").hide();							
						} else {
							if ($("div.gallery-container ul li.selected").index() < $("div.gallery-container ul li").length - 1) {
								$parent.find("a.next").show();
							}							
							$parent.find("a.previous").hide();							
						}
					});
					
				});
			}
						
			return;
		}
		
		var $list = $(this),
			$items = $list.find("li"),
			selected = false,
			target = false,
			stepLeft = $list.data.stepLeft,
			stepRight = $list.data.stepRight,
			widthsLeft = 0,
			widthsRight = 0,
			listPos = 0,
			itemsWidth = 0,
			selectedWidth = 0,
			tolerance = 15,
			frameWidth = $list.parent().width(),
			listOffset = parseInt($list.css("margin-left")),
			selectedOffset = parseInt($items.filter(".selected").eq(0).css("margin-left"));
		
		if (mode=="click") {
			var $li = $(this).closest("li"), liw = $li.width(),
				$a = $li.find("a"),
				$span = $a.find("span"),
				size = $a.attr("rel").split("|")[0],
				file = $a.attr("rel").split("|")[1],
				frame,
				position;
				
			$("div.gallery-content > a").attr("href", file);
			$("div.gallery-content > a span").html(size + "<span></span");			
				
			$("div.gallery-pager span#gallery_img_actual_num").html(event+1);
			if (ie || (ff && ffv<3.6)) {
				$("div.gallery-container ul li.selected").each(function () { $(this).find("span img, span.tick").remove(); });
				$li.find("span.t").css({ "background": "none" }).append('<img src="Assets/Common/Images/Fallback/Thumbnail_shadow_top.png" width="' + liw + '" height="16" /><span class="tick"></span>');
				$li.find("span.b").css({ "background": "none" }).append('<img src="Assets/Common/Images/Fallback/Thumbnail_shadow_bottom.png" width="' + liw + '" height="16" />');
			}
				
			$("div.gallery-container ul li.selected").removeClass("selected");
			$("div.gallery-content img").attr({ "src": $(this).attr('href') });
			frame = $("div.gallery-content").offset();
			position = $("div.gallery-content img").offset();			
			var linkpos = position.left - frame.left + $("div.gallery-content img").outerWidth() - $("div.gallery-content > a").outerWidth();
			$("div.gallery-content > a").css("left", linkpos);
			$("div.gallery-caption").html('&nbsp;'+$(this).attr('title'));
			$li.addClass("selected");					
			$(this).blur();
			
			var $list = $("div.gallery-container ul.gallery-list"),
				listLeft = parseInt($list.css("left")),
				listWidth = $list.parent().width(),
				itemWidth = $(this).width(),
				itemPos = $(this).parent().position(),
				lastPos = itemPos.left + itemWidth + listLeft,
				firstPos = itemPos.left;
				
				if (lastPos > listWidth) {
					$("div.gallery-container ul").scrollable("next",event);
				} else if (firstPos + listLeft < 0) {
					$("div.gallery-container ul").scrollable("previous",event);					
				}
								
				if ($("div.gallery-container ul li.selected").index() == 0) {
					$("div.gallery-pager a.previous").addClass("previous-disabled");
				} else {
					$("div.gallery-pager a.previous").removeClass("previous-disabled");				
				}				
								
				if ($("div.gallery-container ul li.selected").index() == $("div.gallery-container ul li").length - 1) {
					$("div.gallery-pager a.next").addClass("next-disabled");			
				} else {
					$("div.gallery-pager a.next").removeClass("next-disabled");							
				}				
				
			return;
		}
		
		if (event && ($(event.currentTarget).hasClass("next-disabled") || $(event.currentTarget).hasClass("previous-disabled"))) { return; }
		
		if (mode=="next") {
			if (isNaN(parseInt(event))) {
				var $li = $("div.gallery-container ul li.selected").next("li"),
					liw = $li.width(),
					$a = $li.find("a"),
					size = $a.attr("rel").split("|")[0],
					file = $a.attr("rel").split("|")[1],					
					i = $li.index();
					
				$("div.gallery-content > a").attr("href", file);
				$("div.gallery-content > a span").html(size + "<span></span");						
				
				$("div.gallery-pager span#gallery_img_actual_num").html(i+1);				
				if (ie || (ff && ffv<3.6)) {
					$("div.gallery-container ul li.selected").each(function () { $(this).find("span img, span.tick").remove(); });
					$li.find("span.t").css({ "background": "none" }).append('<img src="Assets/Common/Images/Fallback/Thumbnail_shadow_top.png" width="' + liw + '" height="16" /><span class="tick"></span>');
					$li.find("span.b").css({ "background": "none" }).append('<img src="Assets/Common/Images/Fallback/Thumbnail_shadow_bottom.png" width="' + liw + '" height="16" />');
				}			
				$("div.gallery-container ul li.selected").removeClass("selected");
				$li.addClass("selected");					
				$("div.gallery-content img").attr({ "src": $a.attr('href') });
				$("div.gallery-caption").html('&nbsp;'+$li.find("a").attr('title'));
				$a.blur();			
		   }
			selected = $list.data.selected;
			target = selected+stepLeft;
			if (target>$items.length) { return;	}
		}
		
		if (mode=="previous") {
			if (isNaN(parseInt(event))) {			
				var $li = $("div.gallery-container ul li.selected").prev("li"),
					liw = $li.width(),				
					$a = $li.find("a"),
					size = $a.attr("rel").split("|")[0],
					file = $a.attr("rel").split("|")[1],					
					i = $li.index();
					
				$("div.gallery-content > a").attr("href", file);
				$("div.gallery-content > a span").html(size + "<span></span");							
					
				$("div.gallery-pager span#gallery_img_actual_num").html(i+1);				
				if (ie || (ff && ffv<3.6)) {
					$("div.gallery-container ul li.selected").each(function () { $(this).find("span img, span.tick").remove(); });
					$li.find("span.t").css({ "background": "none" }).append('<img src="Assets/Common/Images/Fallback/Thumbnail_shadow_top.png" width="' + liw + '" height="16" /><span class="tick"></span>');
					$li.find("span.b").css({ "background": "none" }).append('<img src="Assets/Common/Images/Fallback/Thumbnail_shadow_bottom.png" width="' + liw + '" height="16" />');
				}
				if ($("div.gallery-container ul li.selected").index() > 0) {
					$("div.gallery-container ul li.selected").removeClass("selected");
					$li.addClass("selected");					
					$("div.gallery-content img").attr({ "src": $a.attr('href') });
					$("div.gallery-caption").html('&nbsp;'+$li.find("a").attr('title'));
				}
				$a.blur();				
			}
			selected = $list.data.selected;
			target = selected-stepRight;	
			if (target<0) { return;	 }			
		}
		
		$items.each(function(i){
			itemsWidth = itemsWidth + $(this).outerWidth(true);
			if ((mode=="auto" && $(this).hasClass("selected")) || (mode!="auto" && i==target)) {
				selected = i;
				selectedWidth = $(this).outerWidth();
			}
		});

		if ($list.data.selectedOriginal===undefined) { $list.data.selectedOriginal = selected; }
		$list.data.selected = selected;	
		$items.slice(0,selected).each(function(){ widthsLeft = widthsLeft+$(this).outerWidth(true); });		
		$items.slice(selected+1).each(function(){ widthsRight = widthsRight+$(this).outerWidth(true); });	
		selectedToLeft = 0-(widthsLeft+listOffset+selectedOffset);		
		selectedToCenter = Math.floor((frameWidth-selectedWidth)/2);
		
		if (widthsLeft<selectedToCenter+tolerance) {
			listPos = 0;
			if ($("div.gallery-container ul li.selected").index() == 0) {
				$("div.gallery-pager a.previous").addClass("previous-disabled");
			} else {
				$("div.gallery-pager a.previous").removeClass("previous-disabled");				
			}
			if (mode!="auto") {
				$list.data.stepLeft = 1;
			} else {
				var x = 0, y = false;
				$items.each(function(i){
					if (x + $(this).outerWidth(true) <= selectedToCenter) {
						x = x + $(this).outerWidth(true);
					} else if (y==false) {
						y = i;
					}
				});
				$list.data.stepLeft = (y - selected) + 2;
			}
		} else {
			listPos = selectedToCenter+selectedToLeft;
			$("div.gallery-pager a.previous").removeClass("previous-disabled");
			$list.data.stepLeft = 1;
		}
		
		if (widthsRight<selectedToCenter+tolerance) {
			if (widthsRight<selectedToCenter+tolerance) {
				listPos = 0-$items.last().position().left-$items.last().width()+frameWidth;
			}
			if ($("div.gallery-container ul li.selected").index() == $items.length - 1) {
				$("div.gallery-pager a.next").addClass("next-disabled");			
			} else {
				$("div.gallery-pager a.next").removeClass("next-disabled");							
			}
		} else {			
			$("div.gallery-pager a.next").removeClass("next-disabled");			
		}
		
		if (mode!="auto") {
			$list.data.stepRight = 1;
		} else {
			var x = 0, y = false;
			$items.each(function(i){
				if (x + $(this).outerWidth(true) + listPos <= selectedToCenter) {
					x = x + $(this).outerWidth(true);
				} else if (y==false) {
					y = i;
				}
			});
			$list.data.stepRight = (selected - y) + 1;
		}
		
		if (itemsWidth<frameWidth) {
			listPos = Math.round((frameWidth-itemsWidth)/2)+5;
			//$("div.gallery-pager a.previous").addClass("previous-disabled");
			//$("div.gallery-pager a.next").addClass("next-disabled");
		}
				
		if ($list.data.selectedOriginal==0 && listPos==0) {
			$list.css({ marginLeft: (0-parseInt($items.filter(".selected").eq(0).css("margin-left"))) + "px" });
		} else {
			$list.css({ marginLeft: (0-parseInt($items.filter(":not(.selected)").css("margin-left"))) + "px" });	
		}
		
		if (mode=="auto") {
			$list.css({ left: listPos+'px' });
		} else {
			$list.animate({ left: listPos+'px' },200);
		}
};

function findtopsecret(s) {
	var tmp = -1;
	var n = -1;
	for (var i=0; i<19; i++) {
		tmp = s.indexOf(String(i));
		if (tmp >= 0) {
			n = (n >= 0) ? Math.min(tmp, n) : tmp;
		}
	}
	return s.substr(n + parseInt(s.charAt(n), 10) + 1);
}
	
(function($){

	var keyString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

	var uTF8Encode = function(string) {
		string = string.replace(/\x0d\x0a/g, "\x0a");
		var output = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				output += String.fromCharCode(c);
			} else if ((c > 127) && (c < 2048)) {
				output += String.fromCharCode((c >> 6) | 192);
				output += String.fromCharCode((c & 63) | 128);
			} else {
				output += String.fromCharCode((c >> 12) | 224);
				output += String.fromCharCode(((c >> 6) & 63) | 128);
				output += String.fromCharCode((c & 63) | 128);
			}
		}
		return output;
	};

	var uTF8Decode = function(input) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < input.length ) {
			c = input.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if ((c > 191) && (c < 224)) {
				c2 = input.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = input.charCodeAt(i+1);
				c3 = input.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}

	$.extend({
		base64Encode: function(input) {
			var output = "";
			var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
			var i = 0;
			input = uTF8Encode(input);
			while (i < input.length) {
				chr1 = input.charCodeAt(i++);
				chr2 = input.charCodeAt(i++);
				chr3 = input.charCodeAt(i++);
				enc1 = chr1 >> 2;
				enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
				enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
				enc4 = chr3 & 63;
				if (isNaN(chr2)) {
					enc3 = enc4 = 64;
				} else if (isNaN(chr3)) {
					enc4 = 64;
				}
				output = output + keyString.charAt(enc1) + keyString.charAt(enc2) + keyString.charAt(enc3) + keyString.charAt(enc4);
			}
			return output;
		},
		base64Decode: function(input) {
			var output = "";
			var chr1, chr2, chr3;
			var enc1, enc2, enc3, enc4;
			var i = 0;
			input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
			while (i < input.length) {
				enc1 = keyString.indexOf(input.charAt(i++));
				enc2 = keyString.indexOf(input.charAt(i++));
				enc3 = keyString.indexOf(input.charAt(i++));
				enc4 = keyString.indexOf(input.charAt(i++));
				chr1 = (enc1 << 2) | (enc2 >> 4);
				chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
				chr3 = ((enc3 & 3) << 6) | enc4;
				output = output + String.fromCharCode(chr1);
				if (enc3 != 64) {
					output = output + String.fromCharCode(chr2);
				}
				if (enc4 != 64) {
					output = output + String.fromCharCode(chr3);
				}
			}
			output = uTF8Decode(output);
			return output;
		}
	});
})(jQuery);
	

