// JavaScript Document
// Remove Tinymce
/*
Ka.Settings.tinyMCE.enabled = false;
Ka.events.listen('global-after-tinymce-init', function(inst){
    setTimeout(function(){ tinyMCE.editors[inst.editorId].remove()}, 200);
});
*/















$j(document).ready(function() {
	$j('#ka_mainContainer').show(); 
});



function getProfile(username){
	//alert('getProfile username:'+username);
	
	//call ajax
	var url = 'http://dev.kickapps.com/shared/ajax/getName.php?callback=?';
	$j.ajax({
		dataType: 'jsonp',
		data: {as:Ka.Info.AFFILIATESITEID, username:username},
		jsonp: 'getProfileCallback',
		url: url,
		success: getProfileCallback
	});
}

function getProfileCallback(json){
	//alert('getProfileCallback status:'+json.status+' error:'+json.error);
	if(json.status=='1' && json.error==''){
		var pathToPhoto = urldecode(json.pathToPhoto);		
		
		$j('#profilePhoto').html('<img src="'+pathToPhoto+'" width="48" />');
	}
}

var currentPage = 1;
function getBlog(containerId){
	//alert('getBlog:'+containerId);
	var keywords = getUrlValue('keywords');
	//alert(keywords);
	
	$j('#blogLoading').show();
	
	//call ajax to retrieve blogs
	var url = 'http://dev.kickapps.com/prevacid/ajax/get-blogs.php?callback=?';
	$j.ajax({
		dataType: 'jsonp',
		data: {quantity:5, page:currentPage, keywords:keywords, containerId:containerId},
		jsonp: 'getBlogCallback',
		url: url,
		success: getBlogCallback
	}); 
}

function getBlogCallback(json){
	//alert('getBlogCallback status:'+json.status+' error:'+json.error+' containerId:'+json.containerId+' feedHtml:'+urldecode(json.feedHtml)+' itemCount:'+json.itemCount+' page:'+json.page+' rssUrl:'+urldecode(json.rssUrl));
	
	$j('#blogLoading').hide();
	
	if(json.status=='1' && json.error==''){
		var itemCount = new Number(json.itemCount);
		if(itemCount > 0){
			var feedHtml = urldecode(json.feedHtml);
			$j('#'+json.containerId).append(feedHtml);
			
			if(itemCount<5){
				$j('#shareyours').hide();
			}
		}
		else{
			if(currentPage==1){
				$j('#'+json.containerId).append('No results found.');
				$j('#browseall').show();
			}
			else{
				$j('#'+json.containerId).append('No more blogs.');
			}
			$j('#shareyours').hide();
		}
	}
	
	currentPage++;
}

function onUploadPhoto(){
	var name = $j('#name').val();
	var mediaDescriptionRequired = getTinyMceValue('mediaDescriptionRequired');
	//alert('name:'+name+' mediaDescriptionRequired:'+mediaDescriptionRequired);
	
	createCookieSS('name',escape(name),1);
	createCookieSS('mediaDescriptionRequired',escape(mediaDescriptionRequired),1);
		
	//window.location.href='/service/displayAddProfileIcon.kickAction';
	
	//initialize colorbox
	$j.fn.colorbox.init();
	
	$j.fn.colorbox({iframe:true, href:"http://www.google.com", transition:"elastic", speed:0, scrollbars:false, initialWidth:"600", initialHeight:"400", width:"600", height:"400", open:true, overlayClose:true, opacity:0.7});	
	
	//$j.fn.colorbox({iframe:true, title:"", href:"/service/displayAddProfileIcon.kickAction", transition:"elastic", speed:0, scrollbars:false, initialWidth:"600", initialHeight:"400", width:"600", height:"400", open:true, overlayClose:true, opacity:0.7});	
}


function populateBlogPost(){
	if(readCookieSS('name')!=null && readCookieSS('name')!=''){
	  var name = readCookieSS('name');
	  $j('#name').val(unescape(name));
	}
	if(readCookieSS('mediaDescriptionRequired')!=null && readCookieSS('mediaDescriptionRequired')!=''){
	  var mediaDescriptionRequired = unescape(readCookieSS('mediaDescriptionRequired'));
	  //alert(mediaDescriptionRequired);
	  document.getElementById('mediaDescriptionRequired').value = mediaDescriptionRequired;
	  //setTinyMceValue('mediaDescriptionRequired',escape(mediaDescriptionRequired));
	}	
}

//individual blog play page
if (Ka.Info.PAGE == "pages/mediaPlayPage.jsp") {
	$j('#ka_playPageDetails').insertAfter('#ka_contentContainer h2');
	$j('#ka_upByTitle').hide();
	$j('#ka_tagsWrap').appendTo('#ka_uploadedby');
	
	

	$j('#ka_rightColumn').prepend('<div id="addblogpost"><a class="addblogbt" href="/view/displayAddBlog.kickAction?as=132865&nextPage=blogUpload">Share Your Story</a></div><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="240" height="318" id="kickWidget_132865_297346" ><param name="movie" value="http://serve.a-widget.com/service/getWidgetSwf.kickAction"></param><param name="FlashVars" value="affiliateSiteId=132865&amp;widgetId=297346&amp;width=240&amp;height=318&amp;revision=19" ></param><param name="wmode" value="transparent" ></param><param name="allowFullScreen" value="true" ></param><param name="allowScriptAccess" value="always" ></param><embed src="http://serve.a-widget.com/service/getWidgetSwf.kickAction" name="kickWidget_132865_297346" width="240" height="318" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" allowScriptAccess="always" allowFullScreen="true" FlashVars="affiliateSiteId=132865&amp;widgetId=297346&amp;width=240&amp;height=318&amp;revision=19"></embed></object> <h4 id="keywordheader"> BROWSE STORIES BY KEYWORDS </h4>');
	$j('#ka_tagCloud').show().insertAfter('#keywordheader');
	$j('#ka_headerSearch').insertBefore('#addblogpost');

	
	$j('h3.ka_comments').html('Add your comment');
	$j('.ka_stats_comments').prependTo('#ka_playPageStats');
	$j('#ka_commentLog').prepend('<h3 class="ka_comments">comments</h3>');
	$j('#ka_playPageStats').after('<div id="loginpost" style="display:none;"><p>To rate or add a comment to this story please <a class="signinOpen" href="javascript:;">sign in</a>.</p><div class="commentLine"/></div>');
	$j('#ka_playPageStats').after('<div class="commentLine"/>');
	$j('#ka_commentsNumArea').appendTo('#ka_playPageStats').append(' comments');
	$j('#ka_shoutBoxContainer').append('<div id="nextPrev"><div style="padding-top: 10px;"><a href="/view/displayAddBlog.kickAction?as=132865&amp;nextPage=blogUpload" class="addblogbt">Share Your Story</a></div><br/><a title="Go Back" class="ro" href="/service/searchEverything.kickAction?as=132865&mediaType=blog&sortType=recent"><img border="0" alt="Go Back" src="http://novartis.chi.tribalddb.com/Prevacid24/images/buttons/go-back.gif"/></a></div>');
	
	//make image larger
	$j('#ka_upImageWrap span').each(function() {
        var $this = $j(this);
        var newStyle = $this.attr('style').replace('48X48', '100X75', 'ig');
        $this.attr('style', newStyle);
    });
	
	//if user is not logged in
	if (Ka.Info.USERID == '') {
		$j('#loginpost').show();
		$j('h3.ka_comments.ka_sprite_misc').hide();
		$j('#ka_shoutBoxArea').hide();
	}
	
	//if pr24hr user
	if (Ka.Info.USERID == '12486965') {
		$j('#ka_tagsInput').show();
	}
	
	$j('#ka_upImageWrap a').attr('href', 'javascript:void(0);');
	$j('#ka_upImageWrap a').attr('disabled', 'true');
	$j('#ka_upImageWrap a').css('cursor', 'default');
	
	$j('a.ka_contributorName').attr('href', 'javascript:void(0);').attr('disabled', 'true').css('cursor', 'default');
	$j('.ka_bubble a').attr('href', 'javascript:void(0);').attr('disabled', 'true').css('cursor', 'default');

	
	
}

//list page
if (Ka.Info.PAGE == 'search/searchPage.jsp') {
	$j('#siteInterior').prepend('<h1>Had It with Heartburn<sup>TM</sup>: Your Stories</h1><h1 class="subheadNoMargin"> I\'d had it with heartburn when...</h1><h2>The pains of frequent heartburn can interrupt special moments in your life as well as your everyday activities. Eventually, there comes a time when you stop and confront it, and realize you have to do something about it. And that\'s a <b>Had It with Heartburn</b><sup>TM</sup> moment!</h2><h2><b>Have you had it with heartburn?</b> Well, you\'re not alone. Join the heartburn community from all around the nation of 50 million adult frequent heartburn sufferers. It\'s important talk about it. And hear what others are saying. So go ahead! Share your most challenging moment, or a particularly spectacular triumph over frequent heartburn.</h2><h2>Get inspired by reading other people\'s stories in our gallery.</h2>');
	$j('#ka_contentContainer').html('<div id="ka_leftColumn"><ul id="bloglist"></ul></ul><div id="blogLoading" style="text-align:center; display:none;"><img src="http://dev.kickapps.com/prevacid/images/loading.gif"/></div> <div style="padding-top: 10px;"><a id="browseall" class="ro" href="/service/searchEverything.kickAction?as=132865" style="display:none;"></a> <a id="shareyours" class="ro" href="javascript:getBlog(\'bloglist\');"></a></div>  <div style="padding-top: 10px;"> <a class="addblogbt" href="/view/displayAddBlog.kickAction?as=132865&amp;nextPage=blogUpload">Share Your Story</a> </div> <p style="font-size: 11px; line-height: 12px; color: #777;">The people featured in this section are actual users and heartburn sufferers. Their opinions and results are not necessarily endorsed by Novartis Consumer Health, Inc. </p> <p style="font-size: 11px; line-height: 12px; color: #777;">The content does not necessarily represent the views of Novartis Consumer Health, Inc.</p> </div><div id="ka_rightColumn"><div id="addblogpost"><a href="/view/displayAddBlog.kickAction?as=132865&amp;nextPage=blogUpload" class="addblogbt">Share Your Story</a> </div><object width="240" height="318" id="kickWidget_132865_297346" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"><param value="http://serve.a-widget.com/service/getWidgetSwf.kickAction" name="movie"/><param value="affiliateSiteId=132865&amp;widgetId=297346&amp;width=240&amp;height=318&amp;revision=19" name="FlashVars"/><param value="transparent" name="wmode"/><param value="true" name="allowFullScreen"/><param value="always" name="allowScriptAccess"/><embed width="240" height="318" flashvars="affiliateSiteId=132865&amp;widgetId=297346&amp;width=240&amp;height=318&amp;revision=19" allowfullscreen="true" allowscriptaccess="always" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" name="kickWidget_132865_297346" src="http://serve.a-widget.com/service/getWidgetSwf.kickAction"/></object> <h4 id="keywordheader"> BROWSE STORIES BY KEYWORDS </h4></div>');
	$j('#ka_tagCloud').show().insertAfter('#keywordheader');
	$j('#ka_headerSearch').insertBefore('#addblogpost');
	
	

	//get blog listing
	getBlog('bloglist');
	
	//delete cookie
	eraseCookieSS('name');
	eraseCookieSS('mediaDescriptionRequired');
}

//add blog page
if (Ka.Info.PAGE == "pages/addBlog.jsp") {
	//populate using cookie values
	populateBlogPost();
	
	$j('#ka_iAccept').before('<a id="browseimage" class="example7 cboxElement" href="/service/displayAddProfileIcon.kickAction"></a>');
		
	//$j.fn.colorbox.init();
	$j(".example7").colorbox({width:"600", height:"500", opacity:0.7, iframe:true});

	
	$j('#ka_manageContent h3').html('Share your story').after('<p id="requiredtext">required information</p>');
	$j('input#name').before('<h3 id="blogtitle" class="ka_comments">Title</h3>');
	$j('#ka_descRequired').before('<h3 id="sharetitle" class="ka_comments">Share your story</h3>');
	//$j('#screenShotfileField').before('<div id="profilePhoto"></div> <h3 id="browsetitle" class="ka_comments">Upload a Picture</h3><div><a id="browseimage" href="javascript:void(0);" onclick="onUploadPhoto();"></a></div><p id="textremain">5 MB max.</p>');
	$j('#screenShotfileField').before('<div id="profilePhoto"></div> <h3 id="browsetitle" class="ka_comments">Upload a Picture</h3><div id="browsepic"></div><p id="textremain">5 MB max.</p>');
	$j('#browseimage').prependTo('#browsepic');
	
	$j('#ka_iAccept').insertBefore('#ka_blogSubmit').html('<input id="termsOfUse" type="checkbox" checked="checked" errorkey="termsOfService" required="true" name="termsOfUse"/> I agree to the following Terms and Conditions. <p>Thank you for adding your story! Please be patient as stories take a while to upload. Please know that by submitting your story you are releasing all rights to ownership of it and it can be repurposed any way Novartis Consumer Health, Inc. and Prevacid&reg;24HR wishes to use it.  Thank you.</p>');

	//get path to photo
	getProfile(Ka.Info.USERNAME);

}

//edit account page
if (Ka.Info.PAGE == 'pages/manageAccountSettings.jsp') {
	$j('body').html('');
	parent.location.reload();
}

//my blog post page
if (Ka.Info.PAGE == 'pages/manageBlog.jsp') {
	//window.location.href = '/view/displayAddBlog.kickAction?as=132865&nextPage=blogUpload';
	$j('#siteInterior').html('<h1>Congratulations!</h1><h2>Thanks for sharing your story with us. Please be patient while it\'s being processed. In the meantime you can:</h2><div style="padding-top: 10px;"><a class="ro" href="/service/searchEverything.kickAction?as=132865&mediaType=blog&sortType=recent"><img width="181" height="32" border="0" alt="" src="http://dev.kickapps.com/prevacid/images/read_otherstories_green.png"/></a></div>');
}

/*
var el = $j('title');
el.html(el.html().replace(/Prevacid - /ig, "Prevacid&reg;24HR"));
el.html(el.html().replace(/Prevacid -/ig, "Prevacid&reg;24HR"));
el.html(el.html().replace(/Prevacid Blog post/ig, "Prevacid&reg;24HR"));
*/

//var legal = '&reg;24HR';
//$j('title').append(legal);
