$(document).ready(function(){

//TWITTER-TWEET
$.getJSON('http://twitter.com/status/user_timeline/planakom.json?count=1&callback=?', function(data){
	$.each(data, function(index, item){
		$('#tweet').html(item.text.linkify());
	});
});

//create actual ankerlinks for twitter
String.prototype.linkify = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
		return m.link(m);
	});
};


//SOCIAl-ICONS-HOVER
$("#icon-twitter, #icon-facebook, #icon-rss").hover(
  function () {
    $(this).animate({"opacity" : "0.8"}, 100);
  }, function () {
    $(this).animate({"opacity" : "1"}, 100);
  }
);

//INSERT SHOWCASE-NAV BY NUMBER OF POSTS AVAILABLE
$('.showcase-text-slide').each(function(){
	if ($(this).index() == 0) {
		$('#showcase-numbers').append(('<div class="marked"><h1>'+String($(this).index() + 1)+'</h1></div>'));
	}
	else { 
		$('#showcase-numbers').append(('<div><h1>'+String($(this).index() + 1)+'</h1></div>')); 
	}
});


//INSERT PRINT-NAV BY NUMBER OF POSTS AVAILABLE
$('.print-text-slide').each(function(){
	if ($(this).index() == 0) {
		$('#print-numbers').append(('<div class="marked"><h1>'+String($(this).index() + 1)+'</h1></div>'));
	}
	else { 
		$('#print-numbers').append(('<div><h1>'+String($(this).index() + 1)+'</h1></div>')); 
	}
});


//INSERT NEWMEDIA-NAV BY NUMBER OF POSTS AVAILABLE
$('.newmedia-text-slide').each(function(){
	if ($(this).index() == 0) {
		$('#newmedia-numbers').append(('<div class="marked"><h1>'+String($(this).index() + 1)+'</h1></div>'));
	}
	else { 
		$('#newmedia-numbers').append(('<div><h1>'+String($(this).index() + 1)+'</h1></div>')); 
	}
});


//INSERT BRANDING-NAV BY NUMBER OF POSTS AVAILABLE
$('.branding-text-slide').each(function(){
	if ($(this).index() == 0) {
		$('#branding-numbers').append(('<div class="marked"><h1>'+String($(this).index() + 1)+'</h1></div>'));
	}
	else { 
		$('#branding-numbers').append(('<div><h1>'+String($(this).index() + 1)+'</h1></div>')); 
	}
});


//SHOWCASE-NAV
var showcaseSelectorIndex = '0';
var showcaseNumberOfPosts = $('#showcase-text-carousel').children().size();
$('#showcase-image-carousel').css({"width" : ((showcaseNumberOfPosts*620) + "px")});
$('#showcase-text-carousel').css({"width" : ((showcaseNumberOfPosts*270) + "px")});

$('#showcase-numbers div').click(function() {
	$('#showcase-numbers').children('[class=marked]').removeClass('marked');
	selectorIndex = $(this).index();
	$(this).attr('class', 'marked');
	
	$('#showcase-image-carousel').animate({opacity:0},200, function() {
		$(this).css({marginLeft : "-" + (selectorIndex*620) + "px" });
		$(this).animate({opacity:1},200);
	});
	
	$('#showcase-text-carousel').animate({opacity:0},200, function() {
		$(this).css({marginLeft : "-" + (selectorIndex*270) + "px" });
		$(this).animate({opacity:1},200);
	});

});


//PRINT-NAV
var printSelectorIndex = '0';
var printNumberOfPosts = $('#print-text-carousel').children().size();
$('#print-image-carousel').css({"width" : ((printNumberOfPosts*620) + "px")});
$('#print-text-carousel').css({"width" : ((printNumberOfPosts*270) + "px")});

$('#print-numbers div').click(function() {
	$('#print-numbers').children('[class=marked]').removeClass('marked');
	selectorIndex = $(this).index();
	$(this).attr('class', 'marked');
	
	$('#print-image-carousel').animate({opacity:0},200, function() {
		$(this).css({marginLeft : "-" + (selectorIndex*620) + "px" });
		$(this).animate({opacity:1},200);
	});
	
	$('#print-text-carousel').animate({opacity:0},200, function() {
		$(this).css({marginLeft : "-" + (selectorIndex*270) + "px" });
		$(this).animate({opacity:1},200);
	});

});


//NEW-MEDIA-NAV
var mediaSelectorIndex = '0';
var mediaNumberOfPosts = $('#newmedia-text-carousel').children().size();
$('#newmedia-image-carousel').css({"width" : ((mediaNumberOfPosts*620) + "px")});
$('#newmedia-text-carousel').css({"width" : ((mediaNumberOfPosts*270) + "px")});

$('#newmedia-numbers div').click(function() {
	$('#newmedia-numbers').children('[class=marked]').removeClass('marked');
	selectorIndex = $(this).index();
	$(this).attr('class', 'marked');
	
	$('#newmedia-image-carousel').animate({opacity:0},200, function() {
		$(this).css({marginLeft : "-" + (selectorIndex*620) + "px" });
		$(this).animate({opacity:1},200);
	});
	
	$('#newmedia-text-carousel').animate({opacity:0},200, function() {
		$(this).css({marginLeft : "-" + (selectorIndex*270) + "px" });
		$(this).animate({opacity:1},200);
	});

});


//BRANDING-NAV
var brandingSelectorIndex = '0';
var brandingNumberOfPosts = $('#branding-text-carousel').children().size();
$('#branding-image-carousel').css({"width" : ((brandingNumberOfPosts*620) + "px")});
$('#branding-text-carousel').css({"width" : ((brandingNumberOfPosts*270) + "px")});

$('#branding-numbers div').click(function() {
	$('#branding-numbers').children('[class=marked]').removeClass('marked');
	selectorIndex = $(this).index();
	$(this).attr('class', 'marked');
	
	$('#branding-image-carousel').animate({opacity:0},200, function() {
		$(this).css({marginLeft : "-" + (selectorIndex*620) + "px" });
		$(this).animate({opacity:1},200);
	});
	
	$('#branding-text-carousel').animate({opacity:0},200, function() {
		$(this).css({marginLeft : "-" + (selectorIndex*270) + "px" });
		$(this).animate({opacity:1},200);
	});

});


//NEWS NAV
var newsNumberOfPosts = $('#news-text-carousel').children().size();
$('#news-text-carousel').css({"width" : ((newsNumberOfPosts*320) + "px")});
$('#news-image-carousel').css({"width" : ((newsNumberOfPosts*380) + "px")});

var newsTextSlideValue = 0;
$('#newer').css({"opacity" : "0.5"});

$('#older').click(function() {
	if (newsTextSlideValue != ((newsNumberOfPosts-1)*320)) {
		newsTextSlideValue += 320;
		newsSlideButtonsOpacity();		
		
		$('#news-text-carousel').animate({opacity:0},200, function() {
			$(this).css({marginLeft : "-" + newsTextSlideValue + "px" });
			$(this).animate({opacity:1},200);
		});
		
		$('#news-image-carousel').animate({opacity:0},200, function() {
			$(this).css({marginLeft : "-" + ((newsTextSlideValue/320)*380) + "px" });
			$(this).animate({opacity:1},200);
		});
	}
});

$('#newer').click(function() {
	if (newsTextSlideValue >= 320) {
		newsTextSlideValue -= 320;
		newsSlideButtonsOpacity();		
		
		$('#news-text-carousel').animate({opacity:0},200, function() {
			$(this).css({marginLeft : "-" + newsTextSlideValue + "px" });
			$(this).animate({opacity:1},200);
		});
		
		$('#news-image-carousel').animate({opacity:0},200, function() {
			$(this).css({marginLeft : "-" + ((newsTextSlideValue/320)*380) + "px" });
			$(this).animate({opacity:1},200);
		});
	}
});

//CHANGE OPACITY OF NEWS-NAV-BUTTONS
function newsSlideButtonsOpacity() {
	if (newsTextSlideValue >= (newsNumberOfPosts-1)*320){
		$('#older').animate({"opacity" : "0.5"});
	}
	if (newsTextSlideValue < (newsNumberOfPosts-1)*320) {
		$('#older').animate({"opacity" : "1"});
	}
	if (newsTextSlideValue == 0) {
		$('#newer').animate({"opacity" : "0.5"});
	}
	if (newsTextSlideValue != 0) {
		$('#newer').animate({"opacity" : "1"});
	}
}

//NEWS NAV-HOVER
$("#older").mouseenter( function () {
	if (newsTextSlideValue < (newsNumberOfPosts-1)*320) {
    	$(this).css({"background-position" : "0px -21px", "cursor" : "pointer"});
    }
});
$("#older").mouseleave( function () {
	$(this).css({"background-position" : "0px 0px", "cursor" : "default"});
});

$("#newer").mouseenter( function () {
	if (newsTextSlideValue != 0) {
    	$(this).css({"background-position" : "0px -21px", "cursor" : "pointer"});
    }
});
$("#newer").mouseleave( function () {
	$(this).css({"background-position" : "0px 0px", "cursor" : "default"});
});


//REPAIR THIS NEWS-TITLE-POSITION-FUNCTION
$('.news-text-slide').each(function(){
	if ($(this).children('.news-title').children('h2').height() > 18) {
		$(this).children('.news-title').children('h2').css({"padding-top" : "5px"});
	}	
});

});

