/********************************************************************************************************+
 * JS for Speedex
 * @author Mario Diaz
 * @project	Speedex
 * @created 2010-05-11
 *******************************************************************************************************/
var bMsie = (document.all) ? true : false;
var bIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

// startup actions
$(document).ready(function() {
	
	// and correct for msie again. What a surprise. 
	if (bIE6) {
		if (document.getElementById('left_content') && document.getElementById('right_content')) {
			aPosLeft = findPos(document.getElementById('left_content'));
			aPosRight = findPos(document.getElementById('right_content'));
			if (aPosRight[0] - aPosLeft[0] > 610) {
				document.getElementById('right_content').style.marginLeft = '-15px';
				document.getElementById('right_content').style.marginRight = '30px';
			}
		}
	}
	
	
	// TODO: delete this:
	if (document.getElementById('logo')) {
		sTmp = document.URL;
		if (sTmp.indexOf('relaunch_2010') != -1) {
			sUrl = 'http://www.speedex.de/relaunch_2010/startseite.html';
			document.getElementById('logo').innerHTML = '<a href="' + sUrl + '"><img src="fileadmin/Layouts/images/logo.jpg" alt="" /></a>';
		}
	}
});



// for preloading images:
var aTmpPreload = new Array();


/**
 * preloads images:
 * @param aImgArr
 * @return void
 */
function preloadImages (aImgArr) {
	iStart = aTmpPreload.length;
	for (i = 0; i < aImgArr.length; i++) {
		aTmpPreload[iStart + i] = new Image();
		aTmpPreload[iStart + i].src = aImgArr[i];
	}
}


/**
 * 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];
}


/**
 * function for main navi. Uses jquery
 * @return void
 */
function navigation () {
    $('#masthead ul li a.top').mouseover( function() {
        $(this).addClass('selected');
        $(this).siblings('ul').addClass('selected');
        $('ul.active').css({display: 'none'});
        $('#masthead a#active').removeClass('active');
        $('#masthead a#active').removeClass('selected');
    });
    $('#masthead ul ul').mouseover( function() {
    	// MSIE: only attach class to a if different:
        $(this).siblings('a').addClass('selected');
        $(this).addClass('selected');
        $('ul.active').css({display: 'none'});
        $('#masthead a#active').removeClass('active');
        $('#masthead a#active').removeClass('selected');
    });
    $('#masthead ul li a.top').mouseout( function() {
        $(this).removeClass('selected');
        $(this).siblings('ul').removeClass('selected');
        $('ul.active').css({display: 'block'});
        $('#masthead a#active').addClass('active');
    });
    $('#masthead ul ul').mouseout( function() {
        $(this).removeClass('selected');
        $(this).siblings('a').removeClass('selected');
        $('ul.active').css({display: 'block'});
        $('#masthead a#active').addClass('active');
    });
    
    $('#masthead ul li a.active').mouseover( function() {
        $(this).addClass('selected');
        $(this).siblings('ul').addClass('selected');
        $('ul.active').css({display: 'block'});
        $('#masthead a#active').addClass('active');
    });
    $('#masthead ul ul.active').mouseover( function() {
        $(this).addClass('selected');
        $('ul.active').css({display: 'block'});
        $('#masthead a#active').addClass('active');
    });
    $('#masthead ul li a.active').mouseout( function() {
        $(this).addClass('selected');
        $('#masthead a#active').addClass('active');
    });
    $('#masthead ul ul.active').mouseout( function() {
        $(this).addClass('selected');
        $('#masthead a#active').addClass('active');
    });
}



/* *** google maps ******************************************************************** */
/**
 * shows google maps address
 * @param string address
 * @param string info
 * @return void
 */
function showAddress (address, info) {
	geocoder.getLatLng(address,
						function (point) {
							if (!point) {
								alert("Could not find postcode " + address);
							} else {
								oMap.setCenter(point, 13);
								var marker = new GMarker(point);
								oMap.addOverlay(marker);
								
								marker.openInfoWindowHtml(info);
							}
						}
	);
}


var gdir;


/**
 * calls method for displaying route in google maps api
 * @param string fromAddress
 * @param string toAddress
 * @param string locale
 * @return void
 */
function setDirections (fromAddress, toAddress, locale) {
  	gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}


/**
 * Use this function to access information about the latest load() results.
 * @return void
 */
function onGDirectionsLoad () { 
    
}


/**
 * shows calc form
 * @return void
 */
function showCalcForm () {
	document.getElementById('route_calc_form').style.display = 'block';
	aTmp = findPos(document.getElementById('gmaps'));
	window.scrollTo(0, aTmp[1]);
	document.forms['route_calc'].route_street.focus();
}


/**
 * handles error codes passed by google maps api
 * @return void
 */
function handleErrors () {
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	   displayError("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	   displayError("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.<br /> Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	   displayError("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.<br /> Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	   displayError("The given key is either invalid or does not match the domain for which it was given.<br /> Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	   displayError("A directions request could not be successfully parsed.<br /> Error code: " + gdir.getStatus().code);
    
   else displayError("An unknown error occurred.");
}



/* *** contact person ********************************************************************** */
var bDisplayContactPerson = false;
/**
 * changes display of contact person data
 * @return void
 */
function toggleDisplayContactPerson () {
	if (document.getElementById('contact_person_data')) {
		bDisplayContactPerson = !bDisplayContactPerson;
		document.getElementById('contact_person_data').className = bDisplayContactPerson ? 'fs12' : 'dn';
	}
}



