$(document).ready(function(){
	$('#most_popular_more').bind('click', function() {
		url = 'most-popular';
		redirectUrl(url);
	}); 
}); 

var sShareon = '';
var sTitle = '';// for showing
var sOrinialTitle = '';// for original title 
var sTitleUrl = '';// for url
var sTweetMsg = ''; // twitter message must be less than 90 char
var contentId = '';// content for everything
var sContentType = '';// Content Type for everything news/updates/videos/photos

var contentMsg = '';
var filename = '';
var urlToShare = ''; // url to share  

var isBitlyUrl = false;
var sShortenURL = ''; // url to share  

// showUpdatesForHandle - this function is used for open the location for passed parameter star
function showUpdatesForHandle(starHandle){
	var sStarUpdateUrl =  bollywood_updates_url; 
	starHandleDashed = replaceSpacesWithDash(starHandle);
	url = sStarUpdateUrl + '/twitter/' + starHandleDashed;  
	redirectUrl(url);
}


// viewMoreNews - this function is used for open the location for passed parameter news title
function viewMoreNews(titleId){ 
	sTitleTmp = document.getElementById(titleId).innerHTML;
	sDate = document.getElementById('date_'+titleId).value;
	if (sDate==''){
		sDate = current_date.replace(/-/g, '/');
	} 
	sTitleUrl  = removeSpecialChar(sTitleTmp);
 
	var sTitleTmpLength = sTitleTmp.length;

	sTweetMsg = sTitleTmp; // twitter message
	if (sTitleTmpLength > 90){
		sTweetMsg = sTitleTmp.substring(0,90); // for twitter not more than 90 char
	}
	 
	sTitle = replaceSpacesWithDash(sTitleUrl);
	var tmpContentId = titleId.split('_');// titleId is combination of content id and position showing
	var ContentId =  tmpContentId[0]; // ContentId to share
	url =  bollywood_news_url + '/news/' + sDate + '/' + sTitle + '/' + ContentId;  
	redirectUrl(url);
}

// viewMoreMovieReview - this function is used for open the location for passed parameter news title
function viewMoreMovieReview(titleId){ 
	sTitleTmp = document.getElementById(titleId).innerHTML; 
	sTitleUrl  = removeSpecialChar(sTitleTmp); 
	sTitle = replaceSpacesWithDash(sTitleUrl); 
	var tmpContentId = titleId.split('_');// titleId is combination of content id and position showing
	var ContentId =  tmpContentId[0]; // ContentId to share
	url = movie_reviews + '/' + sTitle + '/' + ContentId; 
	redirectUrl(url);
}

// viewArtistPhoto - this function is used for open the location for passed parameter news title
function viewArtistPhoto(titleId){ 
	sTitleTmp = document.getElementById(titleId).innerHTML; 
	sTitleUrl  = removeSpecialChar(sTitleTmp); 
	sTitle = replaceSpacesWithDash(sTitleUrl); 
	var tmpContentId = titleId.split('_');// titleId is combination of content id and position showing
	var ContentId =  tmpContentId[0]; // ContentId to share
	if (artist_name.length > 0){ }
	else { 
		artist_name = '/'+sTitle;
	} 
	url = pages + artist_name + '/photo-view/' + sTitle + '/' + ContentId; 
	redirectUrl(url);
}

// viewEventsJustinPhoto - this function is used for open the location for passed parameter 
function viewEventsJustinPhoto(titleId, category, ContentId){ 
	sTitleTmp = document.getElementById(titleId).innerHTML; 
	sTitleUrl  = removeSpecialChar(sTitleTmp); 
	sTitle = replaceSpacesWithDash(sTitleUrl);  
	var tmpContentId = titleId.split('_');// titleId is combination of content id and position showing
	if (ContentId==null){
		var ContentId =  tmpContentId[0]; // ContentId to share 
	}
	url =  photos + '/' + category + '/' + sTitle + '/' + ContentId; 
	redirectUrl(url);
}

// viewMoreNewsHeadline - this function is used for open the location for passed parameter news title
function viewMoreNewsHeadline(titleId, type){
	sDate='';
	sTitleTmp = document.getElementById(titleId).innerHTML;
	try{
		sDate = document.getElementById('date_'+titleId).value;
	}
	catch (e){
	}
	if (sDate==''){
		sDate = current_date.replace(/-/g, '/');
	} 
	sTitleUrl  = removeSpecialChar(sTitleTmp); 
	var sTitleTmpLength = sTitleTmp.length; 
	sTweetMsg = sTitleTmp; // twitter message
	if (sTitleTmpLength > 90){
		sTweetMsg = sTitleTmp.substring(0,90); // for twitter not more than 90 char
	}

	sTitle = replaceSpacesWithDash(sTitleUrl);
	var tmpContentId = titleId.split('_');// titleId is combination of content id and position showing
	var ContentId =  tmpContentId[0]; // ContentId to share
	
	switch (type){
		case 'news':
			url =  bollywood_news_url + '/' + type + '/' + sDate + '/' + sTitle + '/' + ContentId;  		
		break;

		case 'artist-news-view': 
			url =  pages + artist_name + '/news-view/' + ContentId;
		break;
		
		case 'movies-news-view': 
			url =  movies + movie_name + '_' + movie_id + '/news-view/' + ContentId;
		break;
		
		case 'movies-photo-view': 
			url =  movies + movie_name + movieid_separator + movie_id + '/photo-view/' + sTitle + '/'+ ContentId;
		break;
		
		case '':
		
		break;
	}
	 
	redirectUrl(url);
}

// shareBollywoodji - content on social network site
function shareBollywoodji(shareon, titleId, type){
	contentId = titleId;
	sShareon  = shareon;
	sContentType = type;
	var sTitleTmp = '';
	var sDate = '';
	var starHandle = ''
	if (type=='updates'){// for star update getting message and star name @ star name is used in url, message for message
		starHandle = document.getElementById('starHandle_'+titleId).value; //getting star handle for message
		var tmpMsg = document.getElementById('msg_'+titleId).innerHTML;
		sTitleTmp = 'RT @' + starHandle + ' ' + tmpMsg; //getting message for message
		sTitleUrl = document.getElementById('starName_'+titleId).value; //getting star name for url  
		sOrinialTitle = sTitleUrl; // title as showing
		contentMsg = tmpMsg; // message as showing
	}
	else { 
		sTitleTmp = document.getElementById(titleId).innerHTML;
		try{
			sDate = document.getElementById('date_'+titleId).value;
			if (sDate==''){
				sDate = current_date.replace(/-/g, '/');
			}
		}
		catch(err){
			sDate ='';
		}
		var tmpMsg = document.getElementById('msg_'+titleId).innerHTML; 
		sTitleUrl  = removeSpecialChar(sTitleTmp);		
		sOrinialTitle = sTitleUrl; // title as showing
		contentMsg = tmpMsg; // message as showing
	} 
	
	var sTitleTmpLength = sTitleTmp.length;
	
	sTweetMsg = sTitleTmp; // twitter message
	
	if (sTitleTmpLength > 90){
		sTweetMsg = sTitleTmp.substring(0,90); // for twitter not more than 90 char
	}
	 
	sTitle = replaceSpacesWithDash(removeSpecialChar(sTitleUrl));

	var urlToShort = '';
	var tmpContentId = titleId.split('_');// titleId is combination of content id and position showing		
	var ContentId =  tmpContentId[0]; // ContentId to share
	
	var artistName =  replaceSpacesWithDash(artist_name); // replace msg name with 
			
	switch (type){
		case 'news':
			urlToShort =  bollywood_news_url + '/news/' + sDate + '/' + sTitle + '/' + ContentId; 
		break;
		
		case 'movie-reviews':
			urlToShort =  movie_reviews + '/' + sTitle + '/' + ContentId; 
		break;

		case 'artist-news':// artist news share
			urlToShort =  pages + artistName + '/news-view/' + ContentId; 
		break;
		case 'artist-photos': // artist photo share
			urlToShort = urlToShort + pages + artistName + '/photo-view/' + sTitle + '/' + ContentId;
		break;
		case 'artist-songs':// artist songs share
			urlToShort = urlToShort + pages + artistName + '/song-view/' + ContentId; 
		break;
		case 'artist-movies':// artist movies share
			urlToShort = urlToShort + pages + artistName + '/movie-view/' + ContentId;  
		break;
		
		case 'movie-news':// movie news share
			urlToShort =  movies + movie_name + '_' + movie_id + '/news-view/' + ContentId;
		break; 
		
		case 'movie-photos':// movie photos share
			urlToShort =  movies + movie_name + '_' + movie_id + '/photo-view/' + sTitle + '/' + ContentId;
		break; 
		
		case 'movie-movie-reviews':// movie movie reviews share
			urlToShort =  movies + movie_name + '_' + movie_id + '/movie-reviews';
		break; 
		
		case 'movie-songs':// movie songs share
			urlToShort =  movies + movie_name + '_' + movie_id + '/songs';
		break;
		
		case 'events-view': //events view share 
		case 'justin-view': //justin view share 
			urlToShort =  photos + '/' + type + '/' + sTitle + '/' +  ContentId; 
		break;
		
		case 'video-videos': //share video
			urlToShort =  'videos/play/' + sTitle + '/' +  ContentId; 
		break;
		
		case 'updates':
			urlToShort = urlToShort + bollywood_updates_url + '/twitter/' + sTitle;
		break;			
	}
  
	urlToShare = urlToShort; 
	shareBollywoodjiFile();// calling shareBollywoodjiFile 
}

// share bollywoodji on buzz and facebook


function shareBollywoodjiFile(){ 
	var urlShare = web_url + urlToShare;
 
	//window.open("http://www.google.com/","help","status = 0, height = 415, width = 600, resizable = 0, scrollbars=0");return true;
	if (sShareon=='buzz'){ 
		var urlTmp = "http://www.google.com/buzz/post?" + "url=" + Url.encode(urlShare); 
		popup(urlTmp, sContentType); 		
	}
	else if (sShareon=='facebook'){ 
		var urlTmp = "http://facebook.com/sharer.php?" + "u=" + urlShare; 
		popup(urlTmp, sContentType); 		
	}
	else if (sShareon=='email' && (sContentType=='news'  || sContentType=='artist-news'  || sContentType=='movie-reviews')){ // email for news, artist-news
		var urlTmp =  'sendnewsemail.php?action=send&newsurl=' + urlShare + '&contentid=' + contentId + '&contenttype=' + sContentType ;
		popupSend(urlTmp, sContentType); 		
	}
	else if (sShareon=='email' && (sContentType=='updates' || sContentType=='artist-photos' || sContentType=='artist-songs' || sContentType=='artist-movies' || sContentType=='video-videos')){// email for udpdate, artis-photos, artist-songs
		var urlTmp = 'sendnewsemail.php?action=send&newsurl=' + urlShare + '&contentid=' + contentId + '&contenttype=' + sContentType + '&title=' + Url.encode(sOrinialTitle) + '&message=' + Url.encode(contentMsg); 
		
		//alert(urlTmp + "\n\n" + sContentType);//return false;
		
		popupSend(urlTmp, sContentType); 		
	}	
	else if (sShareon=='tweets'){
		var tmpurl = web_url + 'process_ajax.php';
		var dataPost = 'action=getshortenurl&urltoshort=' + web_url + urlToShare;  
	 
		$.ajax({ url: tmpurl, data:dataPost, async:false, success: function(text){
			if (text.length>0){
				if (text.length>0){ 
					sShortenURL = text;
					isBitlyUrl = true;
				}
			}
		}});
			
		if (isBitlyUrl){ 
			var url = sTweetMsg + ' ' +  sShortenURL; 
			try{
				if (tweet_key_word.length > 0){
					var url = sTweetMsg + ' ' + sShortenURL + ' #' + tweet_key_word; // twitter message
				}
			}
			catch (err){
			}
			  
			var urlEncode = Url.encode(url); 
			var urlTmp = "http://twitter.com/home?" + "status=" + urlEncode;
			sShortenURL = '';
			isBitlyUrl = false;
			popup(urlTmp, sContentType);  
		}  
	} 
}

//open window for sharing the content
function popup(url, name){
	var openPopup = window.open(url, 'sharing_content', "status = 0, height = 415, width = 600, resizable = 0, scrollbars = 0, left = 10, top = 50 "); 
}

//open window for sending content to friends
function popupSend(url, name){ 
	url = web_url + url;
	var openPopup = window.open(url, 'sharing_content', "status = 0, height = 415, width = 670, resizable = 0, scrollbars = 0, left = 10, top = 50 "); 
}

//function to add email id to email_address table - for news
function subscribeNews(){
	var errorId = 'emailIdStatus';
	document.getElementById('emailIdStatus').style.visibility = 'hidden';
	var emailVal = document.getElementById('emailId').value;
	var emailValClean = removeSpaces(emailVal);
	var emailValLen = emailValClean.length;
	if (emailValLen > 0){
		var isValidEmail = validateEmailId(emailValClean);
		document.getElementById('emailIdStatus').style.visibility = 'visible';
		if (isValidEmail==false){
			var txt = '<span style="color:red;">Please enter valid email address</span>'; 
			txtId = 'emailId';
			showError(txt, errorId, txtId);  
		}
		else { 
			txt = '<img src="' + web_url + 'images/ajax-loader-ns.gif"/ border="0">';
			showError(txt, errorId, '');
			var tmpurl = web_url + 'process_ajax.php';
			var dataPost = 'action=subscribenews&emailid=' + emailValClean; 
			$.ajax({ url: tmpurl, data:dataPost, async:false, success: function(text){
				if (text.length>0){
					if (text=='Try after sometime'){
						var txt = '<span style="color:red;">' + text + '</span>';
					}
					else {
						var txt = '<span style="color:green;">' + text + '</span>';
					}
					showError(txt, errorId, '');
				} 
			}});
		}
	} 
	return false;
}

//function is used for redirecting the url
function redirectUrl(url){
	window.location = web_url + url;
}

// assign Like url to all link
function assignLike (id_pos){
	var aIdPos = id_pos.split('_');  
	titleId = id_pos;
	posi = aIdPos[1];
	likeId = aIdPos[0];
	likeUrlContent = document.getElementById(titleId).innerHTML;
	likeDate = document.getElementById('date_'+titleId).value;
	if (likeDate==''){
		likeDate = current_date.replace(/-/g, '/');
	} 
	likeUrlContent  = removeSpecialChar(likeUrlContent);
	likeUrlContent = replaceSpacesWithDash(likeUrlContent); 
	var TmpUrlLike = faceBookLikeUrl  + web_url +  'bollywood/news/' + likeDate + '/' + likeUrlContent + '/' + likeId + faceBookLikeOptions;
	document.getElementById('like_' + posi + '_' + likeId).src = TmpUrlLike;
 
}

// assign link to view photo url
function assignLink2View3(phid, type, action){
	var ContentId = phid;

	var photoName = document.getElementById('photo_name_' + phid).innerHTML;
	sPhotoTitleUrl  = removeSpecialChar(photoName);
	sPhotoTitle = replaceSpacesWithDash(sPhotoTitleUrl); 
	
	switch (type){
		case pages: 
			if (action=='photo-view3-feature'){
				sTitleTmp = document.getElementById(phid).innerHTML; 
				sTitleUrl  = removeSpecialChar(sTitleTmp); 
				sTitle = replaceSpacesWithDash(sTitleUrl); 
				var tmpContentId = phid.split('_');// phid is combination of content id and position showing
				var ContentId =  tmpContentId[0]; // ContentId to share
				artist_name = '/'+sTitle; 
				action = 'photo-view3';
			}
			if (action=='events-view3' || action=='justin-view3' ){
				sub_url = photos;
			} 
			else {
				sub_url = pages;
			}
			
			var tmpUrl = web_url + sub_url + artist_name + '/' + action + '/' + sPhotoTitle + '/' + ContentId;
		break;
		
		case movies:
			if (action=='photo-view3'){
				sub_url = movies;
			} 		
			var tmpUrl = web_url + sub_url + movie_name + movieid_separator + movie_id + '/' + action + '/' + sPhotoTitle + '/' + ContentId;
		break;
	}
	
	document.getElementById('view3_' + phid).href = tmpUrl; 
	if (document.getElementById('view3_1_' + phid)){
		document.getElementById('view3_1_' + phid).href = tmpUrl; 
	}
	
}

// assign link to movie names - actors movie's page url
function assignLink2MovieName(mvid, mvidpos){
	var ContentId = mvid; 
	var movieName = document.getElementById('msg_' + mvidpos).innerHTML;
	sMovieNameUrl  = removeSpecialChar(movieName);
	sMovieName = replaceSpacesWithDash(sMovieNameUrl);
	var tmpUrl = web_url + movies + '/' + sMovieName + '_' + ContentId;
	document.getElementById('msg_' + mvidpos).innerHTML = "<a href='" + tmpUrl + "'>" + movieName + "</a>";
}


//function for set the cookie for movie
function setCookiesId(Id){
	setCookie('movieId',Id);
}

// assign link to most popular view photo url
function assignLink2View4(phid, type){ 
	var tmpContentId = phid.split('_');// titleId is combination of content id and position showing
	var ContentId =  tmpContentId[0]; // ContentId to share
	if (type=='mp_photo'){
		var photoName = document.getElementById(phid).innerHTML;
		sPhotoTitleUrl  = removeSpecialChar(photoName);
		sPhotoTitle = replaceSpacesWithDash(sPhotoTitleUrl); 
		var tmpUrl = web_url + photos + '/justin-view/' + sPhotoTitle + '/' + ContentId;
			 
		document.getElementById('view_photo_' + phid).href = tmpUrl; 
		document.getElementById('view_name_' + phid).href = tmpUrl;  
	}
	else if (type=='mp_photo-more'){ 
		redirectUrl(photos);	
	}
}


// assign link to most popular view video url
function assignLink2View5(viid, type){ 
	var tmpContentId = viid.split('_');// titleId is combination of content id and position showing
	var ContentId =  tmpContentId[0]; // ContentId to share
	if (type=='mp_video'){
		var videoName = document.getElementById('video_'+viid).innerHTML; 
		sVideoTitleUrl  = removeSpecialChar(videoName);
		sVideoTitle = replaceSpacesWithDash(sVideoTitleUrl); 
		var tmpUrl = web_url + 'videos/play/' + sVideoTitle + '/' + ContentId;
			 
		document.getElementById('view_video_' + viid).href = tmpUrl; 
		document.getElementById('view_video_name_' + viid).href = tmpUrl;  
	}
	else if (type=='mp_video-more'){ 
		redirectUrl('videos');	
	}
}
