/*-----------------------------------------
 
 copyright 2020 by mmc:agentur
 
 Version: 1.0
 
 Author: David Rerych
 
 Author URI: http://www.mmc-agentur.at
 
 -------------------------------------------*/

var ie7 = ($.browser.msie && ($.browser.version <= 7)) ? true : false;

// Omniture Initialisation

var Omniture = false;
document.domain = 'nivea.at';
if (parent != self && typeof parent.Omniture !== 'undefined') {
   Omniture = parent.Omniture;
}


$(document).ready(function(){
  
  if (location.hash) {
    log('hash:'+location.hash);
  }
 

	$(".setswf").swfElement();
	
	$("#small-teasers ul").jcarousel({
		wrap:"circular",
		initCallback:function(n){
			n.buttonPrev.add(n.buttonNext).dblclick(function(o){
				if(window.getSelection){window.getSelection().removeAllRanges()}
			});
			var maxteasers = 4;
			if ($('body').hasClass('contentpage') || $('body').hasClass('familienfest')) {
				maxteasers = 3;
			}
			if(n.container.find("li.jcarousel-item").filter(":not(.jcarousel-item-placeholder)").length<=maxteasers){
				n.buttonPrev.remove();
				n.buttonNext.remove();
			}
			n.container.css("zoom","1");
		}
	});
	
	
	/* Akkordions */
	$('.accordion.show_prog_tbl .acc_header').append('<span class="toggle"><span class="show">Einblenden</span><span class="hide">Ausblenden</span></span>');
	$('#content .accordion').each(function(){
		$('.acc_header', this).toggle(
			function() {
				$(this).removeClass('closed').next().slideDown(300,'linear',function(){
					//updateIFrameHeight();
				});
				return false;
			},
			function() {
				$(this).addClass('closed').next().slideUp(300,'linear',function(){
					//updateIFrameHeight();
				});
				return false;
			}
		).addClass('closed').next().hide();
	});
	
	// Resizing of parent iFrame
	
	$('#page_wrap').bind('resize',function(){
		updateIFrameHeight();
	});
	
	
	
	/* Header Carousel	*/
	headerCarousel();
	
	
	/* Spenden-Header */
	spendenHeader();

	
	
	//updateIFrameHeight();

	counterPixelInit();
	
	
	
	// iFrame herumsetzen, falls nicht im iFrame aufgerufen
	if (top == self) {
	  top.location.href = 'http://www.nivea.at/NIVEA-erleben/ext/de-AT/familienfest?url='+encodeURIComponent(self.location.href);
	}
	
	
	
	 /* Lightbox Initialisation */

  $(".lightbox").mmcMediabox({
   infoArea: {
     fbLikebox: function($el) {
       var baseHref = $('head base[href]').attr('href');
       var shareHref = encodeURIComponent(baseHref + $el.attr('href'));
       //var shareHref = encodeURIComponent(location.href);
       /*
       if (top != self) {
         var topLocation = top.location.protocol + '://' + top.location.host + '/' + top.location.pathname;
         var urlSep = '?';
         if (top.location.search && top.location.search != '') {
           urlSep = '&';
         }
         shareHref = topLocation + urlSep + 'url=' + shareHref;
       }
       */
       log (shareHref);
       var fbLikeMarkup = '<iframe class="fbLikeIFrame" frameborder="0" scrolling="no" allowtransparency="true" width="270" height="25" style="width:270px;height:25px;border:0 none;" src="http://www.facebook.com/plugins/like.php?href='+shareHref+'&layout=button_count&show_faces=false&action=like&width=100"></iframe>';
       return fbLikeMarkup;
     }
   },
   onInfoAreaReady: function($infoArea) {
     $infoArea.find('.mediabox-download').click(function(){
        sendRequest(counterPixelURLs[0],"gallery/download.nopv");
        sendRequest(counterPixelURLs[1],"gallery/download.nopv");
        if (Omniture) { Omniture.trackDownloadLink($(this).attr('href')); }
     });
     $infoArea.find('.fbLikeIFrame').click(function(){
        sendRequest(counterPixelURLs[0],"gallery/facebook-like.nopv");
        sendRequest(counterPixelURLs[1],"gallery/facebook-like.nopv");    
     });
   },
   onAction: function(evt) {
     switch (evt) {
       case 'open':
         sendRequest(counterPixelURLs[0],"gallery/zoom.nopv");
         sendRequest(counterPixelURLs[1],"gallery/zoom.nopv");
         if (Omniture) { Omniture.trackNOPV('gallery-zoom'); }    
         break;
       case 'next':
         sendRequest(counterPixelURLs[0],"gallery/next.nopv");
         sendRequest(counterPixelURLs[1],"gallery/next.nopv");
         if (Omniture) { Omniture.trackNOPV('gallery-next'); }
         break;
       case 'prev':
         sendRequest(counterPixelURLs[0],"gallery/prev.nopv");
         sendRequest(counterPixelURLs[1],"gallery/prev.nopv");
         if (Omniture) { Omniture.trackNOPV('gallery-previous'); }
         break;
       case 'close':
         sendRequest(counterPixelURLs[0],"gallery/close.nopv");
         sendRequest(counterPixelURLs[1],"gallery/close.nopv");
         if (Omniture) { Omniture.trackNOPV('gallery-close'); }
         break;
       default:
         break;
     }
   }      
  });

/*
  $(".lightbox").lightbox( {
    navbarOnTop: 0,
    resizeSpeed: 300 ,
    overlayOpacity: 0.4,
    borderSize: 45,
    strings : { 
       prevLinkTitle: "vorheriges Bild",
       nextLinkTitle: "nächstes Bild",
       prevLinkText: "",
       nextLinkText: "",
       closeTitle: "schließen",
       image: "Bild",
       of: "von"
    }
  });

*/
	
});




function spendenHeader() {
	var spendenziel = $('#spendenziel').text().replace('.','').replace(',','.');
	var spendenstand = $('#spendenstand').text().replace('.','').replace(',','.');
	var factor = parseInt(spendenstand) / parseInt(spendenziel);
	var maxlength = 354;
	var speed = 2000;	
	var currlength = maxlength*factor;
	var currleft = currlength-44;
	$('.spendenstand .spendenstand_bar').animate({
		'width': currlength+'px'
		},
		speed
	);
	$('.spendenstand .akt_spendenstand').animate({
		'left': currleft+'px'
		},
		speed
	);
	

}


function headerCarousel() {
	
	var path_to_mediaelement = 'fileadmin/system/assets/js/mediaelement/'; 


	var videoPlayer = {};
	videoPlayer.init = function(video_url, $el) {
		var container_markup = '<div class="header_videoplayer_wrap"><video id="header_videoplayer" src="'+video_url+'" type="video/mp4"><!--<p>Bitte installieren Sie das <a href="http://get.adobe.com/de/flashplayer/" target="_blank">Flash Plugin</a> für Ihren Browser!</p>--></video></div>';
		var $container = $(container_markup).insertBefore($el);
		var width = $container.width();
		var height = $container.height();
		var $videoEl = $container.find('video');
		$videoEl.attr({'width': width, 'height': height});
		$el.children().filter(':not(.txt_wrap)').hide();
		
		
	  this.checkFullscreenInt = setInterval(function(){
  		if ($('.header_videoplayer_wrap .mejs-container').hasClass('mejs-container-fullscreen')) {
    		$('.header_carousel ul').css({
    		  'overflow': 'visible',
    		  'z-index': '900'
    		});
        $('#introduction-component').css({
          'z-index': '50'
        });
      } else {
        $('.header_carousel ul').css({
          'overflow': 'hidden',
          'z-index': '400'
        });
        $('#introduction-component').css({
          'z-index': '2'
        });        
      }
    }, 300);
		
		$el.addClass('inactive');
		
		$videoEl.mediaelementplayer({
			features: ['playpause','current','progress','duration', 'fullscreen'],
			plugins: ['flash', 'silverlight'],
			pluginPath: path_to_mediaelement,
			flashName: 'flashmediaelement.swf',
			silverlightName: 'silverlightmediaelement.xap',
			success: function (mediaElement, domObject) { 
				mediaElement.play();
			}
		});
		
		
		/*
		var flashvars = {
			file: video_url,
			controlbar: 'over',
			skin: path_to_flvplayer+'bekle.swf',
			autostart: true
		};
		var params = {
			allowFullscreen: true,
			allowScriptAccess: 'always',
			wmode: 'opaque',
			audio: 'false'
		};
		var attributes = {
			id: 'header_videoplayer',
			name: 'header_videoplayer'
		};
		
		swfobject.embedSWF(path_to_flvplayer+"player.swf", "header_videoplayer", "100%", "100%", "9.0.0",false, flashvars, params, attributes);
		*/
		
	}
	videoPlayer.destroy = function() {
		$header_carousel.find('.header_videoplayer_wrap').remove();
		$header_carousel.find('a.video').removeClass('inactive').children().show();
		if (this.checkFullscreenInt) {
		  clearInterval(this.checkFullscreenInt);
		}
	}	

	
	var $header_carousel = $('.header_carousel ul');
	if ($header_carousel.find('li').length > 1) {
		var $prev = $('<div class="cycle_prev"></div>').insertAfter($header_carousel);
		var $next = $('<div class="cycle_next"></div>').insertAfter($prev);
		
		$header_carousel.cycle({
			timeout: 0,
			speed: 1000,
			fx: 'scrollHorz',
			after: onAfter,
			before: onBefore,
			next: $next,
			prev: $prev, 
			cleartypeNoBg: true
		});
	}

	function onAfter(currSlideElement, nextSlideElement, options, forwardFlag){
		$header_carousel.find('li .txt_wrap').fadeIn();
		if ($(currSlideElement).find('a').hasClass('video')) {
			videoPlayer.destroy();
		}		
	}
	function onBefore(currSlideElement, nextSlideElement, options, forwardFlag){
		$(currSlideElement).find('.txt_wrap').fadeOut();
		$(nextSlideElement).find('.txt_wrap').hide();
	}

	$header_carousel.find('li').each(function(){
		var $aTag = $(this).find('a');
		if ($aTag.hasClass('video')) {
      $aTag.find('.txt_wrap').click(function(e){
        e.stopPropagation();
        location.href = $aTag.attr('href');
      });			
			$aTag.click(function(e){
				e.preventDefault();
				if (!$(this).hasClass('inactive')) { 
					var video_url = $aTag.attr('data-video');
					videoPlayer.init(video_url, $aTag);
				}
				return false;
			});
		}
	});
	
	
	
}





function updateIFrameHeight(){
	
	var parentIFrame;

	try {
		var parentIFrameContainer = window.parent.document.getElementById('container');
		parentIFrame = parentIFrameContainer.getElementsByTagName('iframe')[0];
		parent.window.resizeIframe(parentIFrame);
	} catch(err) {
		log(err);
	}
	
}


/* counterpixel request */
function sendRequest(url,button)
{
	/*jQuery.ajax({
	    type: "GET",
	    url: url+button,
	    success: function(content){
			return content;
		}
	});*/
	var timestamp = Number(new Date());
	//$("body").append("<IMG SRC='"+ url + button +"&tstamp="+ timestamp +"' HEIGHT=0 WIDTH=0>");
	
	$("body").append('<iframe src="'+ url + button +'" height="0" width="0" frameborder="0" style="border:0;height:0;width:0"></iframe>');
}

var counterPixelURLs = [];



function counterPixelInit() {


	 function getSitePath() 
	 { 
	     var path=window.location.pathname;
	     
		 var path_temp = cleanSitePath(path);
		 path = path_temp;
		 
		 /*
		 var counterpixelpath="";
	     
	     if (counterpixelpath.length>0) {
	     	if (counterpixelpath[0] != '/') counterpixelpath = '/'+counterpixelpath;
	     	return counterpixelpath;
	     }
	     */
	     
	     if (path.indexOf("?")) {
	     	var parts=path.split("?");
	     	path=parts[0];
	     }
		 
		 
	     
	     return path;
	 }
	 
	 function cleanSitePath(path) {
	      // entfernen von etwaigen doppelslashes
	     var path_temp = path.replace(/\/\//i, "/");
	     path = path_temp;
	
	     // entfernen von etwaigen no_cache
	     var path_temp = path.replace(/\/no_cache\//i, "/");
	     path = path_temp;
	     
	     return path;
	 }	 
	 
	 

	var insertMe = getSitePath();
	
	insertMe = insertMe.replace(/(\.html|\.htm)/g, '');

	var url = "http://counterpixel.nivea.at/cgi-bin/pageimp.exe?/nivea/at/ext/local"+insertMe+"/";
	var url2 = "http://counterpixel1.nivea.at/cgi-bin/pageimp.exe?/nivea/at/ext/local"+insertMe+"/";
	
  counterPixelURLs = [url, url2];
	
	
    // Omniture trackNOPV
    
    function trackNOPV(buttonName) {
        if (Omniture) {
           Omniture.trackNOPV(buttonName);
        }
    }
    
    // Omniture External Links
    
    $('a[target="_blank"]').click(function(){
        if (Omniture) {
           Omniture.trackExternalLink($(this).attr('href'));
        }      
    });     
	
	
	$("#sidebar .teaser .jcarousel-prev").click(function(){
		sendRequest(url,"sidebar-carousel/previous.nopv");
		sendRequest(url2,"sidebar-carousel/previous.nopv");
		trackNOPV('sidebar-carousel-previous');
	});
	$("#sidebar .teaser .jcarousel-next").click(function(){
		sendRequest(url,"sidebar-carousel/next.nopv");
		sendRequest(url2,"sidebar-carousel/next.nopv");
		trackNOPV('sidebar-carousel-next');
	});	
	$("#sidebar .teaser .pagination").click(function(){
		sendRequest(url,"sidebar-carousel/pagination.nopv");
		sendRequest(url2,"sidebar-carousel/pagination.nopv");
		trackNOPV('sidebar-carousel-pagination');
	});		
	
	
	$("#small-teasers .jcarousel-prev").click(function(){
		sendRequest(url,"big-carousel/previous.nopv");
		sendRequest(url2,"big-carousel/previous.nopv");
		trackNOPV('big-carousel-previous');
	});
	$("#small-teasers .jcarousel-next").click(function(){
		sendRequest(url,"big-carousel/next.nopv");
		sendRequest(url2,"big-carousel/next.nopv");
		trackNOPV('big-carousel-next');
	});		
	$("#small-teasers .pagination").click(function(){
		sendRequest(url,"sidebar-carousel/pagination.nopv");
		sendRequest(url2,"sidebar-carousel/pagination.nopv");
		trackNOPV('big-carousel-pagination');
	});	
	
	
	$(".product_carousel .prev").click(function(){
		sendRequest(url,"product-carousel/previous.nopv");
		sendRequest(url2,"product-carousel/previous.nopv");
		trackNOPV('product-carousel-previous');
	});
	$(".product_carousel .prev").click(function(){
		sendRequest(url,"product-carousel/next.nopv");
		sendRequest(url2,"product-carousel/next.nopv");
		trackNOPV('product-carousel-next');
	});			
	$(".product_carousel .pagination a").click(function(){
		sendRequest(url,"product-carousel/pagination.nopv");
		sendRequest(url2,"product-carousel/pagination.nopv");
		trackNOPV('product-carousel-pagination');
	});
	

	$(".header_visual_pagination a").click(function(){
		sendRequest(url,"header-slideshow/pagination.nopv");
		sendRequest(url2,"header-slideshow/pagination.nopv");
		trackNOPV('header-slideshow-pagination');	
	});
	

	$(".product-category .pagination ol li a").click(function(){
		var i = $(this).parent().index()+1;
		sendRequest(url,"product-category/pagination-page-"+i+".nopv");
		sendRequest(url2,"product-category/pagination-page-"+i+".nopv");
		trackNOPV('product-category-pagination_page-'+i);
	});
	$(".product-category .pagination .prev").click(function(){
		sendRequest(url,"product-category/prev.nopv");
		sendRequest(url2,"product-category/prev.nopv");	
		trackNOPV('product-category-previous');
	});	
	$(".product-category .pagination .next").click(function(){
		sendRequest(url,"product-category/next.nopv");
		sendRequest(url2,"product-category/next.nopv");
		trackNOPV('product-category-next');
	});

	
	$('.accordion .acc_header').toggle(
		function(){
			var headerTxt = $(this).find('h2').text().replace(/[^a-zA-Z0-9]+/g,"");
			sendRequest(url,"accordion/"+headerTxt+"/open.nopv");
			sendRequest(url2,"accordion/"+headerTxt+"/open.nopv");
			trackNOPV('accordion-'+headerTxt+'-open');
		},
		function(){
			var headerTxt = $(this).find('h2').text().replace(/[^a-zA-Z0-9]+/g,"");
			sendRequest(url,"accordion/"+headerTxt+"/close.nopv");
			sendRequest(url2,"accordion/"+headerTxt+"/close.nopv");	
			trackNOPV('accordion-'+headerTxt+'-close');			
		}
	);
  /*	
	$(".galerie a.lightbox").click(function(){
		sendRequest(url,"gallery/zoom.nopv");
	});

	$("a#nextLinkText").click(function(){
		sendRequest(url,"gallery/next.nopv");
	});
	$("a#prevLinkText").click(function(){
		sendRequest(url,"gallery/prev.nopv");
	});
	$("a#bottomNavClose").click(function(){
		sendRequest(url,"gallery/close.nopv");
	});
	*/
	
	
	

	
	
	
}





