if(window.loadFirebugConsole) {
	window.loadFirebugConsole();
}

var playlistObject = false;

var globalJcarouselObj = new Array();
var globalTimer = new Array();
var globalCarouselType = new Array();
var globalItem = new Array();

$(document).ready(function(){
	fixMacintosh();
	
	clearSearch.init();
	navFooter.init();
	jcHighlight.init();
	jcHighlight.run();
	sidebar.init();
	selectSectionHighLight();
	initCategoryRadios();
	
	$.ie6CSSHover();
	
	if( $('body').hasClass('start') ){
		var playlist = new Ccarousel();
	    playlist.init('Playlist', playlist, 'playlist-com');
	}
});

function fixMacintosh(){
	var thisBrowser = jQuery.browser;
	
	if( (navigator.appVersion.indexOf("Mac") != -1) || (navigator.appVersion.indexOf("PowerPC") != -1)){
		if(thisBrowser.mozilla == true){
			$('#Navigation').find('li a').css('padding', '12px 30px 0 30px');
			$('#Navigation').find('li.last a').css('padding', '12px 32px 0 31px');
		}
	}
}

function ArchivRequest(conf) {
	var conf = conf;
	this.load = function (obj) {
		startAjaxLoading(conf.resultContainer);
		$(conf.form).hide();
		$.ajax({
			  url: (conf.url() ? conf.url() : obj.href),
			  type: "POST",
			  cache: true,
			  data: (conf.data() ? conf.data() : ''),
			  dataType: "html",
			  success: function(result){
				 if(conf.resultContainer) {
					 $(conf.form).show();
					$(conf.resultContainer).html(result);
				 }
			  },
			  error:  function() {
				$(conf.form).show();
			  }
		});
		return false;
	}
}

function startAjaxLoading(container){
	var loaderHTML = '<div class="loader"><img src="/com/images/icon/loader.gif" alt="loading..." /></div>'
	$(container).empty().html(loaderHTML);
}

function initCategoryRadios () {
	$('#cathegory label').bind("click", function(e){																				 
		if($(this).hasClass('active')) {
			return false;		
		}
	});
}

//----------------------------------------------------------------------------------


var clearSearch = {
	init: function () {
	
		var defaultValue;
	
		$('#Search').find('input').focus(function(){
			defaultValue = $(this).val(); 
			$(this).val('');
		});

		$('#Search').find('input').blur(function(){
		  var val = $(this).val();
		  if(val === '')
			  $(this).val(defaultValue);
		});
	}
}


//----------------------------------------------------------------------------------

var sidebar = {
	init: function() {
	  $(".cElement").find("a.toggle").click(function () {

			if ( $(this).hasClass('active') ) {
				$(this).next().slideUp("slow");
				$(this).removeClass("active");
				return false;
			}else{
				$(this).next().slideDown("slow");
				$(this).addClass("active");
				return false;
			}

		});
	}
}


//----------------------------------------------------------------------------------


var navFooter = {
	init: function() {
		$("#Footer").find("li.toggle").unbind('click');
		$("#Footer").find("li.toggle").bind('click', function (e) {
			var index = $("li.toggle").index(this);
			$("li.toggle").removeClass("open");
			$("li.toggle").eq(index).addClass("open");
			$(".footerLayer").hide();
			$(".footerLayer").eq(index).show();

			navFooter.closeLayer();
		});

		$("#Footer").find("div.selectBottom").toggle(function() {
			var boxwert = $(this).next().attr('id');
			$(this).addClass('selectBottomSelected');
			$(this).next().find('input').trigger('click');
			},function(){
			$(this).removeClass('selectBottomSelected');
			$(this).next().find('input').trigger('click');
		});
	},
	closeLayer: function() {
		$("#Footer").find("li.open").unbind('click');
		$("#Footer").find("li.open").bind('click', function (e) {
			$(this).removeClass("open");
			$(".footerLayer").hide();
			navFooter.init();
		});
	}
}

//----------------------------------------------------------------------------------

function gfxSelectBox() {

	var gfxSelect = false;
	var optionList = false;

	this.Init = function(id)  {
		gfxSelect = $(id);
		optionList = gfxSelect.find('div.holder');

		// JavaScript on/off
		gfxSelect.find('.select').css('display', 'none');
		gfxSelect.find('.gfxSelectBox').css('display', 'block');

		// options auslesen
		option = '';
		gfxSelect.find('option').each(function (i) {
			option += '<li><a href="' + $(this).attr('value') + '" title="' + $(this).attr('value') + '">' + $(this).text() + '</a></li>';
		});
		option = '<ul>' + option + '</ul>';

		// onclick auf Links in der Liste
		gfxSelect.find('div.holder').find('.scroll-pane').html(option).find('a').click(function (event) {
            $('#languageKey').find('option[value=' + event.target.title + ']').attr('selected', 'selected');
            toPage(event);
		});

		// onclick auf DropDown
		gfxSelect.find('div.gfxSelectBox').bind('click', function(event) {
			if($(event.target).parent('div.gfxSelectBox')) {
				if($(event.target).is('div.jScrollPaneDrag')) {
                    return false;
				}
				displayList(event);
				return false;
			}
		});

		// onclick auf Body
		$('body').bind('click', function(event) {
			$(event.target).parents().each(function(i){
				if($(this).is('div.gfxSelectBox')) {
					return;
				}
			});
			bodyEvent(event);
		});
	}

	function toPage(event) {
		$('#languageSelect').click();//location.href = event.target.href;
	}

	function displayList (event) {
		if(optionList.css('visibility') == 'visible') {
			optionList.css('visibility', 'hidden');
		}
		else {
			optionList.css('visibility', 'visible');
		}
	}

	function bodyEvent (event) {
		optionList.css('visibility', 'hidden');
	}

}

//----------------------------------------------------------------------------------


function filterArchive() {
	$('#formSearchFilterArchive').find('.checkbox').find('a').addClass('active');

	$('#formSearchFilterArchive #cathegory').find('label').bind('click', function(e) {

		currentElementFor = $(e.target).attr('for');
		$(e.target).parent().find('label').each(function(i) {

			if(currentElementFor != $(this).attr('for')) {
				$(this).attr('class', 'inactive');
			}
			else {
				$(this).attr('class', 'active');
			}
		});
	})



	$('#formSearchFilterArchive').find('.checkbox').bind('click', function() {

		if( $(this).find('label').hasClass('active') ){
			$(this).find('label').removeClass("active");

			$('#formSearchFilterArchive').find('.radio').find('label').removeClass('active');
		}else{
			$(this).find('label').addClass("active");
		}
	});
}


//---------------------------------------------------------------------------------

var startPos = false;
function Ccarousel() {
 this.init = function( obj_id, obj, type ){
     startPos = 1;
		$('#Playlist .teaserList').find('.teaser').each( function () {
			myHTML = $(this).html();
			if( $(myHTML).hasClass('active') ){
				startPos = $(myHTML).attr('class');
				startPos = startPos.split(" ");
				startPos = parseInt(startPos[0].substr(14));
			}
		})

		if(type == 'playlist-de'){
			this.startVert(obj_id);
		}else{
			this.startHor(obj_id);
		}
 	};

 	this.startHor = function(obj_id){
 		var scroll = false;

 		obj_id == 'Playlist' ? scroll = 4 : scroll = 1;

 		$('#' + obj_id).find('.mycarousel').jcarousel({
				start: startPos,
				scroll: scroll,
				animation: 700,
				vertical: false,
				wrap: 'circular',
				itemVisibleInCallback: {onBeforeAnimation: itemVisibleInCallbackHor},
				itemVisibleOutCallback: {onAfterAnimation: itemVisibleOutCallback}
		}, obj_id);
 	};

 	this.startVert = function(obj_id){
 		$('#' + obj_id).find('.mycarousel').jcarousel({
				start: 1,
				scroll: 1,
				animation: 700,
				vertical: true,
				wrap: null,
				itemVisibleInCallback: {onBeforeAnimation: itemVisibleInCallbackVert},
				itemVisibleOutCallback: {onAfterAnimation: itemVisibleOutCallback}
		}, obj_id);
 	};
}

itemVisibleInCallbackHor = function(carousel, item, i, state, evt){
	var idx = carousel.index(i, $('#' + carousel.obj_id).find('.teaserList').find('.teaser').length );
	carousel.add(i, getItemHTML(idx, carousel.obj_id, carousel) );

 	if( carousel.obj_id == 'Playlist'){
 		jcHighlight.init();
		checkItems(carousel);
		playlistObject = carousel;
	}
}

itemVisibleInCallbackVert = function(carousel, item, i, state, evt){
	var idx = carousel.index(i, $('#' + carousel.obj_id).find('.teaserList').find('.teaser').length );
	carousel.add(i, getItemHTML(idx, carousel.obj_id) );

 	if( carousel.obj_id == 'Carousel'){
		this.checkItems(carousel);
	}
}

itemVisibleOutCallback = function(carousel, item, i, state, evt){
  carousel.remove(i);
}

getItemHTML = function(item, obj_id){
	var total = $('#' + obj_id).find('.teaserList').find('.teaser').length;
	var pageCounter = "<span>" + item + " / " + total + "</span>";
	$('#' + obj_id).find('.pageCounter').html(pageCounter);

	teaser = $('#' + obj_id).find('.teaserList').find('.teaser').get(item-1);
	return $(teaser).html();
}

checkItems = function(carousel) {
	var myItems = $('#' + carousel.obj_id).find('.teaserList').find('.teaser').length;

	if(myItems < 5){
		var i = 1;

		$('.mycarousel .jcarousel-item').each( function() {
			if(i > myItems) {
				$(this).remove();
			}
			i++;
		})

		$('.jcarousel-prev').hide();
		$('.jcarousel-next').hide();
	}
}

//----------------------------------------------------------------------------------


var CarouselSlideShowTimer = false;

function CarouselSlideShow(obj){
	CarouselSlideShowTimer = setTimeout(function(){ CarouselSlideShow(obj) }, 1000);

	var firstImage = $(obj).find('img:first');
	var lastImage =  $(obj).find('img:last');

	$(lastImage).after(firstImage);
}

function CarouselStopSlideShow(){
	clearTimeout(CarouselSlideShowTimer);
}

var jcHighlight = {
	init: function () {
		$('.rightCol .mycarousel a').click(function(){
			location.href = this.href;
		});

		// Image
		$('.mycarousel li .image a').bind("click",function(){
			location.href = this.href;
		});

		// Headline
		$('.mycarousel').find('li .text a').click(function(){
			location.href = this.href;
		});


		//Slideshow
		var myHandler = $('.mycarousel li').find('span.listener');

		$(myHandler).unbind("mouseover").bind("mouseover",function(e){
			$(this).each( function() {
				if(e.target.tagName == 'SPAN'){
					CarouselSlideShow(e.target.parentNode);
				}
			})
		});

		$(myHandler).unbind("mouseout").bind("mouseout",function(e){
			$(this).each( function() {
				if(e.target.tagName == 'SPAN'){
					CarouselStopSlideShow();
				}
			})
		});
	},

	selectNew: function(videoId){
		$('.mycarousel').find('li').find('div').removeClass('active');
		$('.mycarousel li').find('.' + videoId).addClass('active');

		$('.videoCarousel').find('div').removeClass('active');
		$('.videoCarousel').find('.videoId_' + videoId).addClass('active');

		lastSelectedCarouselTeaser = videoId;
	},

	run: function () {
		$('.jcarousel-next').click(function(){
			jcHighlight.init();
		});

		$('.jcarousel-prev').click(function(){
			jcHighlight.init();
		});

	}
}

//----------------------------------------------------------------------------------


function bookmarkItem(inUrl, inTitle, inDescription , inBookmarkService) {

	inUrl = encodeURIComponent(inUrl);

	switch(inBookmarkService) {
		case 'digg':
			window.open('http://digg.com/remote-submit?phase=2&url='+inUrl+'&title='+inTitle+'&bodytext='+inDescription);
			break;
		case 'delicious':
			window.open('http://del.icio.us/post?&url='+inUrl+'&title='+inTitle+'&notes='+inDescription);
			break;
		case 'facebook':
			window.open('http://www.facebook.com/sharer.php?u='+inUrl+'&t='+inTitle);
			break;
		case 'mrwong':
			window.open('http://www.mister-wong.com/index.php?action=addurl&bm_url='+inUrl+'&bm_description='+inTitle);
			break;
		case 'twitter':
			window.open('http://twitter.com/home?status='+inUrl);
			break;
		case 'webnews':
			window.open('http://www.webnews.de/einstellen?url='+inUrl+'&title='+inTitle);
			break;
        case 'web_news':
			window.open('http://www.webnews.de/einstellen?url='+inUrl+'&title='+inTitle);
			break;
	}
	return false;
}



//----------------------------------------------------------------------------------------

function selectSectionHighLight() {
	var selectSection = $('.selectSection').find('li a');
	
	$(selectSection).unbind('click').bind('click', function(e) {
		$(selectSection).each( function() {
			$(this).removeClass('active');
		})
		$(this).addClass('active');
	});
}

//----------------------------------------------------------------------------------------

function trackViews(id){
	 $('#trackVideo').load('increaseViewCount.do?articleID=' + id);
}

function trackRating(id, rating){
	 $('#trackVideo').load('articleRating.do?articleID=' + id + '&rating');
}

