
var slideWidth = 880;
var slideId = 0;
var url = "http://graph.facebook.com/kinghki/feed?limit=2&callback=?";
var video = '';
var timer;
var timeout = 5000;	// slide change interval
$(document).ready(function(){
    // close popup
    $('#popup').click(function(){
        $(this).hide();
    });	
    // facebook recommend pop
    $('.fbRecommendBtn').mouseover(function (event){
        $(this).css('color', '#ADADAD');
        var id = $(this).attr("id");
        var pos = $(this).position();
        var tPosX = pos.left  -13;
        var tPosY = pos.top -55;
        $('#fbPopup_'+id).css({top: tPosY, left: tPosX});
        $('#fbPopup_'+id).show();
    });
    $('.fbPopup').hover(function (){
        $(this).show();
    },function (){
        $('.fbRecommendBtn').css('color', '#5C5C5C');
        $(this).hide();
    });
	
	// Facebook feed reader
	var html = '';
	/*$.getJSON(
		url,
		function(json) {
			$.each(json.data, function(i, fb){
				var d1 = Date.parse(fb.created_time);
				if (!d1.is().monday()) d1.last().monday();

				html += '<div class="feedItem">';
				html += '<div class="boxH"><span class="socialSource">Facebook | '+ d1.toString('dd.MM.yyyy') +'</span></div>';
				
				
				html += '<div><p>'+ fb.message +'</p></div>';
				
				
				if (fb.type == 'link' || fb.type == 'photo' || fb.type == 'video') {
					html += '<div class="linkData">';
					//html += '<img src="'+ fb.picture +'" alt="" />';
					html += '<div class="floater">';
					
					if (fb.name) html += '<div class="fbName"><a href="'+ fb.link +'">'+ fb.name +'</a></div>';
					if (fb.caption) html += '<div class="fbName"><a href="http://'+ fb.caption +'">'+ fb.caption +'</a></div>';
					//if (fb.description) html += '<div class="fbName">"'+fb.description +'"</div>';
					//html += '<span class="fbName">'+fb.name +'</span>';
					//html += '<span class="fbName">'+fb.name +'</span>';
					html += '</div></div>';
				}
				
				html += "</div></div>\n";
				$('#facebookFeed').html(html);
			});
		}
	);*/

	// slide control
	$('#prev').click(function(){
        changeSlide(slideId);
    });
    $('#next').click(function(){
        changeSlide(slideId+2);
    });
	//initTimer();
	/*$('.pageContainer a, .nostoContainer, .pages a').hover(function(){
		clearTimeout(timer);
	}, function(){
		timer=setTimeout("doTimer()", timeout);	// 5 seconds
	});*/
    
    //dropDown
    $('.dropDown').click(function(){
        $('.layer').slideUp('fast');
        $('.dropDown').css('z-index', 1);
        $(this).children('.layer').stop().slideToggle('fast');
        $(this).css('z-index', 10000);
    });
    
    $('.linkHolder').hover(function (){
        
        //$('.subLink').slideDown('fast');
    });
    
    //person additional info
    $('.person').click(function(){
        if ($(this).css('height') != 'auto'){
            //calculate auto height
            var tmpH1 = $(this).height();
            $(this).css('height', 'auto');
            var tmpH2 = $(this).height();
            $(this).css('height', tmpH1+'px');
            //animate
            $(this).animate({
                height: tmpH2+'px'
            }, 200, function() {
                //when done animating, put the height back to auto
                $(this).css('height', 'auto');
            });
			$(this).children('.basic').children('.arrow').children()
				.removeClass('arrowImage')
				.addClass('arrowImageActive');
        }
        else{
            //hide additional info
            $(this).animate({
                height: '160px'
            }, 200);
			$(this).children('.basic').children('.arrow').children()
				.addClass('arrowImage')
				.removeClass('arrowImageActive');
        }
    });
    
    // comment info
    /*$('#showComments').click(function(event){
        event.preventDefault();
        if ($('#blogPostCommentForm').css('height') != 'auto'){
            //calculate auto height
            var tmpH1 = $('#blogPostCommentForm').height();
            $(this).css('height', 'auto');
            var tmpH2 = $('#blogPostCommentForm').height();
            $('#blogPostCommentForm').css('height', tmpH1+'px');
            //animate
            $('#blogPostCommentForm').animate({
                height: tmpH2+'px'
            }, 200, function() {
                //when done animating, put the height back to auto
                $('#blogPostCommentForm').css('height', 'auto');
            });
			$('#blogPostCommentForm').children('.basic').children('.arrow').children()
				.removeClass('arrowImage')
				.addClass('arrowImageActive');
        }
        else{
            //hide additional info
            $('#blogPostCommentForm').animate({
                height: '1px'
            }, 200);
			$('#blogPostCommentForm').children('.basic').children('.arrow').children()
				.addClass('arrowImage')
				.removeClass('arrowImageActive');
        }
    });*/
});

function initTimer(){
	timer=setTimeout("doTimer()", timeout);
}
function doTimer(){	
	var id = slideId+2;
	var max = $('.slide').size();
	if (id > max) id = 1;
	changeSlide(id);
	timer=setTimeout("doTimer()", timeout);
}
function changeSlide(id){
    if (id == '1') $('.video:first-child div').css('display', 'block');
    else $('.video:first-child div').css('display', 'none');
	id--;
    //make sure the id is within the allowed numbers
    var max = $('.slide').size()-1;
    if (id < 0)id=0;
    if (id > max) id = max;
    slideId = id;
    
	var textId = id+1;
	$('.slideText2').each(function(){
        $(this).hide();
    });
    $('#slideText_'+textId).show();
	
    //animate
    $('.slides').animate({
        left: -1*id*slideWidth
    }, 1000);

    //change the underlined number
    $('.pages > a').removeClass('selected');
    $('.pages > a').each(function(){
        if ($(this).html() == id+1) $(this).addClass('selected');
    });
    
    // video reset
    $('.video:first-child').css('background', '#000');
    $('.video:first-child').css('height', '500px');
    
    
}


/* ------- KING OMAT -------- */

function emailaddr(id)
{
	var bits=id.split("@");
	var name=bits[0];
	document.write(name+'&#64;king.fi');
}

