$(document).ready(function() {
	$("#sr_language_menu_form").jqTransform();
	$(".jqTransform").jqTransform();

	$('#add_bookmark').attr('href', document.location.href);
	$('#add_bookmark').jFav();

	$('a#print').click(function(e) {
		//window.print();
		//return false;
	});
	//$('a.lightbox').fancybox();

	$('#set_homepage').click(function(e) {
		e.preventDefault();
		this.style.behavior = 'url(#default#homepage)';
		this.setHomePage(document.location.href);
		return false;
	});
	
	//Image Preview (Picture library)
	$("a.imagePreview").mouseenter(function(e) {
		this.t = this.title;
		this.title = "";
		$("body").prepend("<p id='imagePreview'><img src='" + $(this).attr("preview") + "' alt='Image preview' /></p>");
		$("#imagePreview").css("top", (e.pageY + 4) + "px").css("left", (e.pageX + 4) + "px").fadeIn("fast");
	}).mouseleave(function() {
		$("#imagePreview").remove();
	}).mousemove(function(e) {
		$("#imagePreview").css("top", (e.pageY + 4) + "px").css("left", (e.pageX + 4) + "px");
	});
	
	

	$('#btn_social_networks').mousedown(function(e) {
		document.location.href = "http://www.altana.com/web20";
	});
	$('#btn_social_networks').mouseover(function(e) {
		$('#share').hide();
		$('#social_networks a').first().css("margin-left", $(this).position().left - 5);
		$('#social_networks').slideDown();
		$('#btn_social_networks').css('background-image', 'url(/dev/templates/main/images/social_networks.png)');
	});
	$('#social_networks').mouseleave(function(e) {
		$('#social_networks').slideUp();
		$('#btn_social_networks').css('background-image', 'url(/dev/templates/main/images/social_networks2.png)');
	});
	$('#btn_share').mouseover(function(e) {
		$('#social_networks').hide();
		$('#btn_social_networks').css('background-image', 'url(/dev/templates/main/images/social_networks2.png)');
		$('#share a').first().css("margin-left", $(this).position().left - 5);
		$('#share').slideDown();
		//$('#btn_social_networks').css('background-image', 'url(/dev/templates/main/images/social_networks.png)');
	});
	$('#share').mouseleave(function(e) {
		$('#share').slideUp();
		//$('#btn_social_networks').css('background-image', 'url(/dev/templates/main/images/social_networks2.png)');
	});
	$('#footer').mouseleave(function(e) {
		$('#social_networks').slideUp();
		$('#btn_social_networks').css('background-image', 'url(/dev/templates/main/images/social_networks2.png)');
	});
	//	$(document).pngFix();
	$('#totop').click(function(e) {
		e.preventDefault();
		$('html, body').animate({
			scrollTop : 0
		}, 'slow');
	});

	$('.fancybox').fancybox();

	$('input#search_q').focus(function() {
		$("div#searchBox").addClass("search_wide");
		$("#langSelector").hide();
		if(this.value == this.defaultValue) {
			this.value = '';
		}
	}).focusout(function() {
		$("div#searchBox").removeClass("search_wide");
		$("#langSelector").show();
		if(this.value == '') {
			this.value = this.defaultValue;
		}
	});

	$("#menu li").hover(function() {
		$(this).addClass('activeSub');

		var ulToShow = $('ul', this).eq(0);
		var children = $(this).children();

		var elementWidth = $(this).width();
		var ulWidth = ulToShow.width();
		var optimalWidth = parseInt(elementWidth + 50);
		var checkLastItem = $(this).parent().parent().hasClass('lastItem') || false;

		if(ulWidth <= optimalWidth && $(children[0]).hasClass('hoverCorner') && !checkLastItem) {
			ulToShow.css('width', optimalWidth + 'px');
		}

		if(checkLastItem) {
			elementWidth = '-' + ulWidth;
			ulToShow.css('left', elementWidth + 'px');
		}

	}, function() {
		$(this).removeClass('activeSub');
	});
	
});
function submitCountry(addressid) {
	var cid = addressid;

	$.ajax({
		url : '/dev/scripts/extConnector/misc/proxy.php?type=flashcard&cid=' + cid,
		before : $.fancybox.showActivity(),
		success : function(html) {
			$.fancybox(html, {
				'autoDimensions' : false,
				'width' : 740,
				'height' : 'auto',
				'transitionIn' : 'none',
				'transitionOut' : 'none'
			});
		}
	});
};

function bookmark(service) {
	var this_url = document.location.href;
	var this_title = document.title;
	switch (service) {
		case "delicious":
			redirect_url = "http://delicious.com/save?url=" + this_url + "&title=" + this_title + "";
			break;
		case "google":
			redirect_url = "http://www.google.com/bookmarks/mark?op=edit&bkmk=" + this_url + "&title=" + this_title;
			break;
		case "mister-wong":
			redirect_url = "http://www.mister-wong.com/index.php?action=addurl&bm_url=" + this_url + "&bm_description=" + this_title;
			break;
		case "facebook":
			redirect_url = "http://www.facebook.com/sharer.php?u=" + this_url + "&t=" + this_title;
			break;
		case "diigo":
			redirect_url = "http://www.diigo.com/post?url=" + this_url + "&title=" + this_title + "&tag=&comments=";
			break;
		case "yahoo":
			redirect_url = "http://myweb2.search.yahoo.com/myresults/bookmarklet?t=" + this_title + "&d=&tag=&u=" + this_url;
			break;
		case "linkedin":
			redirect_url = "http://www.linkedin.com/shareArticle?mini=true&url=" + this_url + "&title=" + this_title + "&ro=false";
			break;
		case "baidu":
			redirect_url = "https://passport.baidu.com/?login&tpl=fa&skip_ok=1&u=http%3A//cang.baidu.com/do/add%3Fit%3D" + this_title + "%26iu%3D" + this_url + "";
			break;
	}

	document.location.href = redirect_url;
}

function blurLink(theObject) {
	if(msie4) {
		theObject.blur();
	}
}

function decryptCharcode(n, start, end, offset) {
	n = n + offset;
	if(offset > 0 && n > end) {
		n = start + (n - end - 1);
	} else if(offset < 0 && n < start) {
		n = end - (start - n - 1);
	}
	return String.fromCharCode(n);
}

function decryptString(enc, offset) {
	var dec = "";
	var len = enc.length;
	for(var i = 0; i < len; i++) {
		var n = enc.charCodeAt(i);
		if(n >= 0x2B && n <= 0x3A) {
			dec += decryptCharcode(n, 0x2B, 0x3A, offset);
		} else if(n >= 0x40 && n <= 0x5A) {
			dec += decryptCharcode(n, 0x40, 0x5A, offset);
		} else if(n >= 0x61 && n <= 0x7A) {
			dec += decryptCharcode(n, 0x61, 0x7A, offset);
		} else {
			dec += enc.charAt(i);
		}
	}
	return dec;
}

function linkTo_UnCryptMailto(s) {
	location.href = decryptString(s, -1);
}

function openPopup(url, width, height, left, top, winname) {
	var trails = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",toolbar=no,directories=no,status=no,scrollbars=no,resizable=no,menubar=no";
	newWindow = window.open(url, winname, trails);
}
