// JavaScript Document
var cat='-';
$(document).ready(function(){
    jQuery("#mycarousel").jcarousel({
		orientation: "vertical",
        itemVisible: 1,
        itemScroll: 1,
        autoScroll: 5,
        wrap: true
    });
	
	jQuery("#mycarousel2").jcarousel({
        orientation: "vertical",
        itemVisible: 1,
        itemScroll: 1,
        autoScroll: 10,
        wrap: true
    });
    
    //bs_menu();
    $('.hand').css('cursor', 'hand');
    //popup_balloon();
    
    //auto_play_music();
    
    //TB_init();
    
    //alert(type);
    
    $('#loading_new').hide();
    
    // Select Page
    $('#link_next').click(function(){
        change_cat(typeid, cat, 2);
    });
    $('#link_last').click(function(){
        change_cat(typeid, cat, num_pages);
    });
    for(i=1;i<=10;i++){
        $('#link'+i).click(function(){
            change_cat(typeid, cat, $(this).html());
        });
    }
    
    // Select Page Search
    $('#search_next').click(function(){
        change_search_cat(search_con, search_cat, 2);
    });
    $('#search_last').click(function(){
        change_search_cat(search_con, search_cat, num_pages);
    });
    for(i=1;i<=10;i++){
        $('#search'+i).click(function(){
            change_search_cat(search_con, search_cat, $(this).html());
        });
    }
	
	search_button();
    
});


function popup_balloon()
{
    var old_search = '-';
    var search_song = false,
        search_picture = false,
        search_theme = false,
        search_game = false,
        search_chickywalky = false,
        search_movieclip = false,
        search_vdoringtone = false;
    
    $('#bs_menu1').mouseover(function() {
        if(old_search != '#balloon_song'){
            $(old_search).fadeOut("fast");
            if(!search_song)
                $('#balloon_song').fadeIn("fast");
        }
        old_search = '#balloon_song';
        search_song=true;
        
    });
    $('#bs_menu1').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_song){
                $('#balloon_song').fadeOut("fast");
                old_search = null;
            }
        }, 150);
        search_song=false;
        
    });
    $('#balloon_song').mouseover(function() {
        search_song=true;
    });
    $('#balloon_song').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_song){
                $('#balloon_song').hide();
                old_search = null;
            }
        }, 150);
        search_song=false;
    });
    
    // menu 2
    $('#bs_menu2').mouseover(function() {
        if(bs_menu_curr < 2)
            $('#balloon_picture').css('top','250px');
        else
            $('#balloon_picture').css('top','115px');
        if(old_search != '#balloon_picture'){
            $(old_search).fadeOut("fast");
            if(!search_picture)
                $('#balloon_picture').fadeIn("fast");
        }
        old_search = '#balloon_picture';
        search_picture=true;
        
    });
    $('#bs_menu2').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_picture){
                $('#balloon_picture').fadeOut("fast");
                old_search = null;
            }
        }, 150);
        search_picture=false;
        
    });
    $('#balloon_picture').mouseover(function() {
        search_picture=true;
    });
    $('#balloon_picture').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_picture){
                $('#balloon_picture').hide();
                old_search = null;
            }
        }, 150);
        search_picture=false;
    });
    
    // menu 3
    $('#bs_menu3').mouseover(function() {
        if(bs_menu_curr < 3)
            $('#balloon_theme').css('top','300px');
        else
            $('#balloon_theme').css('top','130px');
        if(old_search != '#balloon_theme'){
            $(old_search).fadeOut("fast");
            if(!search_theme)
                $('#balloon_theme').fadeIn("fast");
        }
        old_search = '#balloon_theme';
        search_theme=true;
        
    });
    $('#bs_menu3').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_theme){
                $('#balloon_theme').fadeOut("fast");
                old_search = null;
            }
        }, 150);
        search_theme=false;
        
    });
    $('#balloon_theme').mouseover(function() {
        search_theme=true;
    });
    $('#balloon_theme').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_theme){
                $('#balloon_theme').hide();
                old_search = null;
            }
        }, 150);
        search_theme=false;
    });
    
    // menu 4
    $('#bs_menu4').mouseover(function() {
        if(bs_menu_curr < 4)
            $('#balloon_game').css('top','315px');
        else
            $('#balloon_game').css('top','145px');
        if(old_search != '#balloon_game'){
            $(old_search).fadeOut("fast");
            if(!search_game)
                $('#balloon_game').fadeIn("fast");
        }
        old_search = '#balloon_game';
        search_game=true;
        
    });
    $('#bs_menu4').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_game){
                $('#balloon_game').fadeOut("fast");
                old_search = null;
            }
        }, 150);
        search_game=false;
        
    });
    $('#balloon_game').mouseover(function() {
        search_game=true;
    });
    $('#balloon_game').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_game){
                $('#balloon_game').hide();
                old_search = null;
            }
        }, 150);
        search_game=false;
    });
    
    // menu 6
    $('#bs_menu6').mouseover(function() {
        if(bs_menu_curr < 4)
            $('#balloon_chickywalky').css('top','330px');
        else
            $('#balloon_chickywalky').css('top','190px');
        if(old_search != '#balloon_chickywalky'){
            $(old_search).fadeOut("fast");
            if(!search_chickywalky)
                $('#balloon_chickywalky').fadeIn("fast");
        }
        old_search = '#balloon_chickywalky';
        search_chickywalky=true;
        
    });
    $('#bs_menu6').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_chickywalky){
                $('#balloon_chickywalky').fadeOut("fast");
                old_search = null;
            }
        }, 150);
        search_chickywalky=false;
        
    });
    $('#balloon_chickywalky').mouseover(function() {
        search_chickywalky=true;
    });
    $('#balloon_chickywalky').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_chickywalky){
                $('#balloon_chickywalky').hide();
                old_search = null;
            }
        }, 150);
        search_chickywalky=false;
    });
    
    // menu 7
    $('#bs_menu7').mouseover(function() {
        if(bs_menu_curr < 4)
            $('#balloon_movieclip').css('top','345px');
        else
            $('#balloon_movieclip').css('top','205px');
        if(old_search != '#balloon_movieclip'){
            $(old_search).fadeOut("fast");
            if(!search_movieclip)
                $('#balloon_movieclip').fadeIn("fast");
        }
        old_search = '#balloon_movieclip';
        search_movieclip=true;
        
    });
    $('#bs_menu7').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_movieclip){
                $('#balloon_movieclip').fadeOut("fast");
                old_search = null;
            }
        }, 150);
        search_movieclip=false;
        
    });
    $('#balloon_movieclip').mouseover(function() {
        search_movieclip=true;
    });
    $('#balloon_movieclip').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_movieclip){
                $('#balloon_movieclip').hide();
                old_search = null;
            }
        }, 150);
        search_movieclip=false;
    });
    
    // menu 8
    $('#bs_menu8').mouseover(function() {
        if(bs_menu_curr < 4)
            $('#balloon_vdoringtone').css('top','360px');
        else
            $('#balloon_vdoringtone').css('top','220px');
        if(old_search != '#balloon_vdoringtone'){
            $(old_search).fadeOut("fast");
            if(!search_vdoringtone)
                $('#balloon_vdoringtone').fadeIn("fast");
        }
        old_search = '#balloon_vdoringtone';
        search_vdoringtone=true;
        
    });
    $('#bs_menu8').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_vdoringtone){
                $('#balloon_vdoringtone').fadeOut("fast");
                old_search = null;
            }
        }, 150);
        search_vdoringtone=false;
        
    });
    $('#balloon_vdoringtone').mouseover(function() {
        search_vdoringtone=true;
    });
    $('#balloon_vdoringtone').mouseout(function() {
        this.$timer = setTimeout(function() {
            if(!search_vdoringtone){
                $('#balloon_vdoringtone').hide();
                old_search = null;
            }
        }, 150);
        search_vdoringtone=false;
    });
}

function TB_init(){
    /*$('a.thickbox2').click(function(){
        clearInterval(music_play);
    });
    $('a.thickbox6').click(function(){
        clearInterval(music_play);
    });
	$('a.thickbox7').click(function(){
        clearInterval(music_play);
    });*/
    for(i=2; i<8; i++){
		if((i == 2) || (i == 6) || (i == 7)){
			$("a.thickbox" + i).click(function(){
				var t = this.title || this.name || null;
				var g = this.rel || false;
				TB_show2(t,this.href,g);
				this.blur();
				return false;
			});
		}else{
			$("a.thickbox" + i).click(function(){
				var t = this.title || this.name || null;
				var g = this.rel || false;
				TB_show(t,this.href,g);
				this.blur();
				return false;
			});
		}
    }
	
}

var old_b = " ";
var new_b = '#balloon_song';
function search_button()
{
	$('#search_group').change(function(){
		switch($(this).val()){
			case '1': new_b ='#balloon_song';
				break;
			case '2': new_b ='#balloon_truetone';
				break;
			case '3': new_b ='#balloon_wall';
				break;
			case '4': new_b ='#balloon_theme';
				break;
			case '5': new_b ='#balloon_game';
				break;
			case '6': new_b ='#balloon_chickywalky';
				break;
			case '7': new_b ='#balloon_movie';
				break;
			case '8': new_b ='#balloon_vdo';
				break;
			default: new_b ='#balloon_song';
				break;
		}
		pos = $(this).offset();
		$(new_b).css('top',pos.top-55);
		$(new_b).css('left',pos.left+110);
		$(new_b).fadeIn("fast");
		if(old_b != new_b){
			hide_balloon(old_b);
			$(old_b).hide();
		}
		old_b = new_b;
		
	});
	
	$('#search_continue').click(function(){
		pos = $(this).offset();
		$(new_b).css('top',pos.top-55);
		$(new_b).css('left',pos.left+130);
		$(new_b).fadeIn("fast");
		if(old_b != new_b)
				hide_balloon(old_b);
			old_b = new_b;
	});	
}

function change_cat(type, arg, page)
{
    //alert(arg);
    //alert(type);
    typeid = type;
    cat = arg;
    $('#loading_new').show();
    $('#new_tab').hide();
    xajax_switch_new(type, arg, page, media);
}

function change_search_cat(search_con, search_cat, page)
{
    //alert(search_con);
    //alert(search_cat);
    //alert(page);
    typeid = search_con;
    cat = search_cat;
    $('#loading_new').show();
    $('#new_tab').hide();
    xajax_switch_search(search_con, search_cat, page, media);
}

function auto_play_music()
{
    setTimeout(function(){ 
		ch_song();
		music_play = setInterval(ch_song,25000);	
	},1000);
    
    $('#name_bg').html(name_bg[song_time-1] + ' - ' + artist_bg[song_time-1]);
    player_pop_url = "popup_music_chart.php?keepThis=true&width=700&height=500&service=" + service_bg[song_time-1] + "&content=" + content_bg[song_time-1] + "&media=" + media_bg[song_time-1] + "&TB_iframe=true"
    $('#player_popup').attr('href', player_pop_url);
    temp_music_flash = $('#music_flash').html();
    music_play_state = true;
    $('#music_control').click(function(){
        if(music_play_state){
            document.mediaPlayer.controls.stop();
            clearInterval(music_play);
            $(this).attr('src',"/images/music/play.png");
            //$('#music_flash').html('');
            $('#music_flash').hide();
            music_play_state = false;
        }else{
            document.mediaPlayer.controls.play();
            music_play = setInterval(ch_song,25000);
            $(this).attr('src',"/images/music/pause.png");
            //$('#music_flash').html(temp_music_flash);
            $('#music_flash').show();
            music_play_state = true;
        }
    });
    $('#music_fw').click(function(){
        song_time++;
        if(song_time > 10)
            song_time = 1;
        songurl = '/audio/song' + song_time + '.mp3?' + Math.random();
        //alert(song_time);
        handleControlsPlayOrStop(songurl, name_bg[song_time-1],
            artist_bg[song_time-1], service_bg[song_time-1], content_bg[song_time-1], media_bg[song_time-1]);
        clearInterval(music_play);
        music_play = setInterval(ch_song,25000);
    });
    $('#music_bw').click(function(){
        song_time--;
        if(song_time < 1)
            song_time = 10;
        songurl = '/audio/song' + song_time + '.mp3?' + Math.random();
        handleControlsPlayOrStop(songurl, name_bg[song_time-1],
            artist_bg[song_time-1], service_bg[song_time-1], content_bg[song_time-1], media_bg[song_time-1]);
        clearInterval(music_play);
        music_play = setInterval(ch_song,25000);
    });
}

var song_time=1;
function ch_song()
{
    var songurl
    song_time++;
    if(song_time > 10)
        song_time = 1;
    songurl = '/audio/song' + song_time + '.mp3?' + Math.random();
    handleControlsPlayOrStop(songurl, name_bg[song_time-1],
        artist_bg[song_time-1], service_bg[song_time-1], content_bg[song_time-1], media_bg[song_time-1]);
    
}

function new_music()
{
	ch_song();
	music_play = setInterval(ch_song,25000);
}
