var myScroll;

$(document).ready(function()
{ 
    //preload Menu rollover images
    preLoadImages("/images/mainNavBKGroll.png", "/images/mainNavBKG.png");

    //set galleries if they exist
    $("a[rel^='prettyPhoto']").prettyPhoto();


    //Bind the quickform submission 
    if ($("#theform").length > 0)
    {
        $("#theform").submit(function()
        {
            $(this).ajaxSubmit($("#submitResponse").show());
            return false;
        });
    }

    //myScroll = new iScroll('contentWrapperText');


});





var cache = [];
function preLoadImages()
{
    var args_len = arguments.length;
    for (var i = args_len; i--; )
    {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
        cache.push(cacheImage);
    }

}

function doChRo(item)
{
    $(item).animate({ opacity: 0.5, filter: '' }, 150)
    //cvi_corner.modify(item.firstChild.firstChild.firstChild.firstChild.firstChild.firstChild, { filter: [{ f: 'gamma', s: 1.3}] });
    $(item).animate({ opacity: 1.0, filter: '' }, 150)

}

function doTopArrowRo(item)
{
    if ($(item).attr("id") == "topArrowLeft")
    {
        if ($(item).attr("src") == "images/slideLeft.png")
        {
            $(item).attr("src", "images/slideLeft2.png")
        }
        else
        {
            $(item).attr("src", "images/slideLeft.png")
        }
    }

    if ($(item).attr("id") == "topArrowRight")
    {
        if ($(item).attr("src") == "images/slideRight.png")
        {
            $(item).attr("src", "images/slideRight2.png")
        }
        else
        {
            $(item).attr("src", "images/slideRight.png")
        }
    }

}
function pageWide()
{
	$("#sidebar").css("display", "none");
}



if (window.name != "pod") 
{
    window.name = "main";
}


function doSearch(searchTerm) {
    tmpURL = "search.aspx?searchfield=" + searchTerm;
    window.location = tmpURL;
}


function makeStateSelectOptions(target)
{
    var tmpStateList;
    var tmpReturnString;
    var tmpStateArray;
    var i;
    var selectFlag;

    var tmpStateList = "AL|AK|AZ|AR|CA|CO|CT|DC|DE|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY"
    tmpReturnString = tmpReturnString + "<option value=''>&#160;</option>"
    tmpStateArray = tmpStateList.split("|");
    tmpReturnString = '<select id="' + target + '_state" name="' + target + '_state">'

    for (i = 0; i < tmpStateArray.length; i++)
    {
        tmpReturnString = tmpReturnString + "<option value='" + tmpStateArray[i] + "'>" + tmpStateArray[i] + "</option>"
    }
    tmpReturnString = tmpReturnString + "</select>"
    document.getElementById(target).innerHTML = tmpReturnString;
}

//$(document).ready(function()
//{
//    $("#pageButton-1").addClass("pageButtonRoll");
//});

var prevDiv = "#pageDiv-1";
var prevButton = "#pageButton-1";


function swapPage(objID)
{
    var divIndex = objID.charAt(objID.length - 1);
    var obj = "#pageDiv-" + divIndex;
    var button = "#pageButton-" + divIndex;

    $(obj).css("display", "block");
    $(button).addClass("pageButtonRoll");


    if (obj != prevDiv)
    {
        $(prevDiv).css("display", "none");
        $(prevButton).removeClass("pageButtonRoll");
    }

    prevButton = "#pageButton-" + divIndex;
    prevDiv = "#pageDiv-" + divIndex;
}




var campusSelectState = "closed";
function openCampusSelect()
{
    if (campusSelectState == "closed")
    {
        $("#campusSelectDiv").slideDown("slow");
        campusSelectState = "open";
    }
    else
    {
        $("#campusSelectDiv").slideUp("slow");
        campusSelectState = "closed";
    }
}






function tmpPopup(url) 
{
    window.open(url, null, "height=600,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
}

function openContact(url)
{

    window.open(url, "Contact", "height=650,width=760,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
}




function setConsoleInfo(pageid) 
{
    //alert("called");
    //window.parent.document.getElementById("hdncurrentlocation").value = "";
    window.parent.currentLocation = "";
	
	
	window.parent.document.getElementById("curidspan").innerHTML = pageid;
	//window.parent.document.getElementById("currpageid").value = pageid;
	window.parent.currpageid = pageid;

}

function doRo(item) {
    $(item).animate({ opacity: 0.5 }, 150)
    $(item).animate({ opacity: 1.0 }, 150)

}

//TODO: this exist in functions and in main console functions
var tmpLastElement = null;
function makeActive(tmpElement) {

    window.parent.currentLocation = tmpElement;
    
    tmpLocationArray = tmpElement.split("_")
    tmpElementID = tmpLocationArray[2];
    tmpStoryID = tmpLocationArray[1];

    if (window.parent.editMode == true)
    {
        if (tmpLastElement != null)
        {
            document.getElementById(tmpLastElement).style.backgroundColor = "";
        }
        tmpLastElement = tmpElement;
        document.getElementById(tmpElement).style.backgroundColor = "#ffcc00";
        window.parent.currStoryId = tmpStoryID;
        window.parent.activeIndicator = tmpElementID;
        
    }
}


function openBlog(item)
{
    itemToOpen = "blogOpen" + item;
    itemToClose = "blogEntry" + item;

    for (i = 0; i < 5; i++)
    {
        document.getElementById("blogOpen" + i).style.display = "none";
        document.getElementById("blogEntry" + i).style.display = "block";
    }

    // alert(item);
    document.getElementById(itemToClose).style.display = "none";
    document.getElementById(itemToOpen).style.display = "block";
}


function toggleDiv(div)
{
    $("#"+div).toggle("fast");
}


function toggleSeries(id)
{

    if ($("#button_" + id).attr("src") == "/images/misc/sermons" + id + ".gif")
    {
        $("#button_" + id).attr("src", "/images/misc/sermons" + id + "_b.gif");
    }
    
    else
    {
        $("#button_" + id).attr("src", "/images/misc/sermons" + id + ".gif");
    }
    
    $("#week" + id).toggle("fast");
}

function swapFrame(url,tmpCurrTemplateID)
{
	window.location = url;
}


var prevItem;
function swapRotatorText(i) {
    $(prevItem).fadeOut("slow");
    $("#rotator" + i).fadeIn("slow");
    
    prevItem = "#rotator" + i;
}


var prevPodItem = "#pod1";
function swapPod(i) {
    var item = "#pod" + i;
    $(prevPodItem).fadeOut("slow");
    $(item).fadeIn("slow");

    prevPodItem = item;
}



var prevHomeItem = 0;
function swapHomeContent(id) {

    $(prevHomeItem).fadeOut("slow");
    $("#page" + id).fadeIn("slow");

    prevHomeItem = "#page" + id;
}


function swapHomeDiv(id) {
    var obj = "page" + id;

    document.getElementById("homeMain").style.display = "block";

    if (prevHomeItem != "#page" + id) {
        document.getElementById("page0").style.display = "none";
        document.getElementById("page1").style.display = "none";
        document.getElementById("page2").style.display = "none";

        swapHomeContent(id);
    }

}

function hideHomeContent() {

    if (prevHomeItem != 0) {
        var item = prevHomeItem.replace("#", "");
        document.getElementById(item).style.display = "none";
        document.getElementById("homeMain").style.display = "none";
        //document.getElementById("page1").style.display = "none";
        //document.getElementById("page2").style.display = "none";
        prevHomeItem = 0;
    }
}


//Moved to core.js
function openEvite(id)
{
    var ScreenWidth=window.screen.width;
    var ScreenHeight=window.screen.height;	
    var movefromedge=0;
    placementx=(ScreenWidth/2)-((650)/2);
    placementy=(ScreenHeight/2)-((500+50)/2);
    var PopUpUrl= "evite.aspx?storyid=" + id

	window.open(PopUpUrl,"","width=680,height=550,toolbar=0,location=0,directories=0,status=0,scrollbars=0,menubar=0,resizable=0,left="+placementx+",top="+placementy+",screenX="+placementx+",screenY="+placementy+",");
}


function sendPageLink(headline)
{
	tmpURL = "includes/sendPageLink.asp?pageID=" + document.getElementById("currentpageid").value;
	window.open(tmpURL, null, "height=600,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
}

function printPage()
{
	tmpURL = "print.asp?storyid=" + document.getElementById("currentstoryid").value;
	window.open(tmpURL, "null", "height=800,width=630,status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes,scrollbars=yes");
}

function sendHomeLink(headline)
{
	tmpURL = "sendHomeLink.asp?pageID=" + document.getElementById("currentpageid").value;
	window.open(tmpURL, null, "height=600,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
}

function sendStoryLink()
{
	tmpURL = "includes/sendStoryLink.asp?storyid=" + document.getElementById("currentstoryid").value;
	window.open(tmpURL, null, "height=600,width=800,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
}

function getEventList(pageid, source)
{
    $.ajax({
        url: "/retrieval/getEventList.aspx?pageid=" + pageid + "&source=" + source,
        cache: false,
        success: function(html) {
            $("#mainText").html(html);
            
        }
    });
}

function getGroupsList(pageid, source)
{
    $.ajax({
        url: "/retrieval/getGroupsList.aspx?pageid=" + pageid + "&source=" + source,
        cache: false,
        success: function(html) {
            $("#mainText").html(html);
        }
    });
}


function showGroupItem(item) {
    $("#" + item).toggle("blind","fast");

}


function openItem(id) {

    if (itemOpen != id) {
        $("#mainText").accordion("activate", $("#" + id))
    }
    itemOpen = id;

}


//--------------------
function showGallery(id)
{
    $.ajax({
        url: "/retrieval/getGallery.aspx?gid=" + id,
        cache: false,
        success: function(html)
        {
            $("#gallery").html(html);
            $("a[rel^='prettyPhoto']").prettyPhoto();
        }
    });
}




function closeGallery()
{
    $("#galleryFrame").hide();
	//document.getElementById("galleryFrame").style.display = "none";
}


//--------------------

function showModal()
{
    $("#contact-container").modal({ onOpen: function(dialog)
    {
        dialog.overlay.fadeIn('fast', function()
        {
            dialog.container.fadeIn('fast', function()
            {
                dialog.data.fadeIn('fast');
            });
        });
    }
    });
}

function hideModal()
{
    $("#contact-container").modal({ onClose: function(dialog)
    {
        dialog.data.slideUp('slow', function()
        {
            dialog.container.slideUp('slow', function()
            {
                dialog.overlay.fadeOut('slow', function()
                {
                    $.modal.close(); // must call this!
                });
            });
        });
    }
    });

}



//------------------------


function resizeVidDiv()
{
    document.getElementById("mainImageHolder").style.width = "652px";
    document.getElementById("contentWrapper").style.width = "308px";
    document.getElementById("contentWrapper").style.left = "652px";
    document.getElementById("contentWrapperText").style.width = "298px";
}


function shrinkVidDiv()
{
    document.getElementById("mainImageHolder").style.width = "340px";
    document.getElementById("contentWrapper").style.width = "620px";
    document.getElementById("contentWrapper").style.left = "340px";
    document.getElementById("contentWrapperText").style.width = "610px";
}





function resizeStreamDiv(newHeight)
{
    $("#mainImagePlayerStream").animate({ height: 360 }, 250)
}


function shrinkStreamDiv()
{
    $("#mainImagePlayerStream").animate({ height: 180 }, 250)
	resetStreamDiv();
}



//function resizeVidDiv(newHeight)
//{
//    $("#mainImagePlayer").animate({ height: newHeight }, 250)
//}


//function shrinkVidDiv()
//{
//    $("#mainImagePlayer").animate({ height: 180 }, 250)
//	
//}


var mainImageDefaultwidth = "340px";
function shrinkMainImageDiv()
{
    document.getElementById("mainImage").style.height = "0px";
    mainImageDefaultHeight = "0px";
}


function shrinkVidDiv2()
{
    document.getElementById("mainImage").style.height = mainImageDefaultHeight;

    if ((document.getElementById("hdnvidfile").value != "" && document.getElementById("hdnvidfile").value != "/images/flvvideo/none") || document.getElementById("hdnlcid").value != 0)
    {
        resetVidDiv();
    }
    else
    {
        resetMainImage(document.getElementById("hdnvidimage").value);
    }
}

function writeNewVideo(file, lcid)
{
    //shrinkVidDiv();
    ip = document.getElementById("hdnip").value;
    image = document.getElementById("hdnvidimage").value;
    //wideScreen = document.getElementById("hdniswidescreen").value;
    file = "images/flvvideo/" + file


    var flashObject = '<object id="flvVideo" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"  width="652" height="372" >'
	+ '          <param name="movie" value="mainimageplayer.swf?video=' + file + '&lcid=' + lcid + '&ip=' + ip + '&image=' + image + '&playflag=play" />'
	+ '          <param name="quality" value="high" />'
	+ '      	 <param name="wmode" value="transparent" />'
	+ '          <embed src="mainimageplayer.swf?video=' + file + '&lcid=' + lcid + '&ip=' + ip + '&image=' + image + '&playflag=play" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"  width="652" height="372" wmode="transparent"></embed>'
	+ '          </object>'


    document.getElementById("mainImage").innerHTML = flashObject;
    document.getElementById("headerdiv").scrollIntoView();
}


function writeNewVideoStream(file, lcid)
{
    //shrinkVidDiv();
    ip = document.getElementById("hdnip").value;
    image = document.getElementById("hdnvidimage").value;
    //wideScreen = document.getElementById("hdniswidescreen").value;
    file = "images/flvvideo/" + file


    var flashObject = '<object id="flvVideo" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"  width="652" height="372" >'
	+ '          <param name="movie" value="streammainimageplayer.swf?video=' + file + '&lcid=' + lcid + '&ip=' + ip + '&image=' + image + '&playflag=play" />'
	+ '          <param name="quality" value="high" />'
	+ '      	 <param name="wmode" value="transparent" />'
	+ '          <embed src="streammainimageplayer.swf?video=' + file + '&lcid=' + lcid + '&ip=' + ip + '&image=' + image + '&playflag=play" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"  width="652" height="372" wmode="transparent"></embed>'
	+ '          </object>'


    document.getElementById("mainImage").innerHTML = flashObject;
    document.getElementById("headerdiv").scrollIntoView();
}







function resetVidDiv()
{
    lcid = document.getElementById("hdnlcid").value;
    ip = document.getElementById("hdnip").value;
    image = document.getElementById("hdnvidimage").value;
    file = document.getElementById("hdnvidfile").value;
    //wideScreen = document.getElementById("hdniswidescreen").value;

    var flashObject = '<object id="flvVideo" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="558" height="419" >'
	+ '          <param name="movie" value="mainimagepayer.swf?video=' + file + '&lcid=' + lcid + '&ip=' + ip + '&image=' + image + '&playflag=" />'
	+ '          <param name="quality" value="high" />'
	+ '      	 <param name="wmode" value="transparent" />'
	+ '          <embed src="mainimagepayer.swf?video=' + file + '&lcid=' + lcid + '&ip=' + ip + '&image=' + image + '&playflag=" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="558" height="419" wmode="transparent"></embed>'
	+ '          </object>'

    //alert(flashObject)
    document.getElementById("mainImage").innerHTML = flashObject;

}

function resetMainImage(image)
{
    alert("If you see this tell Lee@aspireone.com");
    var flashObject = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="539" height="203">'
    + '    <param name="movie" value="mainImage.swf?image=' + image + '" />'
    + '    <param name="quality" value="high" />'
	+ '    <param name="wmode" value="transparent" />'
    + '    <embed src="mainImage.swf?image=' + image + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="539" height="203" wmode="transparent"></embed>'
    + '    </object>'
    document.getElementById("mainImage").innerHTML = flashObject;
}

function closeMp3()
{
    $("#mp3Div").css("display", "none");
}


function closeMap()
{
	document.getElementById("mapDiv").style.display = "none";
}

function openMap()
{
	document.getElementById("mapDiv").style.display = "block";
}


