var auto_timer;

function initFunctions(myFunction) {
	if(window.addEventListener)
		window.addEventListener('load', myFunction, false);
	else if(window.attachEvent)
		window.attachEvent('onload', myFunction);
}

function showfeature(featurecat,featureid,featuretype,featureuid,feedlimit,feedoffset,object_id,object_type,Artist,ArtistDir,Photopath) {
	if (auto_timer) {
		clearTimeout(auto_timer);
	};
	var FeatureLinkID =featurecat + featureuid + '-link-' + featuretype;
	FeatureLinkID=FeatureLinkID.replace(/ /,'_');

	var FeatureSectionsID =featurecat + featureuid + '-feature-sections';
	var FeatureControlsID =featurecat + featureuid + '-feature-controls';
	var FeatureControlsIDprev =featurecat + featureuid + '-prev';
	var FeatureControlsIDnext =featurecat + featureuid + '-next';

	if (featuretype == 'Chat') {
		document.cookie ='chat_default=true; path=/';
	} else if (featuretype == 'Online Now') {
		document.cookie ='chat_default=false; path=/';
	} else if (featuretype == 'Happening Now') {
		document.cookie ='chat_default=false; path=/';
	} else if (featuretype == 'Comments') {
		document.cookie ='chat_default=false; path=/';
	};

	if (!feedoffset) { feedoffset=0; };

	var lis = jQuery('#' + FeatureSectionsID + " > li");
	lis.each(function(){ jQuery(this).removeClass("selected"); });

	jQuery('#' + FeatureLinkID).addClass("selected");

	displayHappeningNow= function(data) {
		data[0]+="<div style='clear: both;'></div>\n";
		jQuery('#TopicHome1-displaybox').html(data[0]);
	}
	displayFarkHeadlines= function(data) {
		data[0]+="<div style='clear: both;'></div>\n";
		jQuery('#FarkHeadlines1-displaybox').html(data[0]);
	}
	displayPopeaterHeadlines= function(data) {
		data[0]+="<div style='clear: both;'></div>\n";
		jQuery('#PopeaterHeadlines1-displaybox').html(data[0]);
	}
	displayBuzzFeedHeadlines= function(data) {
		data[0]+="<div style='clear: both;'></div>\n";
		jQuery('#BuzzFeedHeadlines1-displaybox').html(data[0]);
	}
	displayTheFriskyHeadlines= function(data) {
		data[0]+="<div style='clear: both;'></div>\n";
		jQuery('#TheFriskyHeadlines1-displaybox').html(data[0]);
	}
	displayWonderwallHeadlines= function(data) {
		data[0]+="<div style='clear: both;'></div>\n";
		jQuery('#WonderwallHeadlines1-displaybox').html(data[0]);
	}
	displayMostCommentedNews= function(data) {
		data[0]+="<div style='clear: both;'></div>\n";
		jQuery('#MostCommented1-displaybox').html(data[0]);
	}
	displayMembersOnlineNow= function(data) {
		data[0]+="<div style='clear: both;'></div>\n";
		jQuery('#Members1-displaybox').html(data[0]);
	}
	displayFeaturePhotos= function(data) {
		data[0]+="<div style='clear: both;'></div>\n";
		jQuery('#Photos1-displaybox').html(data[0]);
	}

	var rand_no = Math.random();
	if (featuretype=='Happening Now') {
		var script = document.createElement("script");
		script.src = "http://cdn-dq.starpulse.com/feed/feature.happening-now.php?jsoncallback=displayHappeningNow&rand=" + rand_no;
		script.type = 'text/javascript';
		document.body.appendChild(script);
	} else if (featurecat=='FarkHeadlines') {
		var script = document.createElement("script");
		script.src = "http://cdn-dq.starpulse.com/feed/feature.external_site_headlines.php?external_site=Fark&jsoncallback=displayFarkHeadlines&rand=" + rand_no;
		script.type = 'text/javascript';
		document.body.appendChild(script);
	} else if (featurecat=='PopeaterHeadlines') {
		var script = document.createElement("script");
		script.src = "http://cdn-dq.starpulse.com/feed/feature.external_site_headlines.php?external_site=Popeater&jsoncallback=displayPopeaterlines&rand=" + rand_no;
		script.type = 'text/javascript';
		document.body.appendChild(script);
	} else if (featurecat=='BuzzFeedHeadlines') {
		var script = document.createElement("script");
		script.src = "http://cdn-dq.starpulse.com/feed/feature.external_site_headlines.php?external_site=BuzzFeed&jsoncallback=displayBuzzFeedHeadlines&rand=" + rand_no;
		script.type = 'text/javascript';
		document.body.appendChild(script);
	} else if (featurecat=='TheFriskyHeadlines') {
		var script = document.createElement("script");
		script.src = "http://cdn-dq.starpulse.com/feed/feature.external_site_headlines.php?external_site=TheFrisky&jsoncallback=displayTheFriskyHeadlines&rand=" + rand_no;
		script.type = 'text/javascript';
		document.body.appendChild(script);
	} else if (featurecat=='WonderwallHeadlines') {
		var script = document.createElement("script");
		script.src = "http://cdn-dq.starpulse.com/feed/feature.external_site_headlines.php?external_site=Wonderwall&jsoncallback=displayWonderwallHeadlines&rand=" + rand_no;
		script.type = 'text/javascript';
		document.body.appendChild(script);
	} else if ((featurecat=='MostCommented') && (featuretype=='News')) {
		var script = document.createElement("script");
		script.src = "http://cdn-dq.starpulse.com/feed/feature.mostcommented-news.php?jsoncallback=displayMostCommentedNews&rand=" + rand_no;
		script.type = 'text/javascript';
		document.body.appendChild(script);
	} else if ((featurecat=='Members') && (featuretype=='Online Now')) {
		var script = document.createElement("script");
		script.src = "http://cdn-dq.starpulse.com/feed/feature.members-onlinenow.php?jsoncallback=displayMembersOnlineNow&feedoffset=" + feedoffset;
		script.type = 'text/javascript';
		document.body.appendChild(script);
	} else if ((featurecat=='Photos') && ((featuretype=='Daily') || (featuretype=='Events') || (featuretype=='Slideshows') || (featuretype=='Popular'))) {
		var script = document.createElement("script");
		script.src = "http://cdn.starpulse.com/feed/feature.photos.php?jsoncallback=displayFeaturePhotos&featuretype=" + featuretype + "&feedoffset=" + feedoffset;
		script.type = 'text/javascript';
		document.body.appendChild(script);
	} else {
		var URL='/feed/include/feature-topic.inc.php?featurecat=' + featurecat + '&featuretype=' + featuretype + '&feedoffset=' + feedoffset + '&object_id=' + object_id + '&object_type=' + object_type + '&Artist=' + Artist + '&ArtistDir=' + ArtistDir + '&Photopath=' + Photopath + '&random=' + rand_no;
		jQuery.get(URL,function(data){
			data+="<div style='clear: both;'></div>\n";
			data=data.replace(/-->/,'');
			data=data.replace(/<!--/,'');
			jQuery('#' + featureid).html(data);
		});
	};

	if (feedlimit > 0) {
		data="<img id=\"" + FeatureControlsIDprev + "\" onmouseover=\"jQuery('#" + FeatureControlsIDprev + "').addClass('highlight');\" onmouseout=\"jQuery('#" + FeatureControlsIDprev + "').removeClass('highlight');\" onclick=\"prevfeature('" + featurecat + "','" + featureid + "','" + featuretype + "','" + featureuid + "','" + feedlimit + "','" + feedoffset + "');\" src='http://images.starpulse.com/images/left.gif' alt='previous' />\n";
		data+="<img id=\"" + FeatureControlsIDnext + "\" onmouseover=\"jQuery('#" + FeatureControlsIDnext + "').addClass('highlight');\" onmouseout=\"jQuery('#" + FeatureControlsIDnext + "').removeClass('highlight');\" onclick=\"nextfeature('" + featurecat + "','" + featureid + "','" + featuretype + "','" + featureuid + "','" + feedlimit + "','" + feedoffset + "');\" src='http://images.starpulse.com/images/right.gif' alt='next' />\n";
	} else {
		data='';
	};

	jQuery('#' + FeatureControlsID).html(data);

	if (feedlimit == -1) {
		auto_timer=setTimeout(function(){showfeature(featurecat,featureid,featuretype,featureuid,feedlimit,feedoffset)}, 15000);
	};
};

function prevfeature(featurecat,featureid,featuretype,featureuid,feedlimit,feedoffset) {
	feedoffset--;
	if (feedoffset < 0) { feedoffset=feedlimit; };
	showfeature(featurecat,featureid,featuretype,featureuid,feedlimit,feedoffset);
};

function nextfeature(featurecat,featureid,featuretype,featureuid,feedlimit,feedoffset) {
	feedoffset++;
	if (feedoffset > feedlimit) { feedoffset=0; };
	showfeature(featurecat,featureid,featuretype,featureuid,feedlimit,feedoffset);
};

function checkImage(imagesource) {
	imagesource.onerror = "";
	imagesource.src="http://images.starpulse.com/members/images/genericavatar.jpg";
	return true;
}

function resizeimage(image,maxheight) {
        if (image.height > maxheight) {
                image.height=maxheight;
        }
}

