/*
	filename: arcor-gen-popup.js
	Basic Javascript for eshop.arcor.net
	version 1.0.0 - 2006-07-11

	This code is free software; you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published
	by the Free Software Foundation; either version 2.1 of the License,
	or (at your option) any later version.

	Lesser GPL license text: http://www.gnu.org/licenses/lgpl.txt
	author: christian dot wania at bluemars dot net
*/

/* Configuration */
// Array structure:	width, height, scrollbars, resizable, [top], [left]
var aPopups = new Array();
	aPopups['perlen-der-region.html'] = new Array(722, 620, false, false);


var oPopup = null;


function windowOpener(sURL, nWidth, nHeight, sName, winX, winY, bScrollbars, bLocation, bStatus, bPersonalbar, bResizable, bToolbar, bMenubar, bDependent, bDirectories) {
	if(sURL.href) sURL = sURL.href;
	// set defaults
	if (!nWidth)		nWidth = 300;
	if (!nHeight)		nHeight = 300
	if (!sName)			sName = 'popupWindow';
	if (!winX)			winX = 50;
	if (!winY)			winY = 50;
	if (!bScrollbars)	bScrollbars = "no"; else bScrollbars = "yes";
	if (!bLocation)		bLocation = "no";
	if (!bStatus)		bStatus = "no";
	if (!bPersonalbar)	bPersonalbar = "no";
	if (!bResizable)	bResizable = "no"; else bResizable = "yes";
	if (!bToolbar)		bToolbar = "no";
	if (!bMenubar)		bMenubar = "no";
	if (!bDependent)	bDependent = "no";
	if (!bDirectories)	bDirectories = "no";
	
	if ((typeof oPopup != 'undefined') && (oPopup != null)) {
		if(typeof oPopup.closed != undefined) {
			if (!oPopup.closed) oPopup.close();
		} else {
			try	{
				oPopup.close();
			} catch (e) {
				//do nothing popup reference is lost
			}		
		}
	}
	
	oPopup = window.open(sURL, sName, "width=" +nWidth+ ",height=" +nHeight+ ",left=" +winX+ ",top=" +winY+ ",screenX=" +winX+ ",screenY=" +winY+ ",scrollbars=" +bScrollbars+ ",location=" +bLocation+ ",status=" +bStatus+ ",personalbar=" +bPersonalbar+ ",resizable=" +bResizable+ ",toolbar=" +bToolbar+ ",menubar=" +bMenubar+ ",dependent=" +bDependent+ ",directories=" +bDirectories);
	if ( (oPopup != undefined) && (oPopup != null) ) {
		if (typeof oPopup.focus == 'function') oPopup.focus();
		if (oPopup.opener != undefined) oPopup.opener = window;
	} else oPopup = null;
	return oPopup;
}


function openPopup(oObj){
	var retVal = false;
	sFilename = oObj.href.replace(/(.*)\/(.*?)(\..*?)($|\?|;)(.*)/gi, "$2$3");
	
	if (sFilename != '') {
		var bFound = false;
		for (key in aPopups) {
			if (key == sFilename) {
				if (typeof aPopups[key][4] != 'undefined') xPos = aPopups[key][4]; else xPos = null;
				if (typeof aPopups[key][4] != 'undefined') yPos = aPopups[key][5]; else yPos = null;
				windowOpener(oObj.href, aPopups[key][0], aPopups[key][1], null, xPos, yPos, aPopups[key][2], null, null, null, aPopups[key][3], null, null, null, null);
				bFound = true;
			}
		}
		if (!bFound) retVal = true;
	}
	return retVal;
}

/**
 * 
 */
function removeHint(oObj, sHintTxt) {
	if (oObj.value == sHintTxt){
		oObj.value = '';
		$(oObj).removeClass('jsDefaultValue');
	}
}
function addHint(oObj, sHintTxt) {
	if (oObj.value == '') {
		oObj.value = sHintTxt;
		$(oObj).addClass('jsDefaultValue');
	}
}

function initFormLabels() {
	$('.ce_form form input[class*=text], textarea').each(function() {
		var id    = $(this).attr('id');
		var value = $('label[for='+id+']').text();
		if ($(this).hasClass('mandatory')) value += ' *';
		if ($(this).val() == '') {
			addHint(this, value);
		}
		$(this).focus(function() {  removeHint(this, value);  });
		$(this).blur(function()  {  addHint(this, value);  });
	});
	/* löschen der default-Einträge bei Form-Submit */
	$('.ce_form form').each(function() {
		$(this).submit(function() {
			$(this).find('input[class*=text], textarea').each(function() {
				var id    = $(this).attr('id');
				var value = $('label[for='+id+']').text();
				if ($(this).hasClass('mandatory')) value += ' *';
				if ($(this).val() == value) {
					removeHint(this, value);
				}
			});
		});
	});
}

function msOverProject(oObj) {
	try {
		oImg = $(oObj).find('img')[0];
		oImg.src	= oImg.src.replace(/-no/, '-ms');
		oNewImg = new Image();
		oNewImg.src = oImg.src; 
		oImg.width	= oNewImg.width;
		oImg.height	= oNewImg.height;

		$(oObj).addClass('hover');
		$(oObj).find('.metaNav').show();
	} catch(e) { try { console.info(e); } catch(e) {} }
}
function msOutProject(oObj) {
	try {
		oImg = $(oObj).find('img')[0];
		oImg.src = oImg.src.replace(/-ms/, '-no'); 
		oNewImg = new Image();
		oNewImg.src = oImg.src; 
		oImg.width	= oNewImg.width;
		oImg.height	= oNewImg.height;

		$(oObj).removeClass('hover');
		$(oObj).find('.metaNav').hide();
	} catch(e) { try { console.info(e); } catch(e) {} }
}
function initProjectListHover() {
	try {
		$('.index #pageContainer .project_listing .projectTeaserRow').hoverIntent({
			sensitivity	:	7,
			interval	:	50,
			over		:	function() {  msOverProject(this);  },
			timeout		:	500,
			out			:	function() {  msOutProject(this); }
		});
	
		$('.index #pageContainer .project_listing .projectTeaserRow').each(function() {
			try {
				var LinkTarget = $(this).find('a')[0].href;
				$(this).bind('click.translocate', function(){
					window.location.href = LinkTarget; 
				});
				if (LinkTarget != undefined) $(this).css('cursor', 'pointer');
			} catch(e) {} 
		});
		$('.index #pageContainer .project_listing .projectTeaserRow .metaNav').hide();
	} catch(e) { try { console.info(e); } catch(e) {} }
}
var bContentHidden = false;
function hideDynamicContent() {
	if (!bContentHidden) {
		bContentHidden = true;
		$('.invisibleContent').hide();
		$('.moreLinkAnchor').after('<a href="javascript:void(0);" class="right invisibleTrigger detailLink" style="margin-right:20px;background:transparent url(./tl_files/wifrm/img/shared/pfeilorange.gif) no-repeat scroll 100% 3px; line-height:16px; padding:1px 10px 0 0;">Mehr</a>');
		$('.invisibleTrigger').bind('click', function(){
			$('.invisibleContent').show(500);
			$(this).hide();
		});
	}
}



function msOverLogo(oObj) {
	try {
		$('.memberLogoClone').remove();
		
		var sID = oObj.id.replace(/logoHoverLink/, 'memberLogo');
		oImg = $('#'+sID).find('img')[0];
		if (oImg.src.indexOf('default') == -1) {
			// Container anpassen
			oDFN = $('#'+sID)[0];
			oDFN.style.width = oImg.width + 'px';
			oDFN.style.height = oImg.height + 'px';
			oDFN.style.position = 'relative';

			var offset = $(oDFN).offset();

			oClone = $(oImg).clone()[0];
			oClone.width			= oImg.width * 2;
			oClone.height			= oImg.height * 2;
			oClone.style.position	= 'absolute';
			oClone.style.top		= offset.top - (oImg.height /2) + 'px';
			oClone.style.left		= offset.left - ((oImg.width /2)-1) + 'px';
			oClone.style.border		= '2px solid #FFFFFF';
			oClone.id				= sID+'Clone';
			oClone.className		= 'memberLogoClone';
			oClone.zIndex			= 1000;

			oImg.zIndex			= 1;

			$('body').append(oClone);
		}
	} catch(e) { try { console.info(e); } catch(e) {} }
}
function msOutLogo(oObj) {
	try {
		var sID = oObj.id.replace(/logoHoverLink/, 'memberLogo');
		$('#' + sID + 'Clone').remove();
	} catch(e) { try { console.info(e); } catch(e) {} }
}
function initLogoHover() {
	try {
		$('.members .memberLogoContainer img').each(function() {
			this.width  = this.width / 2;
			this.height = this.height / 2;
				
		});
		$('.logoHoverLink').hoverIntent({
			sensitivity	:	7,
			interval	:	0,
			over		:	function() {  msOverLogo(this); },
			timeout		:	500,
			out			:	function() {  msOutLogo(this); }
		});
	} catch(e) { try { console.info(e); } catch(e) {} }
}


function getImageSource( src ) {
	return src.replace(/-no\./, "-ms.");
}
function preloadHoverImages() {
	$(window).bind('load', function() {
		$('img[src*=-no.]').each( function( key, elm ) {
			$('<img>').attr( 'src', getImageSource( $(this).attr('src') ) );
		});
	});
}


function initPopups() {
	$('.popupLink').each(function() {
		this.onclick = function() { return openPopup(this); }
	});
}      
           
      

function initPage() {
	hideDynamicContent();
	initProjectListHover();
	initFormLabels();
	initPopups();
	// initLogoHover();
	preloadHoverImages();
}
