// Shared code for CNGC 2010
//

// firebug console logging 
// from: http://css-tricks.com/snippets/javascript/error-free-firebug-logging/
var fb = {}; //An empty object literal for holding the function
fb.log = function(obj, consoleMethod) {
       if (window.console && window.console.firebug && window.console.firebug.replace(/^\s\s*/, '').replace(/\s\s*$/, '') !== '') {
               if (typeof consoleMethod === "string" && typeof console[consoleMethod] === "function") {
                       console[consoleMethod](obj);
               } else {
                       console.log(obj);
               }
       }
}

$(document).ready(function(){
	$('.faq_content').hide();
	$('.more').corner();
	$('.faq_header').corner();
    var mn_bg;
    var mn_proxy = $('#mn_proxy');
	mn_proxy.corner("bottom");
	var isIE = Boolean(document.all);
	if (isIE && window.location.pathname =='/' )
		mn_proxy.uncorner();
	 
		fb.log(window.location.pathname);
	var isIE8 = Boolean(Object.prototype.toString.call(self.JSON) === "[object JSON]");
    var proxy_offset_left = 1; // how much is the proxy shifted to the right
	var parentOffsetLeft = $('.sf-c').offset().left;
    var proxy_offset_top = 156; // how much is the proxy elongated
    var ie_offset_left = -2;
	var switchOffRight = { mn05: 1, mn06: 1, mn07: 1, mn09: 1};
	$('.sf-c > li').hover(
		function() {
            var pid = $(this).attr('id');
			var colorMap = {
				mn02: 'fahren', // fahren
				mn03: 'fzge', // fahrzeuge
				mn04: 'tanken', // tanken
				mn05: 'wirt', // wirtschaft
				mn06: 'umwelt' // umwelt
			};
			var ie8Map = { // correction factors for ie8
				mn02: 3, // fahren
				mn03: -21, // fahrzeuge
				mn04: 0, // tanken
				mn05: -68, // wirtschaft
				mn06: 0, // umwelt
				mn07: 5, // presse
				mn09: 16// links				
			}
            if(ie8Map[pid] == null) {
                return;    
            }
			var bg_image = 'url(/static/img/subnav_al.jpg)';
			if (colorMap[pid]) {
				bg_image = 'url(/static/img/subnav_' + colorMap[pid] + ".jpg)";
			}
			// we have to find the div, shift its z-index and change its img height, and 
            // set the sub-ul to transparent...
			// finally we display it...
            mn_bg = $('#'+ pid + '>ul').css('background-color');
            $('#'+ pid + '>ul').css('background-color', 'transparent');
            // alert("hovering js..." + pid+", "+mn_bg);
            // mn_proxy.css('background-color', mn_bg);
			mn_proxy.css('background-image', bg_image);
            mn_proxy.css('z-index', '3');
            mn_proxy.css('width', $('#'+ pid + '>ul').css('width'));
            proxy_height = $('#'+ pid + '>ul').height() + proxy_offset_top;
            var offset = $('#'+ pid ).offset();
            var offset_left = offset.left - proxy_offset_left;
			if (isIE) {
				if(isIE8){
					// we now have IE 8
					offset_left = offset_left + 1;
					// IE 8 has a different left offset
					
					/* if (ie8Map[pid]) {
						offset_left = offset_left + ie8Map[pid];
					}
					*/
					// IE 8 has a different way to find the height of the subnav ul
					// seems not the rendered height is used
					// had to remove a DIV after A 
					// offset.left = offset from the ul position!!
					offset = $('#'+ pid).position();
					// alert("pos: " + offset.left);
					// offset_left = offset.left - proxy_offset_left; //+ ie_offset_left;
					proxy_height = $('#'+ pid + '>ul').height() + proxy_offset_top;
					// alert("offset_left: " + offset_left + ", proxy_height: " + proxy_height);
					// alert("html: " + $('#'+ pid + '>ul').height());
					// alert("org.height: " + $('#'+ pid + '> ul').css('height'));
				} else {
					offset = $('#'+ pid + '>ul').position();
					offset_left = ( offset.left * -1) - proxy_offset_left + ie_offset_left;
					$('.sf-c > li > ul').css('padding-top', '2em');
					$('.sf-c > li > ul').uncorner();
				}
			}
			mn_proxy.css('height', proxy_height + 'px');
			// mn_proxy.offset( {top: 0, left: offset.left});
            mn_proxy.css('left', offset_left + 'px').css('top', '0px');
			fb.log("mnproxy.left: real_offset_left " + offset_left);
			// now we have to switch off/on following startpage elements...
			if (switchOffRight[pid])
				$('#text_right').hide();
			if (pid == 'mn02' || pid == 'mn03')
				$('#s_wirt').hide();
			if (pid == 'mn05' || pid == 'mn06')
				$('#s_aktuell').hide();		
			if (pid == 'mn07' || pid == 'mn09')
				$('#s_presse').hide();							
            mn_proxy.show();

		},
		function() {
            var pid = $(this).attr('id');
            $('#'+ pid + '>ul').css('background-color', mn_bg);
			mn_proxy.hide();
			// switch on startpage stuff...
			// if (! switchOffRight[pid])
			$('#text_right').show();
		}
	);  
    	
	$('.more_nav > div#right > a').click(function(){
		var myOwn = $(this).closest('.more');
		myOwn.toggle();
		myOwn.next().show();
		}	);

	$('.more_nav > div#left > a').click(function(){
		var myOwn = $(this).closest('.more');
		myOwn.toggle();
		myOwn.prev().show();
		}	);
	
	$('.faq_header').toggle(function() {
			var faqC = $(this).next();
			faqC.fadeIn();
			},
			function() {
			var faqC = $(this).next();
			faqC.fadeOut();
			}
	)	


    $('.hlist > ul > li > a').hover(
        function() {
           // alert("hover ok");
           $(this).prev('.mn_pull').show();
        },
        function() {
           $(this).prev('.mn_pull').hide();
            
        }
    );
    $('#m_s_auto  area').hover(
        function() {
			var texts = { presse : 'right', aktuell : 'right',
				tanken: 'right',
				impressum : 'left' };
            var showMe = $(this).attr('id').split("_").pop();
			$('.s_icons').each(function() {
				if ($(this).attr('id') != ('s_' + showMe)) {
					$(this).fadeOut();				
					}
				
				
			});
            $('#s_' + showMe).fadeIn();
			if (texts[showMe]) {
				$('#text_' + texts[showMe]).fadeOut();
			} else {
					$('#text_right').fadeIn(); 
					$('#text_left').fadeIn(); 
			}
        },
        function() {
            // alert("off");
        }
    );
    // "popup" graphics
    $('.popup').click(    
        function() {
            var src = $(this).attr('rel');
            var wh = $(this).attr('title');
            var w = 0;
            var h = 0;
            var popup = $('#popup');
            if (wh && wh.indexOf(" ")) {
                var whl = wh.split(" ");
                w = parseInt(whl[0]) ;
                h = parseInt(whl[1]) + 38; // allow for schliessen-element
                popup.css("height", h + "px" );
                popup.css("width", w + "px" );
            }
            // $('#popup_caption').html(caption);
			if (src.indexOf('mp4') > -1) {
				img = '<div ></div>';
				h = h + 150;
				popup.css('height', h + 'px');
				  {
					fb.log("pop h: " + h + ", w: " + w);
				}
			}
			else {
				var img ='<img src="/static/uploads' + src + '" border=0" />';
				$('#popup_picture').html(img);
			}
			// $('#col2_content').css('z-index', 3);
            popup.fadeIn ();    
        } 
    
    );    
    
    $('#popup_close').click(
        function() {
          // alert("wookie");
          $('#popup').hide();      
            }
    );    
	
	$('.cng_open').click(
		function() {
			// if $('')
			var wh = [860, 740];
			var rel = $(this).attr('rel');
			// alert("goo");
			if (! rel)
				rel = "";
			 
				fb.log("we open now...");
		    var nwl = (screen.width - wh[0])/2;
		    var nwh = (screen.height - wh[1])/2;
			var page = "/cngt/" + rel;
			var windowname = "_CNGT_";
		    var resizable = true;
		    var scrollbars = true;
     	    var status, toolbar, locationbar, menubar, scrollbars, resizable = false;
		    // status = true;
		    var toolbar = true;
		    var menubar = true;
		    var win = window.open( page, windowname,
		        ( wh[0] ? "width=" 	+ wh[0] + "," : "" ) +
		        ( wh[1] ? "height=" 	+ wh[1] + "," : "" ) +
		        "status=" 		+ ( status 		? "yes" : "no" ) + "," +
		        "toolbar=" 	+ ( toolbar 	? "yes" : "no" ) + "," +
		        "locationbar=" 	+ ( locationbar 	? "yes" : "no" ) + "," +
		        "menubar=" 	+ ( menubar 	? "yes" : "no" ) + "," +
		        "scrollbars=" 	+ ( scrollbars 	? "yes" : "no" ) + "," +
		        "resizable=" 	+ ( resizable 	? "yes" : "no" ) + "," +
		        ',left='+nwl+',top='+nwh+''
		    );
		    win.focus();								
		}
	);
	
    $('p.foerder a').click(
        function() {
            var hr = $(this).attr('href');
            // alert("hr: " + hr);
            var myOwn = $(this).closest('.more');
            var openMe = $('h3' + hr).closest('.more');
            myOwn.toggle();
            openMe.show();
        }
    );
	
	
   
});

