var root = "http://worldvision.latest.uidgets3.appspot.com/"
//var root = "http://10.1.1.63:8080/"

function getWindowHeight(){
    if(window.innerHeight){
        return window.innerHeight;
    }else if(document.documentElement.clientHeight){
        if(document.documentElement.clientHeight==0){
            return document.body.clientHeight;
        }
        else{
            return document.documentElement.clientHeight;
        }
    }
}
function findPos(obj) {
	var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
	return [curleft,curtop];
}

function resetAbout(){
	jQuery('#about_shown1').removeAttr('id');
	jQuery('#about_shown2').removeAttr('id');
	jQuery('#about_shown3').removeAttr('id');
	jQuery('#about_shown4').removeAttr('id');
	jQuery('#about_shown5').removeAttr('id');
	jQuery('#about_shown6').removeAttr('id');
	
}

jQuery(document).ready(function(){
    var objxy = findPos(document.getElementById('root_wV')); 
    var x = objxy[0];
    var y = objxy[1];
    var h = getWindowHeight();
    jQuery('.get_widget_wV').css('top','0');
    jQuery('.get_widget_wV').css('left',String(((document.documentElement.clientWidth/2)-225))-x);
    jQuery('.widget_code_wV').append('<textarea rows=\"6\" cols=\"63\">'+psource+'</textarea>');
});

jQuery(function(){
    jQuery('.embed_wV').click(function(){
        jQuery('.get_widget_wV').css('display','block');
        return false;
    });
    jQuery('.close_wV').click(function(){
        jQuery('.get_widget_wV').css('display','none');
        return false;
    });
	jQuery("a#news_tab_wV").click(function(){
		var div = jQuery(".content_wV_news");
		if(!div.hasClass("show_wV")){
			jQuery(".content_wV_news").addClass("show_wV");
			jQuery(".content_wV_donate").removeClass("show_wV");
			jQuery(".content_wV_donate").hide("fast");
			jQuery(".content_wV_news").show("fast");
			jQuery("a#news_tab_wV").css("background-image","url("+ root + "static/images/news_tab_selected.jpg)");
			jQuery("a#donate_tab_wV").css("background-image","url("+ root + "static/images/donate_tab_unselected.jpg)");
		}
		return false;	
	});
	jQuery("a#donate_tab_wV").click(function(){
		var div = jQuery(".content_wV_donate");
		if(!div.hasClass("show_wV")){
			jQuery(".content_wV_donate").addClass("show_wV");
			jQuery(".content_wV_news").removeClass("show_wV");
			jQuery(".content_wV_news").hide("fast");
			jQuery(".content_wV_donate").show("fast");
			jQuery("a#donate_tab_wV").css("background-image","url("+ root + "static/images/donate_tab_selected.jpg)");
			jQuery("a#news_tab_wV").css("background-image","url("+ root + "static/images/news_tab_unselected.jpg)");
		}
		return false;
	});
	jQuery(".infoclick").click(function(){
		var div = jQuery(this);
		if(div.hasClass("moreinfo")){
			div.addClass("lessinfo");
			div.removeClass("moreinfo");
			jQuery(".content_wV_1").hide("slow");
			jQuery(".content_wV_2").show("slow");
			jQuery(".about_page1").show("slow");
			jQuery(".about_page1").addClass("show_content_wV");
			jQuery(".menu_about_wV").addClass("show_content_wV");
		}
		else{
			div.addClass("moreinfo");
			div.removeClass("lessinfo");
			jQuery(".content_wV_2").hide("slow");
			jQuery(".about_pages.show_content_wV").hide("slow");
			jQuery(".about_pages.show_content_wV").removeClass("show_content_wV");
			jQuery(".menu_about_wV").removeClass("show_content_wV");
			jQuery(".content_wV_1").show("slow");			
		}
		return false;
	});
	jQuery(".page_wV").click(function(){
		var div = jQuery(this);
		if(div.hasClass("page1_wV")){
			jQuery(".about_pages.show_content_wV").hide("slow");
			jQuery(".about_pages.show_content_wV").removeClass("show_content_wV");
			resetAbout();
			div.attr('id','about_shown1');
			jQuery(".about_page1").addClass("show_content_wV");
			jQuery(".about_page1").show("slow");
		}
		else if(div.hasClass("page2_wV")){
			jQuery(".about_pages.show_content_wV").hide("slow");
			jQuery(".about_pages.show_content_wV").removeClass("show_content_wV");
			resetAbout();
			div.attr('id','about_shown2');
			jQuery(".about_page2").addClass("show_content_wV");
			jQuery(".about_page2").show("slow");
		}
		else if(div.hasClass("page3_wV")){
			jQuery(".about_pages.show_content_wV").hide("slow");
			jQuery(".about_pages.show_content_wV").removeClass("show_content_wV");
			resetAbout();
			div.attr('id','about_shown3');
			jQuery(".about_page3").addClass("show_content_wV");
			jQuery(".about_page3").show("slow");
		}
		else if(div.hasClass("page4_wV")){
			jQuery(".about_pages.show_content_wV").hide("slow");
			jQuery(".about_pages.show_content_wV").removeClass("show_content_wV");
			resetAbout();
			div.attr('id','about_shown4');
			jQuery(".about_page4").addClass("show_content_wV");
			jQuery(".about_page4").show("slow");
		}
		else if(div.hasClass("page5_wV")){
			jQuery(".about_pages.show_content_wV").hide("slow");
			jQuery(".about_pages.show_content_wV").removeClass("show_content_wV");
			resetAbout();
			div.attr('id','about_shown5');
			jQuery(".about_page5").addClass("show_content_wV");
			jQuery(".about_page5").show("slow");
		}
		else{
			jQuery(".about_pages.show_content_wV").hide("slow");
			jQuery(".about_pages.show_content_wV").removeClass("show_content_wV");
			resetAbout();
			div.attr('id','about_shown6');
			jQuery(".about_page6").addClass("show_content_wV");
			jQuery(".about_page6").show("slow");
		}
		return false;
	});
	
	// Get the news feed and populate the latest news
	var temp = jQuery(".news_item").clone();
	jQuery(".news_item").remove();
	for(i=0;i<newsfeeds.length ;i++){
		var content = temp.clone();
        jQuery(".news_title",content).text("");
		jQuery(".news_title",content).append("<a target=\"_blank\" class=\"news_title\" href=" + newsfeeds[i].news.nlink + ">"+newsfeeds[i].news.ntitle+"</a>");
		jQuery(".news_date",content).text(newsfeeds[i].news.ndate);
		jQuery(".news_desc",content).text("");
		jQuery(".news_desc",content).append(newsfeeds[i].news.ncontent);
		jQuery(".news_desc>a",content).remove();
		jQuery(".news_desc>div",content).remove();
		jQuery(".news_desc>img",content).remove();
		jQuery(".news_link",content).append("<a target=\"_blank\" class=\"news_link\" href=" + newsfeeds[i].news.nlink + ">read more</a>");
		jQuery(".scroll-pane").append(content);
	}
	
	// Get the child sponsership details and append that to the dom
    jQuery(".child_image").append("<img class=\"child_image_img\" src=\"" + root + "imgcache/" + childdetails.child.childRef + "\"/>");
    jQuery(".child_name").text(childdetails.child.fname);
    jQuery(".child_age").text(childdetails.child.age);
    jQuery(".child_country").text(childdetails.child.country);
    jQuery(".child_hobby").text(childdetails.child.hobby);
    jQuery("#sponsor_form").append("<input type=\"hidden\" name=\"childRef[]\" value=" + childdetails.child.childRef + " id=\"childRef\"/>");
});