/****
 * JS for approximation
 * Author: Mario Diaz @ mario-diaz.com
 * Created: 2011-04-21
 ****/



var bMsie = (document.all) ? true : false;
var bIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
var bIE7 = (navigator.appVersion.indexOf("MSIE 7.") == -1) ? false : true;
var bIPhone = false;



var iWidthBox = 355;
var iMarginBox = 10;
var iPaddingHor = 20;



var addthis_config = {
        services_compact: 'facebook, twitter, myspace, email',
        services_exclude: 'print'
}




$(document).ready(function(){
	setElementsWidth();
});


/**
 * inits masonry. Needed because of margin issues in Firefox
 * @return void
 */
function initMasonry () {
	iWidthAll = iWidthBox + iMarginBox;
	//alert(iWidthBox + ', ' + iWidthAll);
	$('#wrap').masonry({
		columnWidth: iWidthAll,
		itemSelector: '.box',
		singleMode: true
	});
	$('.box').css('visibility', 'visible');
	// and the other elements:
	$('#navmenu').css('visibility', 'visible');
	if (document.getElementById('logo')) {
		$('#logo').css('visibility', 'visible');
	}
	if (document.getElementById('logo_standard')) {
		$('#logo_standard').css('visibility', 'visible');
	}
	if (document.getElementById('calendar_start')) {
		$('#calendar_start').css('visibility', 'visible');
	}
}


/**
 * calculating and setting of widths of elements
 * set elements with min width and as much elements as possible in one row. Resize the elements to set right margin to zero
 * @return void
 */
function setElementsWidth () {
	iWidthBoxOld = iWidthBox;
	oContainer = document.getElementById('container');
	iWidthContent = oContainer.offsetWidth - 20;
	
	// correct navi padding:
	iWidthNavItems = 0;
	iCounter = 0;
	while (document.getElementById('nav_item_' + iCounter)) {
		iWidthNavItems += document.getElementById('nav_item_' + iCounter).offsetWidth;
		iCounter++;
	}
	iPadding = Math.floor((iWidthContent - iWidthNavItems) / (iCounter - 1));
	if (iPadding < 10) {
		iPadding = 10;
	}
	$('ul#navmenu li').css('padding-right', iPadding + 'px');
	
	
	iMinWidth = 375;
	
	//alert(iWidthNew);
	iElements = 1;
	iWidthBox = iWidthContent;
	if (!bIPhone) {
		iElements = Math.floor((iWidthContent + iMarginBox) / (iMinWidth + iMarginBox));
		iWidthBox = Math.floor(((iWidthContent + iMarginBox) / iElements) - iMarginBox) - iPaddingHor;
	}
	
	// now assign new width to boxes and columns:
	$('div.box').css('width', iWidthBox + 'px');
	
	if (document.getElementById('logo')) {
		document.getElementById('logo').style.width = (iWidthBox + 30) + 'px';
	} else if (document.getElementById('logo_standard')) {
		document.getElementById('logo_standard').style.width = (iWidthBox + 30) + 'px';
	}
	// addjust calendar on start site if it exists
	if (document.getElementById('calendar_start')) {
		$('div.cal_col').css('width', (iWidthBox + 20) + 'px');
	}
	
	
	// resize images
	iCounter = 0;
	fFactor = iWidthBox / iWidthBoxOld;
	while (oImg = document.getElementById('m_img_' + iCounter)) {
		iNewHeight = Math.floor(fFactor * oImg.height);
		oImg.height = iNewHeight;
		oImg.width = iWidthBox;
		iCounter++;
	}
	
	// resize iframes:
	$('iframe').css('width', iWidthBox + 'px');
	iNewHeight = 227 * iWidthBox / (iMinWidth - iPaddingHor);
	$('iframe').css('height', iNewHeight + 'px');
	
	setTimeout("initMasonry()", 300);
}


/**
 * finds position of element
 * @param object obj
 * @return int array
 */
function findPos (obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft, curtop];
}


/**
 * opens layer with social bookmark links. Position and slideDown
 * @param object oCallerLayer
 * @return void
 */
function openSocialLinks (oCallerLayer) {
	aTmp = findPos(document.getElementById('container'));
	iDiffX = aTmp[0];
	aTmp = findPos(oCallerLayer);
	oLinks = document.getElementById('social_links');
	oLinks.style.left = (aTmp[0] - iDiffX) + 'px';
	oLinks.style.top = (aTmp[1] + 10) + 'px';
	$('#social_links').slideDown(320, function() {
		
	});
}


var bNewsletterSubDisplayed = false;
/**
 * changes display of newsletter subscription layer
 * @param object oCaller
 * @return void
 */
function toggleNlSubscription (oCaller) {
	bNewsletterSubDisplayed = !bNewsletterSubDisplayed;
	if (bNewsletterSubDisplayed) {
		oSubscript = document.getElementById('nl_subscript');
		aTmpDiff = findPos(oSubscript);
		aTmp = findPos(oCaller);
		oSubscript.style.top = (aTmp[1] + oCaller.offsetHeight + 2) + 'px';
		oSubscript.style.left = (aTmp[0] - oSubscript.offsetWidth - 126) + 'px';
		$('#nl_subscript').show("slide", { direction: "up" }, 500);
		setTimeout("focusEmailField()", 600);
	} else {
		$('#nl_subscript').hide("slide", { direction: "up" }, 1000);
	}
}


function focusEmailField () {
	document.getElementById('email').focus();
}

