/* NCC main JS
 * Dependencies: jQuery 1.2.1
 * @author : Isotop AB
 */ 


var footerLayer;

function Page() 
{
	this.languageBranch = "en";
	this.OnParsedToEnd = function() {


	    topMenu.init();
	    siteTools.init();
	    this.checkLeftMenu();
	    footerLayer = new ContentLayer($('#footer-layer'));
	    $(document).trigger("parseend");


	}

	this.checkLeftMenu = function() {
	if ($("#left-menu").find("li").length == 0) {
	    $("#left-menu").find("ul").hide();
	}
	}
	
	this.adjustHeadlines = function() {
		
		var rowadjust = new Array();
		var rowcount = 1;
		var correctToHeight = 0;
		
		// find measures in left column
		$('.inner-left img').each(function() {
			
			
			if(this.offsetHeight > 25)
			{
				rowadjust.push(rowcount);
				correctToHeight = this.offsetHeight;
			}
			
			if(this.parentNode.parentNode.className.indexOf('leftpuff') == -1)
				rowcount++;
		});
		
		// find measures in right column
		var rowcount = 1;
		$('.inner-right img').each(function() {
			
			
			if(this.offsetHeight > 25)
			{
				rowadjust.push(rowcount);
				correctToHeight = this.offsetHeight;
			}
			
			if(this.parentNode.parentNode.className.indexOf('leftpuff') == -1)
				rowcount++;
		});
		
		// adjust measures in left column
		var rowcount = 1;
		$('.inner-left img').each(function() {
			
			
			if(rowadjust.toString().indexOf(rowcount) != -1)
			{
				
				if((correctToHeight-this.offsetHeight) > 14)
					this.style.marginBottom = (correctToHeight-this.offsetHeight) + "px";
				//this.style.border = "1px solid red";
			}
				
			if(this.parentNode.parentNode.className.indexOf('leftpuff') == -1)
				rowcount++;
			
		});
		
		// adjust measures in right column
		var rowcount = 1;
		$('.inner-right img').each(function() {
			
			
			if(rowadjust.toString().indexOf(rowcount) != -1)
			{
				if((correctToHeight-this.offsetHeight) > 14)
					this.style.marginBottom = (correctToHeight-this.offsetHeight) + "px";
				
			}
			if(this.parentNode.parentNode.className.indexOf('leftpuff') == -1)
				rowcount++;
		});
	}
	
	
	this.attempt = 0;
	this.AdjustSubsectionPuffs = function(container)
	{
		var index = 0;
		var currentheight = 0;
		var previousObj = null;
		var _parent = this;
		$(container).find('img.puff-headline').each(function(){

			if(this.offsetHeight == 0 && _parent.attempt < 3) 
			{
				// this is a hack for adjusting the puffs in edit mode as window.load event will be triggered before the view is loaded.
				setTimeout("page.AdjustSubsectionPuffs($('#puffs'))",200);
				 _parent.attempt++;
				return;
			}
			
			if(index == 1 && currentheight > this.offsetHeight && this.parentNode.parentNode.className.indexOf('news') == -1)
				this.style.marginBottom = (currentheight - this.offsetHeight) + "px";
			
			//if(previousObj)
			//	alert(previousObj.parentNode.className + " " + previousObj.parentNode.className.indexOf('puffleft'));
			
			
			if(previousObj && previousObj.offsetHeight < this.offsetHeight && (previousObj.parentNode.parentNode.className.indexOf('leftpuff') != -1) && this.parentNode.parentNode.className.indexOf('news') == -1)
			{
				previousObj.style.marginBottom = (this.offsetHeight - previousObj.offsetHeight) + "px";
			}
			
			previousObj = this;
			currentheight = this.offsetHeight;
			
			
			index = (index == 1) ? 0 : 1;
			
		});
	}
}


function InlinePopup() {
    this.isActive = false;
    this.currentInternalContent = null;
    this.selector = '0';
    this.hide = function(selector) {
        this.currentInternalContent.css('visibility', 'hidden');
        $(selector).css('visibility', 'hidden');

        this.isActive = false;
    }

    this.show = function(selector) {
        $(selector).show();
    }

    this.showInternal = function(popselector, internalselector) {
        if (!this.isActive || this.selector != internalselector) {
            this.selector = internalselector;
            this.isActive = true;
            if (this.currentInternalContent != null)
                this.currentInternalContent.css('visibility', 'hidden');

            $(popselector).css('visibility', 'visible');

            var img = $(internalselector).find('img.mainImage');

            if ($(img).hasClass("wide") || internalselector == '#mapContainer' || internalselector.startsWith('#flash') || internalselector.startsWith('#boligshop-flash')) {
                $(popselector).removeClass("narrow-popup");
                $(popselector).addClass("wide-popup");
            }
            else {
                $(popselector).removeClass("wide-popup");
                $(popselector).addClass("narrow-popup");
            }

            $(internalselector).css('visibility', 'visible');
            var e = $(internalselector).find(".mainTxt");
            if ($(e).html() != null) {
                if ($(e).html().trim().length > 0) {
                    $(e).show();
                }
            }
            this.currentInternalContent = $(internalselector);
            this.isActive = true;

        }

    }
}
function goTo(link)
{
	location.href = link.attr('href');
}

var dcOpen = false;
var fnAttached = false;
function displayContact(link, selector)
{

	

	$(link).blur();
	var linkContainer = $(link).parent();
	var form = $(selector);
	form.toggle();
	
	if(!fnAttached)
	{
		form.find('.radio').click(function(){
			this.blur();
			form.find('.field3').focus();
		});
	}
	
	fnAttached = true;
	
	if((!dcOpen))
	{
		linkContainer.addClass('arrow-down');
		form.find('.field1').focus();
		
	}
	else
	{
		linkContainer.removeClass('arrow-down');
	}
	
	
	dcOpen = (!dcOpen);
}

function searchNCC(evt, url){

	if(evt.keyCode == 13)
	{
		location.href = url;
	}
		
	return false;
		
}

function ContentLayer(obj)
{
	this.rootElement = obj;
	this.currentInternalContent = null;
	this.currentTextContent = obj;
	_parent = this;
	this.maxHeight = 400;
	
	
	//this.rootElement.find('.close').click(function(){
	//	_parent.hide(_parent.rootElement.find('.close'));
	//});
	
	this.show = function(internalObj, xPos)
	{	
	
		if(this.currentInternalContent != null && internalObj.attr('id') == this.currentInternalContent.attr('id') && this.rootElement.css('display') == 'block')
		{
			this.hide(internalObj.parent().find('.close'));
		}
		else {

			if(this.currentInternalContent) this.currentInternalContent.hide();
			this.currentInternalContent = internalObj;
			
			this.currentTextContent = this.currentInternalContent.find('.txt');

			this.rootElement.show();
			this.currentInternalContent.show();
			
			var h = this.currentInternalContent.height();
			
			if(h > this.maxHeight)
			{
				this.currentTextContent.css('height',this.maxHeight + 'px');
				this.currentTextContent.css('overflow','auto');
				h = this.maxHeight + 22;
			}
			this.rootElement.css('left',xPos + 'px');
			this.rootElement.css('top','-' + (h + 90) + 'px');
			this.rootElement.show();

		}

	} 
	
	this.hide = function(closeObj)
	{
		
		closeObj.parent().parent().find('.container').hide();
		closeObj.parent().parent().hide();
		
		
		
		//this.currentInternalContent.hide();
		//this.rootElement.hide();
		
	}
	
}

function clearOnFocus(inputObjs)
{
	var arr = new Array();
	$(document).ready(function(){
		inputObjs.each(function(){
			$(this).focus(function(){
				if($.inArray(this, arr) == -1 && $(this).attr('class').indexOf('dontclear') == -1)
				{
					arr.push(this);
					$(this).val('');
				}
			});
		});
		
		
	});
}

function correctIndex(type) {
    if (type == 'add') {
        $(".site-footer").addClass("fixIndexIE");
    }
    else {
        $(".site-footer").removeClass("fixIndexIE");
    }
}

function openGoogleMapPopup(lat, lng) {
    var w = window.open("/Templates/Pages/GoogleMapPopup.aspx?lat=" + lat + "&lng=" + lng , "", "width=450, height=375, left=50, top=50, location=no, menubar=no, status=no, toolbar=no");
    w.focus();
}




var ipop = new InlinePopup();
var page = new Page();