// JavaScript Document
var menuOpen = false;
var openIndex = 0;
var isCarPresenter = false;

function showRoute( id )
{
    window.open( 'http://www.yellowmap.de/Filialfinder/Html/Map.aspx?MapPartnerIds=' + id + '&Partner=BMW2&SecurityID=3aeX4SPn8XvxlD%2Fh7YeaTA%3D%3D&bmw_haendlerimg=','routen','width=800,height=530,scrollbars=yes' );
}

function hundertprodiesel()
{
	window.open( '/bmw_hundertprodiesel.html', 'hundertpro', 'width=590,height=580' );
}

function addclickhandler()
{
    document.onclick = hideAllFavs;
    if (document.captureEvents) document.captureEvents(Event.CLICK);
    window.setInterval( "CheckPosition()", 100 );
}

function CheckPosition()
{
    if( navigator.userAgent.indexOf( "MSIE 6" ) > 0 )
    {
        var p = document.getElementById( "popDetailAnsicht" );
        if( p && p.style )
        {
            p.style.top = ( 180 - document.body.scrollTop ) + "px";
        }
        
        var p1 = document.getElementById( "prozente" );
        if( p1 && p1.style )
        {
            p1.style.top = ( 303 - document.body.scrollTop ) + "px";
        }
        
        var p2 = document.getElementById( "multiSlider_outer" );
        if( p2 && p2.style )
        {
            p2.style.top = ( 0 - document.body.scrollTop ) + "px";
        }

        var p3 = document.getElementById("moduleNavigation");
        if (p3 && p3.style) 
        {
            p3.style.top = (74 - document.body.scrollTop) + "px";
        }

        var p4 = document.getElementById("flashdiv");
        if (p4 && p4.style) {
            p4.style.top = (0 - document.body.scrollTop) + "px";
        }

        var p5 = document.getElementById("topRow");
        if (p5) {
            p6 = p5.parentNode;
            if (p6 && p6.style) {
                p6.style.top = (0 - document.body.scrollTop) + "px";
            }
        }
    }
}


function toggleMenu( idx )
{
    if( menuOpen )
    {        
        hideFavLayers();
        menuOpen = false;
    }
    else
    {
        menuOpen = true;
        showFav( idx );
    }
}

function hideAllFavs()
{
    if( !e ) var e = window.event;
    if( e && e.cancelBubble ) e.cancelBubble = true;
    if( e && e.stopPropagation ) e.stopPropagation();
	
    hideFavLayers();
    menuOpen = false;
}

function hideFavLayers()
{
    for( i = 1; i < 20; i++ )
    {
        hideFav(i);
    }    
}

function showFavorites()
{
    myTab = document.getElementById( "favoritesTab" );
    myLayer = document.getElementById( "favoritesLayer" );
    
    if( myTab && myLayer )
    {
        myTab.style.color = "#069";
        myTab.style.background = "url(/img/white.gif)";
        
        myLayer.style.visibility = "visible";
        a = getPosition( myTab );
        myLayer.style.left = (a.x+6) + "px";
        myLayer.style.top = (a.y + myTab.offsetHeight + 20) + "px";
    }
}

function hideFavorites()
{
    myTab = document.getElementById( "favoritesTab" );
    myLayer = document.getElementById( "favoritesLayer" );
    
    if( myTab && myLayer )
    {
        myTab.style.color = "";
        myTab.style.background = "#898989";
        
        myLayer.style.visibility = "hidden";
    }
}

function showFav( idx )
{
    if( menuOpen )
    {
        hideFavLayers();

	    /* Sichtbar machen und positionieren der Favoriten*/
	    myTab = document.getElementById("favTab" + idx);
	    myLayer=document.getElementById("favoLayer" + idx);

        if( myTab && myLayer ) 
        {
	        myTab.style.color = "#069";
	        myTab.style.backgroundColor = "#fff";

	        myLayer.style.visibility = 'visible';
	        a=getPosition(myTab);

            if( myLayer.style.width == "458px" )
            {
                myLayer.style.left = "299px";
            }
            else
            {
	            myLayer.style.left = (a.x-1)+'px';
	        }
	        myLayer.style.top = (a.y + myTab.offsetHeight)+"px";
	    }
	}
}
	
function hideFav( idx )
{
	/* unsichtbar machen und positionieren der Favoriten*/
	myLayer=document.getElementById("favoLayer"+idx);
	myTab = document.getElementById("favTab" + idx);
	myText = document.getElementById("favTab"+idx);

    if( myLayer && myText ) 
    {
	    myLayer.style.visibility = 'hidden';
	    myTab.style.backgroundColor = "transparent";
	    myTab.style.color = "";
	}
}
	
function showContact(id){
	/* toggelt das Kontaktformular*/
	var contact=document.getElementById(id);
	test = contact.style.display
	if (test=='block'){
		contact.style.display='none';
		}
		else{
		contact.style.display='block';
		}
	
}

function showCallback(id) {
    $("[for$=KontaktEmail]").html("E-Mail");
    $("[id$=FormType]").val("callback");
    $("[id$=CustomeSatisfactionForm]").hide();
    location.href = "#form";
}

function showFeedback(){
	/* toggelt das feedbacktformular*/
	var feedback=document.getElementById("FeedbackLayer");
	test = feedback.style.display;
	if (test=='block'){
		feedback.style.display='none';
		}
		else{
		feedback.style.display='block';
		}
	
}

function setBorder(imgRef)
{
	a =document.getElementById(imgRef)
	a.style.border='1px solid blue'
	a.style.width='39px';
	a.style.height='27px';
}

function deleteBorder(imgRef)
{
	a =document.getElementById(imgRef)
		a.style.border='1px solid white'
	a.style.width='39px';
	a.style.height='27px';
}

function switchImage( imgName, imgSrc, index )
{
  if (document.images)
  {
    if (imgSrc != "none")
    {
      document.images[imgName].src = imgSrc;
      openIndex = index;
    }
  }
}

function switchImageIndexed(imgName, imgSrc, index )
{
  if (document.images)
  {
    if (imgSrc != "none")
    {
      document.images[imgName].src = imgSrc;
      currentIndex = index;
    }
  }
}

function PrevImage()
{
    currentIndex--;
    if( currentIndex < 0 ) 
    {
        currentIndex = MaxIndex;
    }
    switchImage( "popUpImg", pics[currentIndex] );
}

function NextImage()
{
    currentIndex++;
    if( currentIndex > MaxIndex )
    {
        currentIndex = 0;
    }
    switchImage( "popUpImg", pics[currentIndex] );
}

function popup()
{
    if( MaxIndex != -1 )
    {
        currentIndex = openIndex;
        showPopup( 'popUpImg', pics[currentIndex], currentIndex );
    }
}

function showPopup(imgName, imgSrc, index )
{
    if( isCarPresenter ) 
    {
        return;
    }
    
	var popup = document.getElementById( "popDetailAnsicht" );
	if( document.getElementById( imgName ) ) 
	{
	    document.getElementById( imgName ).src = imgSrc;
	    currentIndex = index;
	}
	popup.style.display = 'block';
}

function closePopup()
{
	var popup = document.getElementById( "popDetailAnsicht" );
	popup.style.display = 'none';
}

function getPosition(element)
{
  var elem=element;
  tagname="",x=0,y=0;
  while (element && (typeof(element)=="object") && (typeof(element.tagName)!="undefined"))
  {
    y+=element.offsetTop;     /* Offset des jeweiligen Elements addieren */
    x+=element.offsetLeft;    /* Offset des jeweiligen Elements addieren */
    tagname=element.tagName.toUpperCase(); /* tag-Name ermitteln, Grossbuchstaben */
    if (tagname=="BODY")
      elem=0;

    if (typeof(element)=="object")
      if (typeof(element.offsetParent)=="object")
        element=element.offsetParent;
  }
  
  
  position=new Object();
  position.x=x;
  position.y=y;
  return position;
}

function alertPosition(elementId)
/* gibt eine Meldung mit x und y des zu elementId gehoerenden Elements aus */
{
  var a,element;

/* Element-Objekt zur ID ermitteln */
  element=document.getElementById(elementId);
 
/* Position bestimmen und melden */
  a=getPosition(element);
  window.alert("Position "+elementId+": ("+a.x+","+a.y+")");
}


// * Dependencies * 
function setColor(objName, fg, bg)
{
  obj = findObj(objName);
  if (obj.style)
  {
    obj.style.color = fg;
    obj.style.backgroundColor = bg;
  }
}

	
// Example: obj = findObj("image1");

function findObj(theObj, theDoc)
// Example: obj = findObj("image1");
{

  var p, i, foundObj;
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)

  {

    theDoc = parent.frames[theObj.substring(p+1)].document;

    theObj = theObj.substring(0,p);

  }

  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];

  for (i=0; !foundObj && i < theDoc.forms.length; i++) 

    foundObj = theDoc.forms[i][theObj];

  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 

    foundObj = findObj(theObj,theDoc.layers[i].document);

  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

  

  return foundObj;

}

// * Dependencies * 

// this function requires the following snippets:

// JavaScript/readable_MM_functions/findObj

//

// Accepts a variable number of arguments, in triplets as follows:

// arg 1: simple name of a layer object, such as "Layer1"

// arg 2: ignored (for backward compatibility)

// arg 3: 'hide' or 'show'

// repeat...

//

// Example: showHideLayers(Layer1,'','show',Layer2,'','hide');

function showHideLayers()

{ 

  var i, visStr, obj, args = showHideLayers.arguments;

  for (i=0; i<(args.length-2); i+=3)

  {

    if ((obj = findObj(args[i])) != null)

    {

      visStr = args[i+2];

      if (obj.style)

      {

        obj = obj.style;

        if(visStr == 'show') visStr = 'visible';

        else if(visStr == 'hide') visStr = 'hidden';

      }

      obj.visibility = visStr;

    }

  }

}

