// JavaScript Document

$(function() {
	getBrandFeeds();
	setEqualHeights();
	dropDownMenu();
	faqs();
	partners();
	fixActiveLink();
	tools();
	$('.current-pot').find('span').load('http://feeds.cereusplatform.com/oldfeedsapp/bbjackpot.php');
});

function getBrandFeeds() {
	$('<div class="blog-wrap"><h3>Blog News | Last Entries</h3></div><!--Closes blog-wrap-->').insertAfter('.bbj-wrap');
	$('<div class="blog-wrap"><h3>Blog News | Last Entries</h3></div><!--Closes blog-wrap-->').insertAfter('.title-icons-wrap');
	getBrandFeed('http://blog.chipleader.com/feed');
}

function getBrandFeed(brand) {
	$.jGFeed(brand,
		function(feeds){
			// Check for errors
			if(!feeds){
				// there was an error
				return false;
			}
			// do whatever you want with feeds here
			for (var i=0; i < 3; i++){
				var entry = feeds.entries[i];
				
				var html_print = '<div class="blog-entry">';
				// Add entry title
				html_print += '<h4><a href="' + entry.link + '" target="_blank">' + entry.title + '</a></h4>';
				html_print += '<p><a href="' + entry.link + '" target="_blank">' + wordCount(entry.contentSnippet) + '</a></p>';
				html_print += '</div><!--Closes blog-entry-->';
				
				$(".blog-wrap").append(html_print);
			}
		}, 10);
}            

function setEqualHeights() {	
	var columns = $(".left-content, .right-content");
	var max_height = 0;
	$.each(columns, function() {
		column_height = $(this).height();
		if (column_height > max_height) {
			max_height = column_height;
		}
	});
	var columns2 = $(".left-content, .bottom-sidebar-content");
	$.each(columns2, function(id) {
		if (id == 1) {
			max_height2 = max_height - 284;
			$(this).height(max_height2);
		} else {
			$(this).height(max_height);
		}
	});
}

function dropDownMenu() {
	$(".top-nav li.aff-program-link").hover(function() {
		$(this).parent().find("ul.sub-nav").slideDown('fast');		
		$(this).addClass("hover");
	}, function() {
		$(this).parent().find("ul.sub-nav").slideUp('fast');	
		$(this).removeClass("hover");								
	});
}

function faqs() {
	questions_array = $("#content .content").find("a[id^=fq-]");
	$.each(questions_array, function() {
		$(this)[0].onclick = function(event) {
			id = $(this).attr("id");
			$.scrollTo('#' + id + '-title', {duration: 500, onAfter:function(){
				$('.content div[class=' + id + ' faq-box]').highlightFade({color:'rgb(166, 34, 143)', speed: 1000});
			} });
		};
		$(this).removeAttr("href");
		$(this).hover(function() {
			$(this).parent().addClass("faq-purple");
		}, function() {
			$(this).parent().removeClass("faq-purple");
		});
	});	
	back_top_array = $("#content .content").find("a.back-top");
	$.each(back_top_array, function() {
		$(this)[0].onclick = function(event) {			
			$.scrollTo('.em-list', {duration: 500});
			$(this).removeAttr("href");
		};
	});	
	/*questions_array = $("#content .content").find("a[id^=fq-]");
	$.each(questions_array, function() {
		$(this)[0].onclick = function(event) {
			id = $(this).attr("id");
			number = id.substring(3, id.length);
			$("div[class=fq-" + number + " faq-box]").slideToggle();
		};
	});*/
}

function partners() {
	$("#content .content").find("div[class$=animate] a").addClass("align-midle").parent().find("p").toggle();
	partners_array = $("#content .content").find("a[class$=align-midle]");
	$.each(partners_array, function(id, value) {
		$(this).hover(function() {
			$(this).animate({
				marginTop: 0
			}, 500);
			$(this).parent().find("p").slideDown("fast");
		}, function() {		
			/*$(this).parent().find("p").slideDown("fast").hover(function() {}, function() {
				$(this).slideUp("slow").parent().find("a[class$=align-midle]").animate({
					marginTop: 84,
				}, 500);
			});	*/					
		})
	});
}

function fixActiveLink() {
	current_location = location.href.toLowerCase();
	if (current_location.indexOf("/affiliate-program/faq") > -1 || current_location.indexOf("/affiliate-program/commission-tier-structure") > -1 || current_location.indexOf("/affiliate-program/tools") > -1) {
		$("#header .top-nav").find("li.aff-program-link a").addClass("active");
	} 
}

function tools() {
	current_location = location.href.toLowerCase();
	if (current_location.indexOf("/affiliate-program/tools") > -1 || current_location.indexOf("/affiliate%2dprogram/tools") > -1) {
	//if (current_location.indexOf("program-details-tools") > -1) {
		$("#content .content").find("li:has(a[class^=link-]) ol").hide();
		left_cont_height = $(".left-content").height() - 170;
		right_cont_height = $(".bottom-sidebar-content").height() - 170;
		$(".left-content").height(left_cont_height);
		$(".bottom-sidebar-content").height(right_cont_height);
		link_text = false;
		link_banner = false;
		both_true = false;
		less_height = false;
		$("#content .content").find("li:has(a[class^=link-]) ol").each(function() {
			$(this).parent().find("a[class^=link-]")[0].onclick = function(event) {
				ol_height = $(this).parent().find("ol").height();
				ol_display = $(this).parent().find("ol").css("display");
				switch(ol_display) {
					case "none":
						if($(this).attr("class") == "link-text") {
							link_text = true;
						}
						if($(this).attr("class") == "link-banner") {
							link_banner = true;
						}
						$(this).parent().find("ol").slideDown();
						break;
					case "block":
						if($(this).attr("class") == "link-text") {
							link_text = false;
							both_true = false;
						}
						if($(this).attr("class") == "link-banner") {
							link_banner = false;
							both_true = false;
						}
						$(this).parent().find("ol").slideUp("fast");
						break;
				}
				if(link_text && link_banner) {
					if(!both_true) {
						both_true = true;
						less_height = true;
						left_cont_height = $(".left-content").height() + 170;
						right_cont_height = $(".bottom-sidebar-content").height() + 170;
						$(".left-content").height(left_cont_height);
						$(".bottom-sidebar-content").height(right_cont_height);
					} 
				} else {
					if(less_height) {
						less_height = false;
						left_cont_height = $(".left-content").height() - 170;
						right_cont_height = $(".bottom-sidebar-content").height() - 170;
						$(".left-content").height(left_cont_height);
						$(".bottom-sidebar-content").height(right_cont_height);
					}
				}
			}
		});
	} 
}

function wordCount(text) {
	var text_array = text.split(' ');
	var return_tex = '';
	for (var i=0; i < text_array.length; i++) {
		return_tex += text_array[i] + ' ';
		if (i>=3) {
			if(text_array.length > 3) {
				return_tex += '[...]';
			}
			break;
		}
	}
	return return_tex;
}
