var sSpiralURL = "http://users.planningresource.co.uk/";
var sMainSiteURL = "http://www.planningresource.co.uk/";
var sCurrentPageURL = window.location.href;
var sCleanedCurrentPageURL = sCurrentPageURL.replace("#disqus_thread","");

// Add messages to Disqus when logged out, otherwise show the comment box.
jQuery(window).load(function(){ //Ensure disqus is fully loaded and in the dom before we start playing with it
		var checkDisqus = setInterval(function() {
			if (jQuery("#dsq-content").text().length){
		    	clearInterval(checkDisqus);
		    	
	    		 if (jQuery.cookie('CM_LOGGED_IN')) { 
    		         jQuery('#dsq-new-post').removeClass('diq-post-area-hide').css('display','block');
    				 jQuery('#dsq-global-toolbar').show();
    				 jQuery('#dsq-textarea-wrapper').removeClass('dsq-textarea-loading'); //This is a IE bug in IE7 where disqus doesn't remove the class
    				 jQuery('body').removeClass('logged-out');
    		         jQuery('body').addClass('logged-in');
    		         jQuery('.dsq-request-user-info').append('<a href="'+sMainSiteURL+'go/profilepicture">Add or change</a> your picture.<br/>');
    		         jQuery('.dsq-request-user-info').append("Please note: Your name will appear together with your comment. <br />Before commenting, please read our <a href='"+sMainSiteURL+"channel/rulesforcommenting/' target='_blank'>rules for commenting on articles</a>.");
	    		  }
	    		 else{          
    		         jQuery('#dsq-content').before('<h3>Add a comment and tell us your views</h3><p id="login_message">To post a comment you need to be registered and signed in.<br />Please <a href="'+sSpiralURL+'login/?continue=">Sign in</a> or <a href="'+sSpiralURL+'login/?continue=">Register</a>.');
    		         jQuery('#login_message a').attr("href", sSpiralURL+"login/?continue="+ sCleanedCurrentPageURL);
    		         jQuery('body').removeClass('logged-in');
    		         jQuery('body').addClass('logged-out');
    		         jQuery('.dsq-comment-buttons').hide();         
	    		 }
			}
		},250);
});

jQuery(document).ready(function(){
	
	//Update top navigation in header with correct paths and status when logged in and out
	
	if(jQuery.cookie('CM_LOGGED_IN') == null){
		
		jQuery('body').removeClass('logged-in');
		jQuery('body').addClass('logged-out');
		jQuery('#topNav li a[title*="Sign in"]').html('Sign in').attr("title", "Sign in").attr("href", sSpiralURL+"login/?continue="+ sCleanedCurrentPageURL);
		jQuery('#topNav li a[title*="Sign out"]').html('Sign in').attr("title", "Sign in").attr("href", sSpiralURL+"login/?continue="+ sCleanedCurrentPageURL);
		jQuery('#topNav li a[title*="Register"]').html('Register').attr("href", sMainSiteURL+"go/Subscribe/?continue="+ sCleanedCurrentPageURL).parent().attr("id", "register");
		jQuery('#topNav li a[title*="My Account"]').html('Register').attr("title", "Register").attr("href", sMainSiteURL+"go/Subscribe/?continue="+ sCleanedCurrentPageURL).parent().attr("id", "register");
		}
		else if (jQuery.cookie('CM_LOGGED_IN') !== null) {
			
		jQuery('body').removeClass('logged-out');
		jQuery('body').addClass('logged-in');
		jQuery('#topNav li a[title*="Sign in"]').html('Sign out').attr("title", "Sign out").attr("href", sSpiralURL+"logout/?continue="+ sCleanedCurrentPageURL);
		jQuery('#topNav li a[title*="Register"]').html('My Account').attr("title", "My Account").attr("href", sSpiralURL+"account/?continue="+ sCleanedCurrentPageURL).parent().attr("id", "myGP");
	}
	
	//Wordpress specific changes
	 
	jQuery(".userContent").hide(); 
	jQuery(".user a").addClass('authorLink');
	jQuery(".avatar a").removeClass('authorLink');
	
	/* Change Most Commented widget title */
	jQuery("#most-commented-posts h3").html('Most popular');
	
	jQuery(".authorLink").click(function(){
		jQuery(".userContent").hide();
		jQuery(this).next().show();
			return false;
	});	
	
	jQuery('.post:last').addClass('last-post');
	jQuery('.featured:last').addClass('last-post');	
	
	//adds a class 'selected' to Blogs link in main nav so we can style it
	jQuery('#mainNav li a:contains("Blogs")').addClass('selected');
	
	//adds a class 'activationWrap' to Blogs body so we can style it
	if (jQuery('#content').hasClass('activation')) {
		jQuery('body').addClass('activationWrap');
	}
	//adds class to pagination div if there are no pagination links (so as to remove the top & bottom borders)	
	if(jQuery('div.nav-previous:empty').length !=0 && jQuery('div.nav-next:empty').length !=0) {
	    jQuery('div#nav-below').addClass('noBorder')        
	};

	//Removes word 'Tagged:' if there are no tags
	jQuery('span.tags:empty').prev('span.label').hide();
	
	//Removes 'Share' string from featured posts excerpts on home page
	jQuery('span.excerpt').each(function() {
	    var share = jQuery(this);
	    share.text(share.text().substr(6) );
	});
	
	if(jQuery('body').hasClass('logged-out')){
		jQuery('.meta-sep').hide();
		jQuery('.edit-link').hide();
	}
	
	//Creating class name from the blog title and removing spaces
	if(jQuery('#blog-title h2 a').html() == null) {
		var dynamicTitleClass = jQuery('#blog-title h1 a').html().replace(/ /g,'').toLowerCase();
		var dynamicTitleClass2 = dynamicTitleClass.replace(/'/g,'');
		var dynamicTitleClass3 = dynamicTitleClass2.replace(/:/g,'');
		jQuery('#blog-title').addClass(dynamicTitleClass3);
	}
	else {
		var dynamicTitleClass = jQuery('#blog-title h2 a').html().replace(/ /g,'').toLowerCase();
		var dynamicTitleClass2 = dynamicTitleClass.replace(/'/g,'');
		var dynamicTitleClass3 = dynamicTitleClass2.replace(/:/g,'');
		jQuery('#blog-title').addClass(dynamicTitleClass3);
	}

	jQuery('#blog-title').click(function() {
			jQuery(window.location).attr("href", "http://"+window.location.host);

	});
 });
 
