﻿// JScript File
jQuery.noConflict()

jQuery(document).ready(function(){

// --- Calendar functions ------
    jQuery('.details').hide();
    jQuery("a.link").hover(
        
        function () {
            jQuery(this).parent("div").css({"z-index":"1000"});
            jQuery(this).next(".details").show();
        },

        function () {       
            jQuery(this).next(".details").hide();
            jQuery(this).parent("div").css({"z-index":"0"});
        }
    );
    //-- kills calendar links
    jQuery("a.link").click(
        function (){
            return false;
        }
    );
    
// --- sermon functions ------

    // -- dynamically add buttons
    jQuery('.samplebuttons').append("<span id='statusA'></span> <b>&nbsp;of</b> <span id='statusC'></span><br/><a href='#' id='back'> < </a> <a href='#' id='first'>To 1st Panel</a> <a href='#' id='next'> > </a>");
    
    // -- dynamically add functions to buttons
    jQuery('#next').click(function(){
        stepcarousel.stepBy("mscCarousel", 1);
        return false;
    });
    
    jQuery('#back').click(function(){
        stepcarousel.stepBy('mscCarousel', -1);
        return false;
    });
    
    jQuery('#first').click(function(){
        stepcarousel.stepTo('mscCarousel', 1);
        return false;
    });
    
    jQuery('.stepcarousel').css({ position: "relative", border: "0px solid navy", overflow: "hidden", width: "189px", height: "55px" });
    jQuery('.belt').css({ position: "absolute", left: "0", top: "0"});
    jQuery('.panel').css({ float: "left", overflow: "hidden", margin: "0px", width: "189px", float: "none", position: "absolute", top: "0" });
    jQuery('.samplebuttons').css({width: "189px", "text-align": "center", display: "block", "background-color": "#333333", padding: "2px"});
    jQuery('.samplebuttons a').css({color: "#fc8a00", padding: "1px 2px", "margin-right": "3px", "text-decoration": "none", display: "inline-block", border: "solid #cccccc 0px"});
    jQuery('.samplebuttons #next, .samplebuttons #back').css({ width: "30px", "font-weight": "bold"});

    //-- launch function 
    jQuery('.audio').click(function(){
        var heading = jQuery(this).attr('title');
        jQuery('#player').append('<div id="msgTitle">' + heading + '</div><span class="external">' + jQuery(this).attr('href') + '</span>');

        jQuery('#msgTitle').css('padding', '4px');
        jQuery('#msgTitle').css('color', '#fc8a00');
       
        jQuery('#player').animate({width: "195px", height: "84px", opacity: 1}, 1000);       
        jQuery(".external").jmp3({
	        playerpath: "singlemp3player.swf",
		    filepath: "http://www.msconline.org/",
		    backcolor: "666666",
		    forecolor: "000000",
		    width: 195,
		    showdownload: "true",
		    autoplay: "true"
	    });
  
        return false;
    });
    //-- load audio player functions
    jQuery('.close').click(function(){

            jQuery('#player').animate({width: "195px", height: "0px", opacity: 0}, 1500);
            jQuery('#player').css('display', 'none');
            jQuery('#msgTitle').remove();
            jQuery('.external').remove();
            return false;
     });
     
//----------------------------- rotator: main page -----------------------------------------------------------------------------

		var rotatorCount = jQuery('#rotator li').length;
		var currentSlide = 0;
		var oldSlide = 0;
        var hiddenPosition = 0;
        var rotateTimeout;
		var headlineTimeout;
        var rotateInProgress = false;
		var $crosslink;		

			// -- generate UI base menu tags
			jQuery('<ul></ul>').attr('id', 'rotator_menu').appendTo('#rotator');
//----------------------------------------------------------------------- setup list menu
			jQuery('#content #rotator li').each(function(e){
				var revOrder = e + 1;		
				var curOrder = rotatorCount + 1;
				var elem = jQuery(this).find('img').attr('src');
				var createThumb = elem.replace('images/slide_images/', 'images/slide_images/thumbnail.aspx?img=');
				
				// generate LI Tags with links
				var $link = jQuery('<a></a>').attr('href', '#' + (e + 1)).addClass('cross-link').html("<img src='" + createThumb + "' />");
				jQuery("<li></li>").html($link).appendTo("#rotator_menu");
				
				// append attributes to rotator list
				jQuery(this).attr('id', e + 1);
				jQuery(this).css({'z-index' : curOrder - revOrder});
				jQuery(this).addClass('rotate_heading');
				
				highLight(currentSlide);
			});
//------------------------------------------------------------------------------------------ begin rotate			
			var beginRotate = function(){
				if (!rotateInProgress) {
					rotateInProgress = true;
                    rotateTimeout = false;
					
					currentSlide = (oldSlide + 1) % rotatorCount ;
					highLight(currentSlide);
					
					jQuery('li.rotate_heading:eq(' + oldSlide + ')').animate({ "opacity": 0 }, 'slow', function() {
                    	jQuery(this).css({'top': hiddenPosition});
                    });

                    jQuery('li.rotate_heading:eq(' + currentSlide + ')').animate({ "opacity": 1 }, 'slow', function() {
						rotateInProgress = false;
						
						if(!rotateTimeout){
							rotateTimeout = setTimeout(beginRotate, 5000);
						}
                     });
                     oldSlide = currentSlide;
				}
			}; // end rotate function		
			rotateTimeout = setTimeout(beginRotate, 5000);
		
			function highLight(dt)
			{	
				jQuery('#rotator_menu li img').removeClass('highlight');
				jQuery('#rotator_menu li img').eq(dt).addClass('highlight');
			}
		
			jQuery(function(){
				jQuery(".cross-link").click(function() {
				    clearTimeout(rotateTimeout);
                    rotateTimeout = false;
					
					var value1 = jQuery(this).attr('href');
					var valInt = value1.slice(1) -1; // value of the link selected -1
					
					if(valInt == rotatorCount -1)
					{
						oldSlide = rotatorCount -1;
						currentSlide = 0;
					}else{
						oldSlide = valInt -1;
						currentSlide = valInt;
					}

					jQuery('#rotator div li').css({'opacity' : 0});
					
					jQuery(value1).animate({ "opacity": 1 }, 'fast');;
					highLight(valInt);
					if(!rotateTimeout){
							rotateTimeout = setTimeout(beginRotate, 5000);
						}
				return false;
				});

        }); // -- end rotator init function;   
//jQuery.ajax({
//    url: 'ajax/callbacks.aspx',
//    type: 'GET',
//    data: {name: 'john'},
//    dataType: 'html',
//    success: function(data){
//        jQuery(data).appendTo('#ddl_year');
//    }
//});
//------------------------------------------------------------------------------------------ Sermon viewer
jQuery('#showYear').hide(); // -- hides DDL and Label
series_ddl(); // -- hides DDL and Label

loadingLbl(); // by default hide the loading animation
playerState(); // by default hide the player

// --- **** initial call get year(s) **** ---
if(jQuery('#ddl_year').length)
{
   ajaxGet('year', '');
   loadingLbl('show');
}

// --- **** ddl year: get sermon seires **** ---
jQuery('#ddl_year').change(function(){
    var selectedYear = jQuery("#ddl_year option:selected").text();
    var selectVal = '<option>-- Select a Sermon Series --</option>';
    
    //-- show loading icon and hide ddl
    loadingLbl('show');
    series_ddl('hide');
    sermonList();
    stopSermon();
    hideStop();
    
    if(jQuery('#ddl_series option').length)
    {
        jQuery('#ddl_series option').remove();
        jQuery('#ddl_series').html(selectVal);
        ajaxGet('series', selectedYear);
    }else{
        jQuery('#ddl_series').html(selectVal);
        ajaxGet('series', selectedYear);
    }
});

// --- **** ddl series: get sermons **** ---
jQuery('#ddl_series').change(function(){

    var listData = jQuery(this).find('option:selected').attr('value');
    
    loadingLbl('show');
    sermonList('hide');
    stopSermon();
    hideStop();
    
    // check if there is any data in the #sermonList DIV if so remove it then add new if not just add new data.
    if(jQuery('#sermonList').html() != '')
    {
        sermonList();
        ajaxGet('sermons', listData);
    }else{
        ajaxGet('sermons', listData);
    }
    
})

// --- *** ajax retreival function **** ---
function ajaxGet(qString1, qString2)
{
    jQuery.get('ajax/callbacks.aspx', {strVal: qString1, strItem : qString2}, 
    function(data){
        value = data;
        switch(qString1)
        {
            case 'year':
                jQuery(data).appendTo('#ddl_year');
                jQuery('#showYear').fadeIn();
                loadingLbl('hide');
                break
            case 'series':
                jQuery(data).appendTo('#ddl_series');
                series_ddl('show');
                loadingLbl('hide');
                break
            case 'sermons':
                jQuery('<ul/>').addClass('sermons').html(data).appendTo('#sermonList');
                
                hideStop(); // -- hide the top button
                jQuery('#sermonList .play').click(function(){
                    hideStop(); // -- hide the top button
                    playSermon(jQuery(this));
                    return false;
                });
                
                loadingLbl('hide');
                sermonList('show');
         }
    });
} // -- END ajax retreival function

function playSermon(itm)
{   
    //-- set up stop button functions
    var elem, parElem;
    elem = jQuery(itm).parent();

    jQuery(elem).find('.stop').show(); // -- shows relative stop btn
    jQuery(elem).find('.stop').click(function(){
        stopSermon();
        hideStop();
        return false;
    })
    
    playerState('show');
    // -- begin playing the clip
    jQuery('#sermonPlayer').html(jQuery(itm).attr('title'));
    jQuery("#sermonPlayer").jmp3({
	    playerpath: "singlemp3player.swf",
		filepath: "http://www.msconline.org/",
		backcolor: "666666",
		forecolor: "000000",
		width: 500,
		showdownload: "true",
		autoplay: "true",
	    showfilename: "false"
    });
}

function stopSermon()
{
    jQuery('#sermonPlayer').html('');
}

function hideStop()
{
    jQuery('#sermonList .stop').hide();
}

// --- **** switch sermon series states **** ---
function series_ddl(state)
{
    switch(state)
    {
        case 'hide':
            jQuery('#showSeries').slideUp('fast'); // -- fade out DDL and Label
            break
        case 'show':
            jQuery('#showSeries').slideDown('slow'); // -- fade in DDL and Label
            break
        default:
            jQuery('#showSeries').hide(); // -- hides DDL and Label
    }
}

// --- **** switch sermon list states **** ---
function sermonList(state)
{
    switch(state)
    {
        case 'hide':
            jQuery('#sermonList').fadeOut('fast');
            break
        case 'show':
            jQuery('#sermonList').fadeIn('slow');
            break
        default:
            jQuery('#sermonList').fadeOut();
            jQuery('#sermonList').html(''); // -- Clear sermon data
    }    
}

// --- ***** change loading label states **** ---
function loadingLbl(state)
{
    switch(state)
    {
        case 'hide':
            jQuery('#msgLabel').fadeOut(); // -- fade out loading label
            break
        case 'show':
            jQuery('#msgLabel').fadeIn(); // -- fade in loading label
            break
        default:
            jQuery('#msgLabel').hide(); // -- hide loading label
    }
}

function playerState(state)
{
    switch(state)
    {
        case 'hide':
            jQuery('#sermonPlayer').hide(); // -- fade out player
            break
        case 'show':
            jQuery('#sermonPlayer').show(); // -- fade in player
            break
        default:
            jQuery('#sermonPlayer').hide();
    }
}

});// -- end jquery functions

startList = function() 
{

  if (document.all && document.getElementById) 
  {

    navRoot = document.getElementById("dmenu");
    
    for (i=0; i < navRoot.childNodes.length; i++) 
    {
		
        node = navRoot.childNodes[i];
 
	    if (node.nodeName=="LI") 
	    {
	        node.onmouseover=function() 
	        {   
	            this.className+=" over";	
	        }
				
	    node.onmouseout=function() 
        {	
          this.className=this.className.replace(" over", "");		
	    }		
     }
    }	
  }
}

window.onload = function() 
{
  startList();
  removeTitle();
}

function removeTitle()
{
// removes the calendar title from showing up.
    if(!document.getElementById("ctl00_ContentPlaceHolder1_Calendar1")) return false;
    var tbl = document.getElementById("ctl00_ContentPlaceHolder1_Calendar1");
    var tblTitle = tbl.setAttribute("title", "");
}

//implement event listeners -----------------------------------------------------------------------------------------------------------------
function showForm(e)
{
    var inviteForm = document.getElementById('inviteForm');
    inviteForm.style.display = 'block';
}

function hideForm(e)
{
    var inviteForm = document.getElementById('inviteForm');
    inviteForm.style.display = 'none';
}

function showLarge(e)
{

var map = document.getElementById('map');

  map.innerHTML = "<img src='images/map.gif' id='map' align='right' />";
    
}

function addListeners(e)
{
	var formLink = document.getElementById('openForm');
	var cancelBtn = document.getElementById('cancel');
	var map = document.getElementById('map');
	
	formLink.innerHTML = "<img src='images/evite.gif' alt='invite a friend' />";
	
	addMyEvent(formLink, 'click', showForm, false);
	
	addMyEvent(cancelBtn, 'click', hideForm, false);
}

addMyEvent(window, 'load', addListeners, false);


function addMyEvent(elm, evType, fn, useCapture)
// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
{
	if(elm.addEventListener)
	{
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}else if(elm.attachEvent){
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}else{
		elm['on' + evType] = fn;	
	}
}

//---------ajax call ------------------------------------------------------------
var obj;

function GetDataViaAJAX()
{
    if(window.ActiveXObject)
    {
        var obj = new ActiveXObject("Microsoft.XMLHTTP");
    }else if(window.XMLHttpRequest){
        var obj = new XMLHttpRequest();
    }else{
        var obj = null;
    }
    
	if(obj!=null)
	{
		obj.onreadystatechange = ProcessResponse;
		obj.open("POST", "http://localhost/test.aspx",  true);
		    //obj.setRequestHeader("Host","localhost");
		obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		    //obj.setRequestHeader("Content-Length","12");
		    //alert("http://localhost/AJAX/helloworld.aspx?value=" + document.getElementById("txt_text").value)
		obj.send("txt_text=" + document.getElementById("txt_text").value);
	}
	return false;
}

function ProcessResponse()
{
	if(obj.readyState == 4)
	{
		if(obj.status == 200)
		{
		var retval=obj.responseText;  
			//alert(retval);
			document.getElementById('returnVal').innerHTML = retval;
		}else{
			//alert("Error retrieving data!" );
			document.getElementById('returnVal').innerHTML = retval;
		}
	}
}