$(document).ready(function(){	
	set_cufon_iannnnn('.cufon-iannnnn');
	set_cufon_impact('.cufon-impact');
	set_cufon_verdana('.cufon-verdana');
	
	set_cufon_impact('#wrapper #container #page-nav a, #wrapper #container #page-nav span, #wrapper #container #page-nav strong');
	
	//start_animation_logo_picture();
	//start_animation_logo();
	$('#header .logo').mouseover(function(){
		//$('#header .logo').html('<span class="text"><img src="images/logo-text.png" /></span>');
		$('#header .logo .picture').hide();
		$('#header .logo .text').show();
	});
	
	$('#header .logo').mouseout(function(){
		//$('#header .logo').html('');
		$('#header .logo .picture').show();
		$('#header .logo .text').hide();
	});
	
	$('.top-menu .menu .channels, .top-menu .submenu').mouseover(function(){
		$('.top-menu .submenu').show();
	}).mouseout(function(){
		if( $('.top-menu .menu .channels a').attr('class') != 'current' ){
			$('.top-menu .submenu').hide();
		}
	});
	
	/** ตัวอย่างเอาไว้ทำ slide
	if( $('#slides').length > 0 ){
		$('#slides').slides({
			preload: true,
			preloadImage: 'images/loading.gif',
			play: 5000,
			pause: 2500,
			hoverPause: true
		});
	}
	*/
	
	$('.bottom-tools .sound a').click(function(){
		$('.bottom-tools .sound a').removeClass('current');
		set_cufon_impact('.bottom-tools .sound a');
		$(this).addClass('current');	
	});
	
	setInterval('updateClock()', 1000);
	
	$("a.play").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 980,
			'height'		: 850,
			'href'			: this.href,
			'type'			: 'iframe'
		});
	
		return false;
	});
	
});

function set_cufon_iannnnn(element){
	Cufon.replace(element, {fontFamily: '2006_iannnnnISO', hover: true, hoverables: {a: true}});
}

function set_cufon_impact(element){
	Cufon.replace(element, {fontFamily: 'Impact', hover: true, hoverables: {a: true}});
}

function set_cufon_verdana(element){
	Cufon.replace(element, {fontFamily: 'Verdana', hover: true, hoverables: {a: true}});
}

function start_animation_logo_picture() { $('#header .logo').html('<span class="picture"><img src="images/logo-picture.png" /></span>'); }//attr('src', 'images/logo-picture.png'); }
function start_animation_logo_text() { $('#header .logo').html('<span class="text"><img src="images/logo-text.png" /></span>'); }//attr('src', 'images/logo-text.png'); }

function start_animation_logo() {
    setTimeout(start_animation_logo_picture, 1000);
    setTimeout(start_animation_logo_text, 2000);
    setTimeout(start_animation_logo, 2000);
}

function get_youtube_img( code, size ){
  	size = (size === null) ? "big" : size;
 	if(size == "small"){
    	return "http://img.youtube.com/vi/"+code+"/2.jpg";
  	}
  	else {
    	return "http://img.youtube.com/vi/"+code+"/0.jpg";
  	}
}

function goToByScroll(id){
	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}

function updateClock ( )
    {
    var currentTime 		= new Date();
    var currentDay 			= currentTime.getDate();
    var currentMonth 		= currentTime.getMonth()+1;
    var currentYear 		= currentTime.getYear();
    var currentHours 		= currentTime.getHours();
    var currentMinutes 	= currentTime.getMinutes();
    var currentSeconds 	= currentTime.getSeconds();
 
    currentDay = ( currentDay < 10 ? "0" : "" ) + currentDay;
    currentMonth = ( currentMonth < 10 ? "0" : "" ) + currentMonth;
	if(currentYear < 1000){
  		currentYear+=1900
	}
 
    // Pad the minutes and seconds with leading zeros, if required
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
 
    // Choose either "AM" or "PM" as appropriate
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
 
    // Convert the hours component to 12-hour format if needed
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
 
    // Convert an hours component of "0" to "12"
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;
    currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
 
    // Compose the string for display
    var currentTimeString = '';//"TIME <strong>"+currentHours + ":" + currentMinutes + ":" + currentSeconds + "</strong> " + timeOfDay;// + ":" + currentSeconds + " " + timeOfDay;
	
	currentTimeString += '<div class="cufon-impact date">'+currentDay+'.'+currentMonth+'.'+currentYear+'</div>';
	currentTimeString += '<div class="cufon-impact time"><span>'+timeOfDay+'.</span><span class="current-time">'+currentHours+':'+currentMinutes+':'+currentSeconds+'</span><span>TIME</span></div>';
	currentTimeString += '<br />';
	
    $("#clock").html(currentTimeString);
	Cufon.replace("#clock div", {fontFamily: 'Impact', hover: true, hoverables: {a: true}});
 
 }

function getYouTubeListInfo(loop, v, element, pic_size, view_font) {
	$(element+' #picture-'+(loop+1)).html('<img src="'+get_youtube_img(v, 'big')+'" width="'+pic_size+'">');
	$.ajax({
		url: "http://gdata.youtube.com/feeds/api/videos/"+v+"?v=2&alt=json",
		dataType: "jsonp",
		success: function (data){			
			$(element+' #link-'+(loop+1)).attr('href', 'watch.php?v='+v+'&channel='+$(element+' #link-'+(loop+1)).attr('rel'));
			$(element+' #title-'+(loop+1)).html(data.entry.title.$t);
			$(element+' #views-'+(loop+1)).html(formatNumber(data.entry.yt$statistics.viewCount));
			set_cufon_iannnnn(element+' #title-'+(loop+1));
			if( view_font == 'iannnnn' ){
				set_cufon_iannnnn(element+' #views-'+(loop+1));
			}
			else if( view_font == 'impact' ){
				set_cufon_impact(element+' #views-'+(loop+1));				
			}
			else if( view_font == 'verdana' ){
				set_cufon_verdana(element+' #views-'+(loop+1));				
			}
		}
	});
}

 function formatNumber(n) {
    if (!isFinite(n)) {
        return n;
    }

    var s = ""+n, abs = Math.abs(n), _, i;

    if (abs >= 1000) {
        _  = (""+abs).split(/\./);
        i  = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
               _[0].slice(i).replace(/(\d{3})/g,',$1');

        s = _.join('.');
    }

    return s;
}
