// global variables
var timeout         = 1000;
var closetimer      = 0;
var ddmenuitem      = 0;

var news_num        = 5;

function init(ctrler, action) {
    $(".img_buttons").mouseover(onMouseOverImgButtons);
    $(".img_buttons").mouseout(onMouseOutImgButtons);
    $('#jsddm > li').mouseover(jsddm_open);
    $('#jsddm > li').mouseout(jsddm_timer);
    document.onclick = jsddm_close;
    
    switch (ctrler) {
        case 'home':
            initSlider();
            showHomePopup();
            break;
        case 'news':
            switch (action) {
		case "index":
                    //$('a.map').colorbox(  {maxWidth: '80%',maxHeight: '80%'}  );
                    break;
                case "latest":
                case "past":
                    var news_num = parseInt($('#news_num').html());
                    for (var i=1; i<=news_num; i++) {
                        var gallery_name = 'news_gallery'+i;
                        $('a.'+gallery_name).colorbox(  {
                            maxWidth:'80%', maxHeight:'80%',
                            close:g_MsgClose, previous:g_MsgPrevious, next:g_MsgNext, current:g_MsgCurrent,
                            rel:gallery_name
                        });
                    }
                    $('a.news_video').colorbox();
                    break;
                case "beneficiaries":
                    $('a.beneficiary_btn').colorbox(  {
						maxWidth:'80%', maxHeight:'80%',
						close:g_MsgClose
					});
                    break;
            }
            break;
        case 'member':
            if (action=="news") {
                var news_num = parseInt($('#news_num').html());
                for (var i=1; i<=news_num; i++) {
                    var gallery_name = 'news_gallery'+i;
                    $('a.'+gallery_name).colorbox(  {
						maxWidth:'80%', maxHeight:'80%',
						close:g_MsgClose, previous:g_MsgPrevious, next:g_MsgNext, current:g_MsgCurrent,
						rel:gallery_name
					});
                }
                $('a.news_video').colorbox( {
					maxWidth:'80%', maxHeight:'80%',
					close:g_MsgClose
				});
            }
            break;
    }
    
    // to ensure Colorbox is automatically resized when finished loading the content
    $(document).bind('cbox_complete', function(){
        $.colorbox.resize();
    });
}

function jsddm_open(evt) {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
    
    var a = $(this).find('a').eq(0);
    a.addClass('active_menu');
    //a.css("background-image", "url(images/header/menu_bg_active.png)");
}

function jsddm_close() {
    $('.active_menu').removeClass('active_menu');
    
    if(ddmenuitem) {
        ddmenuitem.css('visibility', 'hidden');
    }
}

function jsddm_timer(evt) {
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer(evt) {	
    if(closetimer) {
	window.clearTimeout(closetimer);
        closetimer = null;
    }
}

function initSlider() {
	//Set Default State of each portfolio piece
	$(".slideshow_paging").show();
	$(".slideshow_paging a:first").addClass("active");
		
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $(".slideshow_window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging + Slider Function
	rotate = function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
		
		$(".slideshow_paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		// update caption
		for (var i=0; i<news_num; i++) {
			if (i == triggerID)
				$('#slideshow_caption_'+i).show();
			else 
				$('#slideshow_caption_'+i).hide();
		}
		
		//Slider Animation
		$(".image_reel").animate({ 
			left: -image_reelPosition
		}, 500 );
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			$active = $('.slideshow_paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.slideshow_paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (3 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});
	
	//On Click
	$(".slideshow_paging a").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});
}
function showHomePopup() {
    Lightview.show({
        url: 'index.php/home/popup/',
        type: 'ajax'
    });
    /*
    var str = "<a href='http://www.waiyin.org.hk/charitywalk2012/' target='_blank'><img style='width:90%;' src='images/home/charitywalk_2012.jpg' /></a>";
        
    $.colorbox({
        maxWidth:'80%', maxHeight:'80%', 
        html: str
    });
    */
}

function onMouseOverImgButtons(evt) {
    var old_src = evt.target.src;
    var new_src = old_src.replace(/.png/i, "_active.png");    
    evt.target.src = new_src;
}
function onMouseOutImgButtons(evt) {
    var old_src = evt.target.src;
    var new_src = old_src.replace(/_active.png/i, ".png");    
    evt.target.src = new_src;
}
function changeBeneficiaryMap(id) {
    if (id=="HK") {
        $('#beneficiaries_hk_title').show();
        $('#beneficiaries_hk_map').show();
        $('#beneficiaries_hk_list').show();
        $('#beneficiaries_other_title').hide();
        $('#beneficiaries_other_map').hide();
        $('#beneficiaries_other_list').hide();
    } else if (id=="OTHER") {
        $('#beneficiaries_hk_title').hide();
        $('#beneficiaries_hk_map').hide();
        $('#beneficiaries_hk_list').hide();
        $('#beneficiaries_other_title').show();
        $('#beneficiaries_other_map').show();
        $('#beneficiaries_other_list').show();
        
    }
}
function showBeneficiaryTooltip(id) {
	var target_url = "news/beneficiaries_detail/" + id + "/";
	//alert(target_url);	
	$('#beneficiary_'+id+'_tooltip').show();
	
	/*
	$.ajax({
	  url: target_url,
	  success: function(data){
		//alert(data);
		//$('#beneficeries_detail').html(data);
		$('#beneficiary_'+id).qtip({
		   content: data,
		   show: 'mouseover',
		   hide: 'mouseout'
		});
	  }
	});
	*/
}
function hideBeneficiaryTooltip(id) {
	$('#beneficiary_'+id+'_tooltip').hide();
}

