﻿// JavaScript Document
var featureYtId="";
jQuery(document).ready(function(){
	
	// Survey says
	/*
	jQuery("#logoSpace").css("position","relative");
jQuery("#logoSpace").append('<div id="divOverlay" style="z-index:1000000;position:absolute;width:283px;height:57px;background-color:black;top:-1px;left:684px;padding:6px;font-size:18px;border:1px solid white;color:white">The home page has a new look!<br /><a target="_blank" style="color:white" href="http://spreadsheets.google.com/viewform?hl=en&formkey=dEZOaVp4SnBxLTc5QUkwaF9jWUZRWkE6MA">Click here to fill out a survey.</a><a id="closeWindow" href="#" style="color:white;font-size:14px">(close)</a></div>');
jQuery("#closeWindow").click(function(){jQuery("#divOverlay").remove()});
	*/
	// end Survey Says
				
								
	var feature=new xmlPull();
	feature.grabRSS("#featureStory","feature",7);
	var blog=new xmlPull();
	blog.grabRSS("#topThirdRow","blogs",3);
	
	//Show and hide main nav
	jQuery("li#ourchurch > ul").show();
	jQuery("li#ourchurch").addClass("selected");
	jQuery("div#mainNav > ul > li").hover(function() { 
		jQuery("div#mainNav ul > li").removeClass("selected");										   
		jQuery("div#mainNav > ul > li > ul:visible").hide(); 
		jQuery("ul",this).show();
		jQuery(this).addClass("selected");
	});
	
	//preload the rolloever
	jQuery.preLoadImages("http://www.umc.org/atf/cf/%7Bdb6a45e4-c446-4248-82c8-e131b6424741%7D/HOMEPLAYBUTTON.PNG");
 });
	var cache = [];
	jQuery.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    	}
  	}

function xmlPull(){
	var parent=this;
	
	this.grabRSS=function(rssLocation,blogsOrFeature,maxItems){
		parent.maxItems=maxItems;
		parent.blogsOrFeature=blogsOrFeature;
		var rss = jQuery(rssLocation).attr('rss');
		jQuery.ajax({
			type:"GET",
			url:rss,
			dataType:"xml",
				success: function(data){
        		        jQuery(".loader").remove();
                		jQuery(data).find('item').each(function(i){
                                var $item=jQuery(this);
                                storyObject={                   
                                	title:$item.find('title').text(),
                                	description:$item.find('description').text(),
                                	articleLink:$item.find('link').text(),
                                	pubDate:$item.find('pubDate').text(),
                                	imageThumb:$item.find('[nodeName=k:feature_image]').text(),
                                	shortTitle:$item.find('[nodeName=k:shortTitle]').text(),
                                	target:$item.find('[nodeName=k:go_to_article_now_external]').text(),
                                	externalLink2:$item.find('[nodeName=k:go_to_article_now_url_external]').text(),
                                	externalLink:$item.find('[nodeName=k:go_to_article_now]').text(),
									link1Href:$item.find('[nodeName=k:extra_link1_href]').text(),
									link1Name:$item.find('[nodeName=k:extra_link1_name]').text(),
									link2Href:$item.find('[nodeName=k:extra_link2_href]').text(),
									link2Name:$item.find('[nodeName=k:extra_link2_name]').text(),
									homeFeature:$item.find('[nodeName=k:home_feature]').text(),
									homeSubFeature:$item.find('[nodeName=k:home_subfeature]').text(),
									homeFeatureLink:$item.find('[nodeName=k:home_feature_link]').text(),
									videoId:$item.find('[nodeName=k:video]').text(),
									blogImage:$item.find('[nodeName=k:blog_image]').text(),
									readMore:$item.find('[nodeName=k:read_more]').text(),
									customSummary:$item.find('[nodeName=k:custom_home_summary]').text()
                                }
									
									if(storyObject.externalLink==""){storyObject.externalLink=storyObject.articleLink}
								if(parent.blogsOrFeature=="feature"){
									if(i==parent.maxItems){return false;}
									if(i==0){parent.createFeatureBlock(storyObject);jQuery("#homeFirstRow .loader").remove();}
									else if(i>0){parent.createStoryBlock(storyObject,i)};
									if(i==(parent.maxItems-1)){parent.initHovers();}
								}else if(parent.blogsOrFeature=="blogs"){
									if(i==parent.maxItems){return false;}
									if(i==0){jQuery("#topThirdRow .loader").remove();}
									parent.createBlogBlock(storyObject,i);
								}
    							});
				}
		});
	}
	this.initHovers=function(){
		jQuery("#subFeature a.videoIconLink img").hover(function(){jQuery(this).attr("src","http://www.umc.org/atf/cf/%7Bdb6a45e4-c446-4248-82c8-e131b6424741%7D/HOMEPLAYBUTTON.PNG");},function(){jQuery(this).attr("src","http://www.umc.org/atf/cf/%7Bdb6a45e4-c446-4248-82c8-e131b6424741%7D/HOMEPLAYBUTTONTRANS.PNG");});
	}
	
	this.createBlogBlock=function(storyObj,num){
		
		num+=1;
		jQuery("#blog"+num+" .image").append('<a target="_blank" href="'+storyObj.externalLink+'"><img src="'+storyObj.blogImage+'" /></a>');
		jQuery("#blog"+num+" .blogTitle").append('<a target="_blank" href="'+storyObj.externalLink+'">'+storyObj.title+'</a>');
		jQuery("#blog"+num+" .summary").append(storyObj.description);
		
	}
	
	this.createFeatureBlock=function(storyObj){
		if(storyObj.videoId!=''){
			featureYtId=storyObj.videoId;
			//var params = { allowScriptAccess: "always",allowfullscreen:"true",wmode:"transparent" };
    		//var atts = { id: "myytplayer" };
    		//swfobject.embedSWF("http://www.youtube.com/v/"+storyObj.videoId+"?enablejsapi=1&playerapiid=ytplayer&&rel=0&cc_load_policy=0&showinfo=0", "featureImage", "420", "262", "8", null, null, params, atts);
    		jQuery("#featureImage").append('<iframe title="YouTube video player" width="420" height="262" src="http://www.youtube.com/embed/'+featureYtId+'?wmode=transparent" frameborder="0" allowfullscreen></iframe>');
			
			
		}else{
				jQuery("#featureImage").append('<a href="'+storyObj.externalLink+'"><img height="262" width="420" src="'+storyObj.homeFeature+'"/></a>');
		}
		var description=storyObj.description;
		if(storyObj.customSummary!=""){description=storyObj.customSummary;}
		jQuery("#featureTitle").append('<a target="'+storyObj.target+'" href="'+storyObj.externalLink+'">'+storyObj.title+'</a>');
		jQuery("#featureSummary").append(description);
		var readMoreText="FULL STORY";
		if(storyObj.readMore!=""){readMoreText=storyObj.readMore;}
		jQuery("#fullStory").append('<a target="'+storyObj.target+'" href="'+storyObj.externalLink+'">'+readMoreText+'</a>');
		
		var relatedStories="";
		if(storyObj.link1Name){relatedStories+='<li><a href="'+storyObj.link1Href+'">'+storyObj.link1Name+'<a/></li>'}
		if(storyObj.link2Name){relatedStories+='<li><a href="'+storyObj.link2Href+'">'+storyObj.link2Name+'<a/></li>'}
		if(relatedStories!=""){jQuery("#relatedStories").css("background-color","#eeeeee");jQuery("#relatedStories").append('<div class="title">Related Stories:</div><ul>'+relatedStories+'</ul>');}else{jQuery("#featureTitle").css("padding-top","18px");}
	}
	
	this.createStoryBlock=function(storyObj,num){
		var videoIcon="";
		/* if(storyObj.videoId!=''){videoIcon='<a target="'+storyObj.target+'" class="videoIconLink" href="'+storyObj.externalLink+'"><img src="http://www.umc.org/atf/cf/%7Bdb6a45e4-c446-4248-82c8-e131b6424741%7D/HOMEPLAYBUTTONTRANS.PNG" class="videoIcon" /></a>'}
		*/
		jQuery("#storyBlock"+num).html(videoIcon+'<a target="'+storyObj.target+'" href="'+storyObj.externalLink+'"><img  height="75" width="130" class="subFeatureImg" src="'+storyObj.homeSubFeature+'"/></a><div class="title"><a target="'+storyObj.target+'" href="'+storyObj.externalLink+'">'+storyObj.title+'</a><div>');		
		
	}
}

function onYouTubePlayerReady(playerId){
	
	ytplayer = document.getElementById("myytplayer");
	ytplayer.addEventListener("onStateChange","onytplayerStateChange")	
}

function onytplayerStateChange(newState){
	if(newState==0){ytplayer = document.getElementById("myytplayer");
	var embedCode=ytplayer.getVideoEmbedCode();
jQuery("#featureStory").append('<div id="shareDiv"><h2 id="replayVid">Replay</h2><h3 class="embed">Embed:</h3><textarea name="embedCode" cols="40" rows="5">'+embedCode+'</textarea><h2 class="shareFb">Share on Facebook</h2><h2 class="shareT">Share on Twitter</h2></div>');
	jQuery("#shareDiv textarea").click(function(){selectAllText(jQuery(this))});
	jQuery(".shareFb").click(function(){shareVideo("facebook")});
	jQuery(".shareT").click(function(){shareVideo("twitter")});
	jQuery("#replayVid").click(function(){jQuery("#shareDiv").remove();ytplayer.seekTo(0,true);ytplayer.playVideo();});
	
	}
}
function selectAllText(textbox) {
    textbox.focus();
    textbox.select();
}

function shareVideo(service){
	var url;
	var height;
	var width;
	var scrollbars=""
	var title=encodeURI(jQuery("#featureTitle").text());
	if(service=="facebook"){url='http://www.facebook.com/sharer.php?u=http%3A//www.youtube.com/watch%3Fv%3D'+featureYtId+'&t='+title;width=620;height=440;}
	else if(service=="twitter"){url='http://twitter.com/home?status=Check%20this%20video%20out%20--%20'+title+'%20http%3A//youtu.be/'+featureYtId;width=1024;height=650;scrollbars=",scrollbars";}
	newwindow=window.open(url,'name','height='+height+',width='+width+scrollbars);
	
}



 
 /* *********************************************************** 
 *	FUNCTION: searchFunction
 *	PURPOSE: searchysearchy
 * ********************************************************* */
 function siteRefinement() {
    var domain = document.domain;
    var domainStripped = domain.substring(domain.indexOf(".") + 1, domain.indexOf(".", 4)).toLowerCase();
    var refine;
    switch (domainStripped) {
    case "gcah":
        refine = "gcah_sites";
        break;
    case "gcorr":
        refine = "gcorr_sites";
        break;
    case "umcgiving":
        refine = "umcgiving_sites";
        break;
    case "gbhem":
        refine = "gbhem_sites";
        break;
    case "gbod":
        refine = "gbod_sites";
        break;
    case "umcom":
        refine = "umcom_sites";
        break;
    case "umc":
        refine = "umc_sites";
        break;
    case "koreanumc":
        refine = "koreanumc";
        break;
    default:
        refine = "";
    }
    return refine;
}
 
function siteResults() {
    var domain = document.domain;
    var domainStripped = domain.substring(domain.indexOf(".") + 1, domain.indexOf(".", 4)).toLowerCase();
 
 var resultsPage;
    switch (domainStripped) {
    case "gcah":
        resultsPage = "http://www.gcah.org/site/c.ghKJI0PHIoE/b.6093405/k.6F7B/Search_Results_GCAHorg.htm";
        break;
    case "gcorr":
        resultsPage = "http://www.gcorr.org/site/c.mwKWJ9NTJtF/b.6094613/k.7C72/Search_Results_GCORRorg.htm";
        break;
    case "umcgiving":
        resultsPage = "http://www.umcgiving.org/site/c.qwL6KkNWLrH/b.6094643/k.715C/Search_Results_UMC_Giving.htm";
        break;
    case "gbhem":
        resultsPage = "http://www.gbhem.org/site/c.lsKSL3POLvF/b.5888555/k.9E16/Search_Results.htm";
        break;
    case "gbod":
        resultsPage = "http://www.gbod.org/site/c.nhLRJ2PMKsG/b.5898067/k.9E6A/Search_Results.htm";
        break;
    case "umcom":
        resultsPage = "http://www.umcom.org/site/c.mrLZJ9PFKmG/b.5489619/k.9EA6/Search_Results.htm";
        break;
    case "umc":
        resultsPage = "http://www.umc.org/site/c.lwL4KnN1LtH/b.5687105/k.77B9/Search_Results_UMCorg.htm";
        break;
    case "koreanumc":
        resultsPage = "http://www.koreanumc.org/site/c.kqLSIYOxGnF/b.6149693/k.9E11/Search_Results.htm";
        break;
    default:
        resultsPage = "http://www.umc.org/site/c.lwL4KnN1LtH/b.5687105/k.77B9/Search_Results_UMCorg.htm";
    }
    return resultsPage;
}
function search() {
    var url = siteResults();
    url += "?query=" + escape(jQuery("#ajaxSearch").val());
    if (jQuery("input#this:checked").val() != null) {
        var refine = siteRefinement();
        url += "&more=" + refine;
    }
    window.open(url);
} 
 /* End searchFunction script */
