﻿var homeUrl = "/data/";
hs.graphicsDir = '/_gfx/highslide/';
hs.align = 'center';
hs.outlineType = 'rounded-white';

// <div onclick="return false;" href="/data/et/schema/" id="randomid" class="autobubble">&nbsp;</div>

//var homeUrl = "http://ad3doors.maui.mwx.ee/data/";

var locId_show = null;
var locId_article = null;

$(document).ready(function()
{
	$("#slider").easySlider();

	$(".hinnaTabSelect").bind("click", function() { hinnaTabSelect(this) });

	$(".autobubble").bind("click", showAutoBubble);

	$(".bubble").bind("click", showBubble);

	$(".topmenuitem").click(function()
	{
		$(".topmenuitem").removeClass("topmenuitemactive");
		$(this).addClass("topmenuitemactive");
		document.location.hash = "loc?topmenuitem=" + this.id;
		return false;
	});

	//slide
	var locId = getParameter("topmenuitem");
	if (locId != "")
		$("#" + locId).click();
	//show bubble
	locId_show = getParameter("show");
	if (locId_show != "")
	{
		locId_article = getParameter("article");
		document.location.hash = "";
		$("#" + locId_show).click();
	}
});

var oldPosition;
var oldColor;

function showBubble()
{

	oldPosition = $(this).offset();
	oldColor = $(this).css("backgroundColor");
	var newBubble = $(this).clone(false);
	$(newBubble).css('zIndex', 100);
	$(newBubble).css("left", oldPosition.left);
	$(newBubble).css("top", oldPosition.top);
	$(newBubble).css("width", 285);
	$(newBubble).css("height", 190);
	$(newBubble).css("position", "absolute");
	$(newBubble).attr("id", "zoombubble");
	$(newBubble).removeClass("bubble");
	$(newBubble).addClass(this.id);
	$(newBubble).empty();
	newBubble.prependTo($(this).parent());
	var position = $("#main").offset();
	$(newBubble).animate({
		width: $("#main").innerWidth() + 2, height: $("#main").innerHeight() + 2, left: position.left, top: position.top
	}
   , 800, function()
   {
   	animateCallback(newBubble)
   }
   );
}

function animateCallback(element)
{
	//inner div'id
	$(element).append("<div id='bubblecontent'>&nbsp;</div>");
	$(element).append("<div id='bubblenavigation'> \
    <ul> \
    <li id='navibox1' class='navi translate'>" + firstNaviBoxContent + "</li> \
    <li id='navibox2' class='navi design'>" + secondNaviBoxContent + "</li> \
    <li id='navibox3' class='navi printing'>" + thirdNaviBoxContent + "</li> \
    </ul>\
    </div>");

	$(".navi[class*='" + getElementTypeByClass($("#zoombubble")) + "']").addClass("active");



	//lisame close nupu ja funktsiooni
	$("#bubblenavigation").append("<div id='bubbleclose'>" + closeButtonContent + "</div>");
	$("#bubbleclose").bind("click", function()
	{
		restoreBubble(element)
	}
   );
	$("#bubbleclose").show(500);
	$("#bubblenavigation").animate({
		height: "62px"
	}
   , 200);
	//täidame sisuga
	fillBubbleContent();
}

function restoreBubble(element)
{
	$("#bubblecontent").fadeOut(500);
	$("#bubbleclose").hide(250);
	$("#zoombubble").css("backgroundColor", oldColor);
	$("#bubblenavigation").animate({
		height: 0
	}
   , 100, function()
   {
   	minimizeBubble(element)
   }
   );
}

function minimizeBubble(element, iHeight, iWidth)
{

	if (iHeight == null)
		iHeight = "229px";

	if (iWidth == null)
		iWidth = "304px";

	jQuery.each(jQuery.browser, function(i)
	{
		if ($.browser.msie)
		{
			$(element).animate({
				height: iHeight, width: iWidth, top: oldPosition.top, left: oldPosition.left
			}
   , 800, function()
   {
   	restoreBubbleCallback(element)
   }
   );
		}
		else
		{
			$(element).animate({
				height: iHeight, width: iWidth, backgroundColor: oldColor, top: oldPosition.top, left: oldPosition.left
			}
   , 800, function()
   {
   	restoreBubbleCallback(element)
   }
   );
		}

	});



}

function restoreBubbleCallback(element)
{
	document.location.hash = "";
	$(element).remove();
}

function getElementTypeByClass(element)
{
	if (element.hasClass("translate")) return "translate";
	if (element.hasClass("design")) return "design";
	if (element.hasClass("printing")) return "printing";
	return "";
}

function removeCommonClasses(element)
{
	element.removeClass("translate");
	element.removeClass("design");
	element.removeClass("printing");
	return element;
}


function zoomedSlide(element)
{
	$(".navi").unbind("click");
	$(".navi").removeClass("active");
	$(element).addClass("active");


	var zoombubble = $("#zoombubble");
	removeCommonClasses(zoombubble);
	zoombubble.css("backgroundColor", "#47a6d2");
	zoombubble.addClass(getElementTypeByClass($(element)));

	$(".ajaxhtml").fadeOut(250, function()
	{
		fillBubbleContent();
	}
   );
}

function fillBubbleContent()
{
	$(".navi").bind("click", function()
	{
		zoomedSlide(this)
	}
   );
	var zoombubble = $("#zoombubble");

	var fileToGet = homeUrl + "/" + langContent + "/services/" + getElementTypeByClass(zoombubble) + "/";

	var html = $.ajax({
		url: fileToGet, async: false
	}
   ).responseText;

	html = html.substring(html.indexOf("<div class=\"ajaxhtml"), html.length - 15);


	$("#bubblecontent").html(html);
	$(".list_link").bind("click", loadLayerContent);
	//article
	if ((locId_article == "") || (locId_article == "undefined") || (locId_article == null))
	{
		locId_article = $(".list_link:first");
	}
	else
	{
		locId_article = $("#" + locId_article);
	}

	$(".layer_content_panel").html(getLayerContent($(locId_article)));
	$(locId_article).addClass("list_link_active");	
	$(".ajaxhtml").fadeIn(500);
	locId_article = null;
}

function getLayerContent(element)
{

	var fileToGet = element.attr("href");

	var html = $.ajax({
		url: fileToGet, async: false
	}
   ).responseText;
	html = html.substring(html.indexOf("<div class=\"layer_content"), html.lastIndexOf("</div>"));

	document.location.hash = "#loc?show=" + getElementTypeByClass($("#zoombubble")) + "&article=" + element.attr("id");
	return html;
}

function loadLayerContent()
{
	$(".list_link").removeClass("list_link_active");
	$(this).addClass("list_link_active");	
	$(".layer_content_panel").html(getLayerContent($(this)));
	return false;
}

function getParameter(name)
{
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return results[1];
}

function showAutoBubble()
{
	oldPosition = $(this).offset();
	oldColor = $(this).css("backgroundColor");
	var newBubble = $(this).clone(false);
	$(newBubble).attr("id", "zoombubble");
	$(newBubble).css('zIndex', 100);
	$(newBubble).css("left", oldPosition.left);
	$(newBubble).css("top", oldPosition.top);
	$(newBubble).css("backgroundColor", "silver");

	$(newBubble).css("width", $(this).width());
	$(newBubble).css("height", $(this).height());
	$(newBubble).css("position", "absolute");
	$(newBubble).empty();
	newBubble.prependTo($(this).parent());
	var position = $("#main").offset();
	$(newBubble).animate({
		width: $("#main").innerWidth() + 2, height: $("#main").innerHeight() + 2, left: position.left, top: "120px"
	}
   , 800, function()
   {
   	animateAutoCallback(newBubble)
   }
   );
}

function animateAutoCallback(element)
{

	$(element).append("<div id='bubblecontent'>&nbsp;</div>");
	$(element).append("<div id='bubblenavigation'></div>");


	//lisame close nupu ja funktsiooni
	$("#bubblenavigation").append("<div id='bubbleclose'>" + closeButtonContent + "</div>");
	$("#bubbleclose").bind("click", function()
	{
		minimizeBubble(element, "30px")
	}
   );
	$("#bubbleclose").show(500);
	$("#bubblenavigation").animate({
		height: "62px"
	}
   , 200);

	loadAutoBubbleContent(element);
}

function loadAutoBubbleContent(element)
{
	var fileToGet = $(element).attr("href");

	var html = $.ajax({
		url: fileToGet, async: false
	}
	).responseText;

	html = html.substring(html.indexOf("<div class=\"ajaxhtml"), html.length - 15);

	$("#bubblecontent").html(html);

	$(".ajaxhtml").fadeIn(500);
}

function hinnaTabSelect(element)
{
	$(".hinnaTabSelect").removeClass("selectedHinnaSelect");
	$(element).addClass("selectedHinnaSelect");
	$(".form_tab").removeClass("visibletab");
	$(".form_tab").addClass("hiddentab");
	var s = "#" + element.id + "Form";
	$(s).removeClass("hiddentab");
	$(s).addClass("visibletab");

}

function tabByButton(element)
{
	$(".hinnaTabSelect").removeClass("selectedHinnaSelect");
	$("#" + $(element).attr("name")).addClass("selectedHinnaSelect");
	$(".form_tab").removeClass("visibletab");
	$(".form_tab").addClass("hiddentab");
	var s = "#" + $(element).attr("name") + "Form";
	
	$(s).removeClass("hiddentab");
	$(s).addClass("visibletab");
}

function jsUpload(upload_field)
{
	var newElem = $(upload_field).clone(false);
	$("#" + $(upload_field).attr("name") + "_orig").text("Uploading...");
	$(newElem).insertAfter(upload_field);
	//$(newElem).attr("disabled", "disabled");
	$("#uploadform").empty();
	$("#upload_iframe").empty();
	$("#uploadform").append(upload_field);
	$("#uploadform").append("<input type='hidden' name='id' value='" + $(upload_field).attr("name") + "'/>");
	
	document.uploadForm.submit();
	return true;
}

function submitFeedback(form)
{
	$(":file").remove();
	form.submit();
}

function fileUploaded(element, fileName)
{
	$('#' + element).text(fileName);
}
