var templateBasePath = "";
var clear="images/spacer.gif" //path to clear.gif
var homeHeroArr = new Array();


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setCookie ( name, value )
{
    var d = new Date();
    d.setMinutes(d.getMinutes()+30);
  //var cookie_string = name + "=" + escape ( value ) + "; expires=" + d.toGMTString();
  
  var cookie_string = name + "=" + escape ( value ) + ";";
  document.cookie = cookie_string;
}


pngfix=function(){var els=document.getElementsByTagName('*');var ip=/\.png/i;var i=els.length;while(i-- >0){var el=els[i];var es=el.style;if(el.src&&el.src.match(ip)&&!es.filter){es.height=el.height;es.width=el.width;es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";el.src=clear;}else{var elb=el.currentStyle.backgroundImage;if(elb.match(ip)){var path=elb.split('"');var rep=(el.currentStyle.backgroundRepeat=='no-repeat')?'crop':'scale';es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";es.height=el.clientHeight+'px';es.backgroundImage='none';var elkids=el.getElementsByTagName('*');if (elkids){var j=elkids.length;if(el.currentStyle.position!="absolute")es.position='static';/*while (j-- >0)if(!elkids[j].style.position)elkids[j].style.position="relative";*/}}}}}


$(document).ready(function(){
	
	positionBackground();
	
	buttonWrap();
	$("#welcomeBox .page").css("visibility","visible").hide();
	$("#welcomeTestimonial .page").css("visibility","visible").hide();
	$("#mainTop div.right .page").css("visibility","visible").hide();
	
	// POPULATE COMMUNITY SECTION
	if (BEXlang == "es"){
	} else {
	   $.get('proxy.php?url='+escape('http://pipes.yahoo.com/pipes/pipe.run?_id=NhcUsAYw3hGyjusadvQQIA&_render=rss'), function(d) {
			parseRSSItem("Community Activity", d);	
			$.get('proxy.php?striphtml=1&url='+escape('http://pipes.yahoo.com/pipes/pipe.run?_id=b6ec02ee09c62cc9647faa29b562ed70&_render=rss'), function(e) {
					parseRSSEntry("Recent Blog Posts", e);	
					$.get('proxy.php?url='+escape('http://pipes.yahoo.com/pipes/pipe.run?_id=7281b78e0ac6637e1939ed926fecb397&_render=rss'), function(f) {
							parseRSSForum("Forums", f);					   
					});
			});
	   });
	}

	exdetails(0);
	loginpage(1);
	
	
	$("button#signInBtn").click(function(){
		
		$.get("zug-api/ajaxlogin.php", {email: $("#signInForm input[name=email]").get(0).value, password: $("#signInForm input[name=password]").get(0).value, remember: $("#signInForm input[name=rememberBool]").eq(0).is(':checked')}, function(d){
			if(d!="0"){
				$("#guestname").text(d);
				$("#loggedInName").text(d);
				loginpage(2);
			}else{
				loginpage(3);
			}
		});
		return false;
	});
		
	$("button#forgetBtn").click(function(){
		
		$.get("zug-api/email_password.php", {email: $("#forgetForm input[name=email]").get(0).value}, function(d){
			if(d.indexOf("fail")==-1){
				loginpage(4);
			}else{
				loginpage(5);
			}
		});
		return false;
	});
		
	$("button").each(function(){
		if($(this).attr("href")){
			$(this).click(function(){
				window.location = $(this).attr("href");				   
			});
		}
	});
	$("#featureSection .content").css("visibility", "visible");
	rotateFeature();
	$(window).resize(function(){
	  positionBackground()
	});


})


var rotateFeatureInt;
var featureCurrent = -1;
function rotateFeature(){
	clearTimeout ( rotateFeatureInt );
	featureCurrent++;
	if(featureCurrent > $("#featureSection .content").length-1){
		featureCurrent = 0;
	}
	var target = $("#featureSection .content").eq(featureCurrent);
	
	//alert(featureCurrent);
	$("#featureSection .content").not(target).hide();
	$(target).fadeIn();
	rotateFeatureInt = setTimeout (  "rotateFeature()", 10000 );
}




function buttonWrap(){
	/*
	if ($.browser.msie && jQuery.browser.version.substr(0,1)=="6") {
  		$("button.gradient").each(function(){
  			$(this).css("border","2px #a1a1a1 solid").css("margin","10px 10px 10px 0").css("height","30px").css("backgroundColor","#3d5c75");
  		});
	}else{*/
		$("button.gradient").each(function(){			
			//var wasFloat = $(this).css("float");
			//$(this).css("float","left");
			var tempColor = $(this).attr("color");
			
		
			
			$(this).addClass(tempColor);
			$(this).html('<span class="left"><img src="'+templateBasePath+'images/btn_left_'+tempColor+'.png" /></span><span class="text">'+$(this).text()+'</span><span class="right"><img src="'+templateBasePath+'images/btn_right_'+tempColor+'.png" /></span>');
			var theWidth = $(this).find("span.text").eq(0).width() + 40;
			if($(this).text()=="Get Started Now"){
				//alert($(this).find("span.text").eq(0).width());	
			}
			if($.browser.mozilla){
			$(this).css("width", theWidth+6+"px");
			}else{
				
			$(this).css("width", theWidth+6+"px").css("padding", "0 3px");
			}
			//$(this).find("span.right").eq(0).css("left", theWidth-10+"px");
			
   		});
	//}
	
	
	if($.browser.msie && parseInt($.browser.version)<7){
		pngfix();
		
	}
	
}

var exdetailsNum = null;
function exdetails(i){
	if(exdetailsNum != i){
	exdetailsNum = i;
	var target = $("#welcomeBox .page").eq(i);
	$("#welcomeBox .page").not(target).hide();
	$(target).show();
	}
}

var exquoteNum = null;
function exquotes(i){
	//$("#debug").text($("#welcomeTestimonial .page").eq(i).text());
	if(exquoteNum != i){
	exquoteNum = i;
	var target = $("#welcomeTestimonial .page").eq(i);
	$("#welcomeTestimonial .page").not(target).hide();
	$(target).show();
	}
}

function loginpage(i){
	var target = $("#mainTop div.right .page").eq(i-1);
	$("#mainTop div.right .page").not(target).hide();
	$(target).show();
}


function positionBackground(){
	//alert($("body").css());
	var str = Math.round(($(window).width()/2 - 476)-900) + "px 100px";
	//alert(str);
	//$("#container").css({backgroundPosition: str});
	$("#container").attr("style", "background-position:"+str+";");
	
}


function parseRSSItem(title, d) {
	
	
		//find each 'item' in the file and parse it
		//$('#mainBottom .section.right .activity').append($("<h3 style='padding-top:10px;'><a href='http://community.becomeanex.org/'>Visit the Community &gt;</a>"+title+"</h3>"));
		//alert($(d).find('rss'));
	
		$(d).find('item').each(function(i) {
									
 			if(i<2){
			//name the current found item this for this particular loop run
			var $item = $(this);
			// grab the post title
			var title = $item.find('title').text();
			// grab the post's URL
			var link = $item.find('link').text();
			// next, the description
			var description = $item.find('description').text();
			//don't forget the pubdate
			var pubDate = $item.find('pubDate').text();
 
			// now create a var 'html' to store the markup we're using to output the feed to the browser window
			var html = "<div class=\"entry\">";//<h4 class=\"postTitle\">" + title + "<\/h4>";
			//html += "<em class=\"date\">" + pubDate + "</em>";
			html += description ;
			//html += "<a href=\"" + link + "\" target=\"_blank\">Read More >><\/a>";
			html += "<\/div>";
 
			//put that feed content on the screen!
			$('div#mainBottom div.section.right .activity').append($(html));  
			}
			
		});		
		

};


function parseRSSEntry(title, d) {
	//find each 'item' in the file and parse it
	//$('#mainBottom .section.right .blog').append($("<div class='hr'></div><h3><a href='http://community.becomeanex.org/profiles/blog/list'>All Blogs &gt;</a>"+title+"</h3>")); 
	$(d).find('item').each(function(i) {
//		if(i<2){
			//name the current found item this for this particular loop run
			var $item = $(this);
			// grab the post title
			var title = $item.find('title').text();
			// grab the post's URL
			var link = $item.find('link').text();
			// next, the description
			var description = $item.find('description').text();
			//don't forget the pubdate
			var pubDate = $item.find('updated').text();
         var titleMarginStyle = "";

         if (description == '') {
            titleMarginStyle = " style='margin-bottom:4px;'";
         }
			// now create a var 'html' to store the markup we're using to output the feed to the browser window
			var html = "<div class=\"entry\"" + titleMarginStyle + "><h4 class=\"postTitle\"><a href=\"" + link + "\" target=\"_blank\">" + title + "<\/a><\/h4>";
			//html += "<em class=\"date\">" + pubDate + "</em>";
         if (description != '') {
   			html += "<div class=\"description\" style='line-height:1.2em; height:2.4em; overflow:hidden; margin-bottom:6px;'>" + description + "</div>";
         }
			//html += "<a href=\"" + link + "\" target=\"_blank\">Read More >><\/a>";
			html += "<\/div>";
 
			//put that feed content on the screen!
			$('div#mainBottom div.section.right .blog').append($(html));  
//		}
	});			
		
 	$('div#mainBottom div.section.right embed').remove();
	$('div#mainBottom div.section.right div.description img').remove();
};

function parseRSSForum(title, d) {
		//find each 'item' in the file and parse it
		//$('#mainBottom .section.right .forum').append($("<div class='hr'></div><h3><a href='http://community.becomeanex.org/forum'>All Forums &gt;</a>"+title+"</h3>")); 
		$(d).find('item').each(function(i) {
 			if(i<3){
			//name the current found item this for this particular loop run
			var $item = $(this);
			// grab the post title
			var title = $item.find('title').text();
			// grab the post's URL
			var link = $item.find('link').text();
			// next, the description
			var description = $item.find('summary').text();
			//don't forget the pubdate
			var pubDate = $item.find('updated').text();
 
			// now create a var 'html' to store the markup we're using to output the feed to the browser window
			var html = "<div class=\"entry\" style='margin-bottom:4px;'><h4 class=\"postTitle\"><a href=\"" + link + "\" target=\"_blank\">" + title + "</a><\/h4>";
			//html += "<em class=\"date\">" + pubDate + "</em>";
			//html += "<p class=\"description\">" + description + "</p>";
			//html += "<a href=\"" + link + "\" target=\"_blank\">Read More >><\/a>";
			html += "<\/div>";
 
			//put that feed content on the screen!
			$('#mainBottom .section.right .forum').append($(html));  
			}
			
		});				
 
};
