/*
Filename : subscribe.js
Purpose : To Manage the Subscription Logic in the Application
Date Created : 17 Aug 2009
Author : Primoris

Functionalities :
- Subscribe as a friend. (user:becomeFriend)
- Subscribe for a Specific News Stream. (publications,blogs,ideas)
- Standard functions for subscribing from Main Page or Channel Page by separating the channelInfo & isFriend  as a global variable

Modification History  :
*/

/*************************************************************************/
//Global Variables : Initialized from respective pages accordingly.
/*************************************************************************/
var isFriend=0;
var channelInfo={channel_id:"",user_id:"",user_name:"",url:""};
var pageType=-1;

/*
1=>Story Page
2=>Blog Page
4=>Idea Page
0=>Channel Page
-1=>Main Page

*/

//To handle Channel Subscription. Will be striped off later

function subscribe(subscribeMode, hideMessage,cbFunction){

	subscribeMain(subscribeMode,0,hideMessage,cbFunction);
		
}

function subscribeMain(subscribeMode, subscribeFor,hideMessage,cbFunction){

	unSubscribeText="Are you sure to unsubscribe ## on this channel?";
	subscribeText="Do you want to subscribe to " + channelInfo.user_name + "'s ## ?";
	
	if(subscribeFor==0) unSubscribeText="Are you sure to unsubscribe to this channel?";
	if(subscribeFor==0) subscribeText='Do you want to subscribe to this channel and become ' + channelInfo.user_name + "'s friend?";

	
	itemTypes=["channel","publications","blogs","","ideas"];

	var msgText;
	
	
	if (hideMessage == true) {
			
			invoke(null,'user:becomeFriend', {type:subscribeMode}, false, function(req){
				
				if (req.responseText == 'success') {
					
					if(typeof(cbFunction)=="function") cbFunction();
					
				}
			});
		return;	
	}

	if(subscribeMode==1){
		msgText=subscribeText.replace("##",itemTypes[subscribeFor]);
	}else{
		msgText=unSubscribeText.replace("##",itemTypes[subscribeFor]);

	}
	
		Message.confirm(msgText, function(){
			if(subscribeMode==1) {
				showProgress("Subscribing to " + channelInfo.user_name + "...", true);
			} else {
				showProgress("Unsubscribing from " + channelInfo.user_name + "...", true);
			}
			invoke(null,'user:becomeFriend', {type:subscribeMode,subscribe_for:subscribeFor}, false, function(req){
				hideProgress();	
				
				if (req.responseText=='success'){
				
					refreshSubscribeLink();	
					if(typeof(cbFunction)=="function") cbFunction();
						
					if(subscribeMode==1) isFriend=1; else isFriend=0;
				}else
				{
					Message.alert(req.responseText);

				}
				
			 }
			);
		
		
		});
	

	
}

function refreshSubscribeLink(){

	if(pageType>=0){ //To refresh the Subscription Button Link except the Main Page
		invoke(null, "channel:subscriptionLink", {std_link:1,channel_id:channelInfo.channel_id,page_type:pageType}, false,function(req){$("channel.menuTabs.subscribeLink").innerHTML=req.responseText;
		
		invoke(null, "channel:subscriptionLink", {std_link:0,size:'big',channel_id:channelInfo.channel_id,page_type:pageType}, false,function(req){$("signUpAreaFollowBox").innerHTML=req.responseText;});
		
		});
		
	}
}


function inviteFriends(vUserId,vType,vKeyId) {

	if(vUserId>0){

		if(vType==1){


			showProgress("Sharing with friends...", true);
			invoke(null, "story:inviteFriends", {story_id:vKeyId}, false, function(req){
				if (req.responseText != "success") {
					hideProgress();
					Message.alert(req.responseText, 'error');
				} else {
					hideProgress();
				}
			});

		}else if(vType==2){


			showProgress("Sharing with friends...", true);
			invoke("mm1", "blog:inviteFriends", {blog_id:vKeyId}, false, function(req){
				if (req.responseText != "success") {
					hideProgress();
					Message.alert(req.responseText, 'error');
				} else {
					hideProgress();
				}
			});

		}else if(vType==3){


			showProgress("Sharing with friends...", true);
			invoke(null, "ideas:inviteFriends", {idea_id:vKeyId}, false, function(req){
				if (req.responseText != "success") {
					hideProgress();
					Message.alert(req.responseText, 'error');
				} else {
					hideProgress();
				}
			});

		}

	}else{
		invoke(null, 'user:saveReturnTo', {returnTo:location.href});
		login($('loginForm'));						
	}
}