﻿/*
 * jQuery for OBV
 * 
 * Copyright (c) 2011 Michael Stamm
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 */

(function($){
 
 // slideBlender
 
 $.fn.slideBlender = function(config){

  var settings = jQuery.extend({
   "nestSelector": "ul",
   "itemSelector": "li",
   "firstBlendIndex": 0,
   "zBase": 500,
   "duration":250,
   "showList": true,
   "autoBlend": false,
   "timer":5000
  }, config);

  var animationRunning = false;
  var blendIndexLast = settings.firstBlendIndex;
  var noMapBlending = parseInt($.browser.version.slice(0,1)) == 8 && $.browser.msie ? true : false;
  var useStaticMap = parseInt($.browser.version.slice(0,1)) < 8 && $.browser.msie ? true : false;
  
  // private function
  function isMap(sliderObj){
   return $(sliderObj).children("div:eq(0)").hasClass("map");
  }
  
  // private function
  function blendTo(objHandle, settings, blendIndexNext, blendIndexCurrent){
   if(blendIndexNext != blendIndexCurrent && animationRunning == false){

    var nestHandle = objHandle.children("div.blenderNest:eq(0)");
    
    animationRunning = true;

    var isNextMap = isMap(nestHandle.children("div.blenderSlide:eq(" + blendIndexNext + ")"));
    var isCurrentMap = isMap(nestHandle.children("div.blenderSlide:eq(" + blendIndexCurrent + ")"));
    
    var isMapInvolved = (isNextMap || isCurrentMap) ? true : false;
    
    if(noMapBlending && isMapInvolved){
  		 nestHandle.children("div.blenderSlide:eq(" + blendIndexNext + ")").css({"z-index":settings.zBase + 50});
  		 nestHandle.children("div.blenderSlide:eq(" + blendIndexCurrent + ")").css({"z-index":settings.zBase});
     blendIndexLast = blendIndexNext;
     animationRunning = false;
    }
    else{
     nestHandle.children("div.blenderSlide:eq(" + blendIndexNext + ")").css({"opacity":1, "z-index":settings.zBase + 25});
     nestHandle.children("div.blenderSlide:eq(" + blendIndexCurrent + ")").css({"opacity":1, "z-index":settings.zBase + 50});
     nestHandle.children("div.blenderSlide:eq(" + blendIndexCurrent + ")").animate(
      {"opacity":0},
      {duration:settings.duration,
     	 complete:function(){
     		 nestHandle.children("div.blenderSlide:eq(" + blendIndexNext + ")").css({"z-index":settings.zBase + 50});
     		 nestHandle.children("div.blenderSlide:eq(" + blendIndexCurrent + ")").css({"z-index":settings.zBase});
        blendIndexLast = blendIndexNext;
        animationRunning = false;
        if(settings.autoBlend == true){
         window.setTimeout(autoBlendNext, settings.timer);
        }
     		}
      }
     );
    }
   }
  };

  // private function
  function nextBlendIndex(objHandle, settings, blendIndexLast){
   var nestHandle = objHandle.children("div.blenderNest:eq(0)");
   var blendIndexNext = (blendIndexLast + 1 >= nestHandle.children("div.blenderSlide").length) ? 0 : blendIndexLast + 1;
   return blendIndexNext;
  }

  // private function
  function autoBlendNext(){
   var blendIndexNext = nextBlendIndex(blendCanvasObj, settings, blendIndexLast);
   blendTo(blendCanvasObj, settings, blendIndexNext, blendIndexLast);
  }

  if($(this).children(settings.nestSelector).length == 1){
   
   // init blendSlider -> build up
   $(this).children(settings.nestSelector + ":eq(0)").before("<div class=\"blenderCanvas\"><div class=\"blenderNest\"></div></div>");
   
   $(this).children(settings.nestSelector + ":eq(0)").children(settings.itemSelector).each(function(){
    
    var blenderCanvasObject = $(this).parent().parent().children("div.blenderCanvas:eq(0)").children("div.blenderNest:eq(0)");
    
    blenderCanvasObject.append("<div>");
    blenderCanvasObject.children("div").last().attr("class", "blenderSlide");
    
    var blenderSlideType = $(this).attr("class");
    
    if(blenderSlideType == ""){
     //image
     var imageURL = "";
     if($(this).children("span.download").length == 1 && $(this).children("span.download").children().length > 0){
       var downloadURL =  $(this).children("span.download").find("a:eq(0)").attr("href");
      blenderCanvasObject.children("div").eq($(this).index()).append("<a target=\"_blank\" href=\"\"></a>");
      blenderCanvasObject.children("div").eq($(this).index()).children("a:eq(0)").attr("href", downloadURL);
      blenderCanvasObject.children("div").eq($(this).index()).children("a:eq(0)").append("<img src=\"\" alt=\"\">");
      blenderCanvasObject.children("div").eq($(this).index()).children("a:eq(0)").children("img:eq(0)").attr("src", $(this).children("a:eq(0)").attr("href"));
     }
     else{
      blenderCanvasObject.children("div").eq($(this).index()).append("<img src=\"\" alt=\"\">");
      blenderCanvasObject.children("div").eq($(this).index()).children("img:eq(0)").attr("src", $(this).children("a:eq(0)").attr("href"));
     }
    }
    else if(blenderSlideType == "map"){
     //alert("map");
     
     var mapLink = $(this).children("a:eq(0)").children("img:eq(0)").attr("src");
     var navLink = $(this).children("a:eq(0)").attr("href");
     
     if(mapLink.lastIndexOf("kluwe=") > -1){
      var mapInfo = unescape(mapLink.substr(mapLink.lastIndexOf("kluwe=") + 6)).split(";");

      var gLat = parseFloat(mapInfo[0]);
      var gLng = parseFloat(mapInfo[1]);
         
      var pointHtml = "";
      pointHtml += "<table class=\"gMapInfo\">";
      pointHtml += " <tr>";
      pointHtml += "  <th colspan=\"2\">" + mapInfo[2] + "</th>";
      pointHtml += " </tr>";
      pointHtml += " <tr>";
      pointHtml += "  <td>";
      pointHtml += "   " + mapInfo[3] + "<br>";
      pointHtml += "   " + mapInfo[4] + "<br>";
      pointHtml += "   <br>";
      pointHtml += "   <a href=\"" + navLink + "\" target=\"_blank\">Anfahrt berechnen</a><br>";
      pointHtml += "  </td>";
      pointHtml += "  <td>";
      pointHtml += "   Tel.:	" + mapInfo[5] + "<br>";
      pointHtml += "   Fax:	" + mapInfo[6] + "<br>";
      pointHtml += "  </td>";
      pointHtml += " </tr>";
      pointHtml += "</table>";
      
      if(useStaticMap){
       //alert("static");
       var imgLink = "http://maps.google.com/maps/api/staticmap?center=" + (parseFloat(gLat) + 0.02) + "," + gLng + "&zoom=12&size=438x348&sensor=false&maptype=roadmap&markers=color:red%7C" + gLat + "," + gLng;
       blenderCanvasObject.children("div").eq($(this).index()).append("<img src=\"" + imgLink + "\" alt=\"\">");
       blenderCanvasObject.children("div").eq($(this).index()).append("<div class=\"gMapBubble\">" + pointHtml + "</div>");
      }
      else if(GBrowserIsCompatible){
       var idCounter = 1;
       while(true){
        if($(".pageContent").find("#mapCanvasId" + idCounter).length == 0){
         break;
        }
        idCounter++;
       }
       
       blenderCanvasObject.children("div").eq($(this).index()).append("<div class=\"map\" id=\"mapCanvasId" + idCounter + "\"></div>");
       // gMap v2
       function createMarker(point,html) {
         var marker = new GMarker(point);
         GEvent.addListener(marker, "click", function() {
           marker.openInfoWindowHtml(html);
         });
         return marker;
       }
       var map = new GMap2(document.getElementById("mapCanvasId" + idCounter));
       map.addControl(new GLargeMapControl());
       map.setCenter(new GLatLng(gLat + 0.02,gLng),12);
     
       var point = new GLatLng(gLat,gLng);
       var marker = createMarker(point,pointHtml)
       map.addOverlay(marker);
       marker.openInfoWindowHtml(pointHtml);
       
      }
     }
    }
    else if(blenderSlideType == "video"){
     //alert("video");

     var idCounter = 1;
     while(true){
      if($(".pageContent").find("#videoCanvasId" + idCounter).length == 0){
       break;
      }
      idCounter++;
     }

     var autoPlayOnFirstSlide = ($(this).index() == 0) ? true : false;
     var videoLink = $(this).children("a:eq(0)").attr("href");
     
     var splashImage = "http://www.kluwe.de/fileadmin/www.kluwe.de/downloads/aktionen-video-01-splash.jpg"; //($(this).children("a:eq(0)").children("img:eq(0)").attr("src")).replace(/thumb/, "splash");
     
     if(videoLink.indexOf("herthakluwefinal.flv") > -1){
      splashImage = "http://www.kluwe.de/fileadmin/www.kluwe.de/downloads/herthakluwefinal.jpg";
     }
     else if(videoLink.indexOf("herthakluwe2.f4v") > -1){
      splashImage = "http://www.kluwe.de/fileadmin/www.kluwe.de/downloads/herthakluwe2_splash.jpg";
     }
     
     
     
     blenderCanvasObject.children("div").eq($(this).index()).append("<div class=\"video\" id=\"videoCanvasId" + idCounter + "\"><img src=\"" + splashImage + "\"></div>");
     $f("videoCanvasId" + idCounter,{
       // player config
       src: "/fileadmin/www.kluwe.de/templates/script/flowplayer.commercial-3.2.7.swf",
       wmode: "opaque",
       allowfullscreen: true,
       allowscriptaccess: "always", // "always"/false
       quality: "high"
      },{
       key: "#$30b99757a756b2509d9",
       clip:{
        url: videoLink,
        //
        // urls for testing purposes
        //      http://projects.pixellion.de/kwp/kluwe/html05/download/kluwe_wirgebenalles_438x323.flv
        //      /fileadmin/www.kluwe.de/downloads/kluwe_wirgebenalles_438x323.flv
        //
        autoPlay: true, //false, //autoPlayOnFirstSlide,
        autoBuffering: true,
        bufferLength: 10,
        scaling: "fit" 
       },
       screen:{
		      background:"#00000",
	      	backgroundGradient: "none",
      	 bottom: 0	// make the video take all the height
       },
       canvas:{
		      background:"#00000",
	      	backgroundGradient: "none"
       },
       splash:{
		      background:"#00000",
	      	backgroundGradient: "none"
	      },
       play:{
		      replayLabel:""
	      },
       plugins:{
      	 controls:{
      		 url:"/fileadmin/www.kluwe.de/templates/script/flowplayer.controls-3.2.5.swf",
       		play:true,
       		volume:true,
       		mute:true,
       		time:true,
       		stop:false,
       		playlist:false,
       		fullscreen:false,
       		scrubber: true,
       		
      			backgroundColor: "transparent",
      			backgroundGradient: "none",
      			sliderColor: "#FFFFFF",
      			sliderBorder: "1.5px solid rgba(160,160,160,0.7)",
      			volumeSliderColor: '#FFFFFF',
      			volumeBorder: "1.5px solid rgba(160,160,160,0.7)",
      			timeColor: "#ffffff",
      			durationColor: "#535353",
      			tooltipColor: "rgba(255, 255, 255, 0.7)",
      			tooltipTextColor: "#000000"
       	}
       }
      }
     );
    }
    

   	if($(this).index() != settings.firstBlendIndex){
   		blenderCanvasObject.children("div").eq($(this).index()).css({"z-index":settings.zBase});
   	}
   	else{
   	 blenderCanvasObject.children("div").eq($(this).index()).css({"z-index":settings.zBase + 50});
   	}
   });
   
   var blendCanvasObj = $(this).children("div.blenderCanvas:eq(0)");
   var blendBoxObj = $(this);
  
   // hide gallery or set event handler
   if(!settings.showList || blendBoxObj.children(settings.nestSelector + ":eq(0)").children(settings.itemSelector).length < 2){
    blendBoxObj.children(settings.nestSelector).css({"display":"none"});
   }
   else{    
    blendBoxObj.children(settings.nestSelector + ":eq(0)").children(settings.itemSelector).each(function(){
     $(this).bind("click", function(event){
   		 event.preventDefault();
      listItemIndex = $(this).index();
      blendTo(blendCanvasObj, settings, listItemIndex, blendIndexLast);
   		});
   	});
   }
   
   // start autoBlend
   if(settings.autoBlend == true){
    window.setTimeout(autoBlendNext, settings.timer);
   }
  }
 };
 
 
 
 // slideOut
 $.fn.slideOut = function(config){

  var settings = jQuery.extend({
   "accordionSelector":"ul",
   "itemSelector":"li",
   "headerSelector":"h2",
   "bodySelector":"div",
   "openedIndex":0,
   "classOpened":"opened",
   "classClosed":"closed",
   "duration":500
  }, config);
    
  var animationRunning = false;
  var itemObj = $(this).is(settings.accordionSelector) ? $(this).children(settings.itemSelector) : $(this);
  var isSingleSlide = $(this).is(settings.accordionSelector) ? false : true;
  itemObj.each(function(){
   $(this).attr("class", "sildeOutItem");
   $(this).children(settings.headerSelector + ":eq(0)").attr("class", "sildeOutHeader");
   var backupHeight = $(this).children(settings.bodySelector + ":eq(0)").height();
   var backupContent = $(this).children(settings.bodySelector + ":eq(0)").detach();
   $("<div></div>").append(backupContent).insertAfter($(this).children(settings.headerSelector + ":eq(0)"));
   $(this).children(settings.bodySelector + ":eq(0)").attr("backupheight", backupHeight).attr("class", "slideOutNest");
   $(this).children(settings.bodySelector + ":eq(0)").children("div:eq(0)").attr("class", "slideOutContent");
   
   if(!isSingleSlide && settings.openedIndex == $(this).index()){
    $(this).children(settings.headerSelector + ":eq(0)").addClass("opened");
   }
   else{
    $(this).children(settings.headerSelector + ":eq(0)").addClass("closed");
    $(this).children(settings.bodySelector + ":eq(0)").css({"height":"0px"});
    $(this).children(settings.bodySelector + ":eq(0)").children("div:eq(0)").css({"margin-top":"-" + backupHeight + "px"});
   }
   
   $(this).children(settings.headerSelector + ":eq(0)").bind("click", function(event){
     event.preventDefault();
     if(animationRunning == false){
      animationRunning = true;
      var clickedIndex = isSingleSlide ? 0 : $(this).parent().index();
      var clickedNestObj = isSingleSlide ? $(this).parent(".sildeOutItem:eq(" + clickedIndex + ")") : $(this).parent().parent().children(".sildeOutItem");
      var isClickedSlideOpen = clickedNestObj.eq(clickedIndex).children(settings.headerSelector + ":eq(0)").hasClass("opened");

      
      clickedNestObj.each(function(){
       var isSlideOpened = $(this).children(settings.headerSelector + ":eq(0)").hasClass("opened");
       if(isSlideOpened){
        // Close it!
        $(this).children(settings.headerSelector + ":eq(0)").removeClass("opened").addClass("closed");
        $(this).children(".slideOutNest:eq(0)").animate(
         {"height":"0px"},
         {duration:settings.duration}
        );
        $(this).children(".slideOutNest:eq(0)").children(".slideOutContent:eq(0)").animate(
         {"margin-top":"-" + $(this).children(".slideOutNest:eq(0)").attr("backupheight") + "px"},
         {duration:settings.duration,
        	 complete:function(){
        	  animationRunning = false;
        		}
         }
        );
       }
       else if((!isSlideOpened && clickedIndex == $(this).index()) || isSingleSlide && !isSlideOpened){
        // Open it!
        $(this).children(settings.headerSelector + ":eq(0)").removeClass("closed").addClass("opened");
        $(this).children(".slideOutNest:eq(0)").animate(
         {"height":$(this).children(".slideOutNest:eq(0)").attr("backupheight") + "px"},
         {duration:settings.duration}
        );
        $(this).children(".slideOutNest:eq(0)").children(".slideOutContent:eq(0)").animate(
         {"margin-top":"0px"},
         {duration:settings.duration,
        	 complete:function(){
        	  animationRunning = false;
        		}
         }
        );


       }
       
      });
     }
   });
   
  });
  
 };
})(jQuery);


 /*
      // gMap v3
      var mapLatLng = new google.maps.LatLng(gLat + 0.02, gLng);
      var markerLatLng = new google.maps.LatLng(gLat, gLng);
      var gMapOptions = {
       zoom: 12,
       center: mapLatLng,
       mapTypeId: google.maps.MapTypeId.ROADMAP,
       mapTypeControl: false,
       streetViewControl: false
      };
      
      var gMap = new google.maps.Map(document.getElementById("mapCanvasId" + idCounter), gMapOptions);
      
      var gMapInfoWindow = new google.maps.InfoWindow({
       content: pointHtml
      });

      var gMapMarker = new google.maps.Marker({
       position: markerLatLng,
       map: gMap
      });

      google.maps.event.addListener(gMapMarker, 'click', function() {
       gMapInfoWindow.open(gMap,gMapMarker);
      });
      
      gMapInfoWindow.open(gMap,gMapMarker);
*/
