var d = document;

/* window_resize - begin */
var winIE = (navigator.userAgent.indexOf("Opera") == -1 && (d.getElementById && d.documentElement.behaviorUrns)) ? true
		: false;

function bodySize() {
	if (winIE && d.documentElement.clientWidth) {
		sObj = d.getElementsByTagName("body")[0].style;
		sObj.width = (d.documentElement.clientWidth < 999) ? "999px" : "100%";
	}
}

function init() {
	if (winIE) {
		bodySize();
	}
}

onload = init;

if (winIE) {
	onresize = bodySize;
}
/* window_resize - end */

/* flash - begin */
function insertFlash(width, height, path, bgcolor, align, id) {
	document
			.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="'
					+ width
					+ '" height="'
					+ height
					+ '" id="'
					+ id
					+ '" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="' + path + '" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="menu" value="false" />');
	document.write('<param name="wmode" value="transparent" />');
	document.write('<param name="bgcolor" value="' + bgcolor + '" />');
	document
			.write('<embed src="'
					+ path
					+ '" quality="high" wmode="transparent" bgcolor="'
					+ bgcolor
					+ '" width="'
					+ width
					+ '" height="'
					+ height
					+ '" name="'
					+ id
					+ '" align="middle" menu="false" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}
/* flash - end */

// JavaScript Document
function Set_Favorites() {

	page_title = document.title;
	page_link = location.href;
	window.external.AddFavorite(page_link, page_title);
}
function Set_Homepage(obj) {
	obj.style.behavior = 'url(#default#homepage)';
	obj.setHomePage(location.href);
}

function MM_jumpMenu(targ, selObj, restore) { // v3.0
	// eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	// if (restore) selObj.selectedIndex=0;
	document.form1.submit();
}

/**
 * functions to manage input fields
 */
function startInput(input, initValue) {
	if (input != null) {
		if (input.value == initValue)
			input.value = "";
	}
}

function finishInput(input, initValue) {
	if (input != null) {
		if ((input.value == initValue) || (input.value == "")) {
			input.value = initValue;
		}
	}
}

/**
 * Open image in popup window
 */
var PositionX = 100;
var PositionY = 100;
var defaultWidth = 500;
var defaultHeight = 500;
var AutoClose = true;

if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
	var isNN = (navigator.appName == "Netscape") ? 1 : 0;
	var isIE = (navigator.appName.indexOf("Microsoft") != -1) ? 1 : 0;
}
var optNN = 'scrollbars=no,width=' + defaultWidth + ',height=' + defaultHeight
		+ ',left=' + PositionX + ',top=' + PositionY;
var optIE = 'scrollbars=no,width=150,height=100,left=' + PositionX + ',top='
		+ PositionY;

function popImg(imageURL, imageTitle) {
	if (isNN) {
		imgWin = window.open('about:blank', '', optNN);
	}
	if (isIE) {
		imgWin = window.open('about:blank', '', optIE);
	}
	with (imgWin.document) {
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');
		writeln('<sc' + 'ript>');
		writeln('var isNN,isIE;');
		writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');
		writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');
		writeln('if (isIE){');
		writeln('window.resizeTo(400,300);');
		writeln('width=400-(document.body.clientWidth-document.images[0].width);');
		writeln('height=300-(document.body.clientHeight-document.images[0].height);');
		writeln('window.resizeTo(width,height);}');
		writeln('if (isNN){');
		writeln('window.innerWidth=document.images["Zoom"].width;');
		writeln('window.innerHeight=document.images["Zoom"].height;}}');
		writeln('function doTitle(){document.title="' + imageTitle + '";}');
		writeln('</sc' + 'ript>');
		if (!AutoClose) {
			writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">');
		} else {
			writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
		}
		writeln('<img name="Zoom" src=' + imageURL + ' style="display:block; margin: 0; padding: 0;"></body></html>');
		close();
	}
}

/* new_window - begin */
var popWins = new Array();
function MM_openBrWindow(theURL, winName, features) { // v2.0
	if (popWins[winName]) {
		if (!popWins[winName].closed) {
			popWins[winName].close();
		}
	}
	popWins[winName] = window.open(theURL, winName, features);
	return popWins[winName];
}
/* new_window - end */

/* go back - begin */
function go_back(url) {
	if (opener) {
		if (opener.closed)
			window.open(url, "");
		else if (opener.location.href.search(url) == -1)
			opener.location.href = url;
		self.close();
		return false;
	}
	return true;
}
/* go back - end */

$(document).ready(
		function() {
			showRich();

			$('#content').find('img').each(function() {
				var img = $(this);
				var border_width = img.attr('border');
				if (border_width != null && border_width > 0) {
					border_width = border_width + 'px';
					img.css('border-style', 'solid');
					img.css('border-width', border_width);
				}
				var align = img.attr('align');
				if (align != null) {
					img.addClass("align_" + align);
				}
			});

			$('#content .caption').addClass('highslide-caption').removeClass(
					'caption');

			$('#content a[onclick]').add('#content a.pict').add(
					'#content a:has(.pict)').addClass('highslide')
					.each(function() {
						var a = $(this);
						var attr = '' + a.attr('onclick');
						// alert(attr);
							a.unbind('click').removeAttr('onclick').click(
									modalImg);
							if (a.attr('href') == '#') {
								attr = attr.replace(/\r?\n/mg, " ");
								var href = attr.replace(
										/.+img_url=(.+?)['"].*/, "$1");
								a.attr('href', href);
							}
						});

			$('#voting form').submit(function() {
				var form = $(this);
				$.post(form.attr('action'), form.serialize(), function(data) {
					$('#voting').empty().html(data);
				});
				return false;
			});

			/*
			 * function modalImg () {
			 * 
			 * $.facebox.settings.opacity = 0.3; $.facebox.settings.loadingImage =
			 * $(this).find('img:first').attr('src'); $.facebox.settings.animate =
			 * true; $.facebox.settings.closeByClick = true; $.facebox({ image:
			 * $(this).attr('href')}); return false; }
			 */

			function modalImg() {
				return hs.expand(this, {
					outlineType : 'rounded-white'
				});
			}

			showBigPics();

			if (window.screen.width < 1280) {
				$('#topmenu .menu').find(' > div.last').remove().end().find(
						'> div:last').addClass('last').end().addClass('short');
			}

			$('a[href^="javascript:to_mylo"]').each(
					function() {
						var a = $(this);
						var href = a.attr('href').replace(
								/javascript:to_mylo\("(.*)"\);/, '$1');
						a.attr('href', my_mylo(href));
					});

		});

function showBigPics() {
	var bigPictures = $('div.bigPictures:first');
	if (bigPictures.size()) {
		bigPictures.find('div.bigPic:empty').remove();

		var pair_count = $('#pair_pics div.bigPic').size();
		if (pair_count == 0) {
			$('#pair_pics').remove();
		}

		if (bigPictures.find('div.bigPic').size() > 1) {
			if (window.screen.width < 1280) {
				$('div.bigPic img').add('div.bigPic embed').add(
						'div.bigPic object').each(function() {
					var t = $(this);
					t.parents('div.bigPic:first').attr('w', t.attr('width'));
				});

				var pair_width = 0;
				var mono_width = 0;
				$('#pair_pics div.bigPic').each(function() {
					pair_width += parseInt($(this).attr('w'));
				});
				$('#pic_mono').each(function() {
					mono_width += parseInt($(this).attr('w'));
				});

				var content_width = $('#content').width();

				if (pair_width > content_width && mono_width > content_width) {
					if (Math.random() > 0.5) {
						mono_width = 0;
						$('#pic_mono').remove();
					} else {
						pair_width = 0;
						$('#pair_pics').remove();
					}
				} else if (pair_width > 0 && mono_width > content_width) {
					mono_width = 0;
					$('#pic_mono').remove();
				}

				if (pair_width > content_width) {
					if (Math.random() > 0.5) {
						$('#pic_left').remove();
					} else {
						$('#pair_right').remove();
					}
				}
			}

			pair_count = $('#pair_pics div.bigPic').size();
			var mono_count = $('#pic_mono').size();

			if (pair_count && mono_count) {
				if (Math.random() > 0.5) {
					$('#pair_pics').remove();
				} else {
					$('#pic_mono').remove();
				}
			}

			if (pair_count == 1) {
				$('#pair_pics').css('float', 'none');
			}
		}

		if (bigPictures.find('div.bigPic').size() > 0) {
			bigPictures.css('display', 'block');

			var pic_ids = [];
			bigPictures.find('div.bigPic div').each(function() {
				pic_ids.push($(this).attr('pic_id'));
			});
			$.get("/index.php", {
				go : "Link",
				pics : pic_ids.join('_')
			});
		}
	}
}
/*
 * function showRich() { var rich = $('#rich');
 * 
 * if (rich.size()) { $.facebox.settings.opacity = 0;
 * $(document).bind('reveal.facebox', function() { window.setTimeout( function()
 * {$(document).trigger('close.facebox');}, 10000 ) }) $.facebox(rich,
 * 'rich_view'); } }
 */
function showRich() {
	var rich = $('#rich');

	rich.insertAfter('#frame');

	if (rich.size()) {
		var params = {
			contentId : 'rich',
			minWidth : 200,
			minHeight : 200
		}
		var i = rich.find('img:first');
		if (!i.size())
			i = rich.find('embed:first');
		if (!i.size())
			i = rich.find('object:first');
		if (i.size()) {
			if (i.attr('width'))
				params.minWidth = i.attr('width');
			if (i.attr('height'))
				params.minHeight = i.attr('height');
		}
		rich.addClass('highslide-html-content').removeClass('rich_hidden');
		hs.htmlExpand($('#rich_open').get(0), params);
		$('#rich_open').remove();
	}
}

function my_mylo(str) {
	var res_str = '';
	eval('res_str="' + str + '";');
	var arr = res_str.split(',');
	var res_str = '';
	var step = arr[arr.length - 1] / 2;
	for ( var i = arr.length - 2; i >= 0; i--) {
		if (arr[i]) {
			res_str += String.fromCharCode(arr[i] - step);
		}
	}
	return res_str;
}

function to_mylo(str) {
	window.location.href = my_mylo(str);
}

function mylo(str) {
	document.write(my_mylo(str));
}
