var ads = {
	init : function() {
		ads.positionAds();
	},
	
	positionAds : function() {
		if ($('#mpu-1').length){
			document.getElementById("mpu-1").innerHTML = document.getElementById("mpu-content").innerHTML;
			$("#mpu-content").remove();
		}
		
		if ($('#mpu-2').length){
			document.getElementById("mpu-2").innerHTML = document.getElementById("mpu-content-2").innerHTML;
			$("#mpu-content-2").remove();
		}		
		
		if ($('#leaderboard').length) {
			document.getElementById("leaderboard").innerHTML = document.getElementById("leaderboard-content").innerHTML;
			$("#leaderboard-content").remove();
		}
		
		if ($('#adsense').length) {
			document.getElementById("adsense").innerHTML = document.getElementById("adsense-content").innerHTML;
			$("#adsense-content").remove();
		}		
		
	}			
}

jQuery.fn.ratings = function(stars, initialRating) {
  var elements = this;//Save  the jQuery object for later use.
  //Go through each object in the selector and create a ratings control.
  return this.each(function() {
    //Make sure intialRating is set.
    if(!initialRating)
      initialRating = 0;
    var containerElement = this;  //Save the current element for later use.
    var container = jQuery(this); //grab the jQuery object for the current container div
    var starsCollection = Array();//Create an array of stars so they can be referenced again.
   
    containerElement.rating = initialRating;//Save the initial rating.   
    //create each star
    for(var starIdx = 0; starIdx < stars; starIdx++) {
     
      var starElement = document.createElement('li');//Create a div to hold the star.
      var star = jQuery(starElement);//Get a jQuery object for this star.
      starElement.rating = starIdx + 1;//Store the rating that represents this star.
      star.addClass('jquery-ratings-star');//Add the style.     
	  $(star).attr('id', "star" + starIdx)
	  
	  //Add the full css class if the star is beneath the initial rating.
      if(starIdx < initialRating) {
        star.addClass('jquery-ratings-full');
      }
      //add the star to the container
      container.append(star);
      starsCollection.push(star);
      //hook up the click event
      star.click(function() {
		$('#ratingoptions option').removeAttr('selected');
		var currentStar = $(this).attr('id');
		var startPos = currentStar.indexOf("star");
		var restOfString = currentStar.substr(startPos +4);
		var starNumb = parseInt(restOfString);
		var blah = $("#ratingoptions ." + starNumb);
		$("#ratingoptions ." + starNumb).attr('selected', 'selected')
		containerElement.rating = this.rating; //set the containers rating
        elements.triggerHandler("ratingchanged", {rating: this.rating});//When clicked, fire the 'ratingchanged' event handler.  //Pass the rating through as the data argument.
      });
     
      star.mouseenter(function() {
        //Highlight selected stars.
        for(var index = 0; index < this.rating; index++) {
          starsCollection[index].addClass('jquery-ratings-full');
        }
        //Unhighlight unselected stars.
        for(var index = this.rating; index < stars; index++) {
          starsCollection[index].removeClass('jquery-ratings-full');
        }
      });
     
      container.mouseleave(function() {
        //Highlight selected stars.
        for(var index = 0; index < containerElement.rating; index++) {
          starsCollection[index].addClass('jquery-ratings-full');
        }
        //Unhighlight unselected stars.
        for(var index = containerElement.rating; index < stars ; index++) {
          starsCollection[index].removeClass('jquery-ratings-full');
        }
      });
	  
    }
  });
};

/*------- Email Article Form ----------*/
			
var articleEmailForm = {

	formVisibility: function () {
		$('#emailbutton').attr('href', '#TB_inline?height=450&width=375&inlineId=article-email-wrapper').click(function(e) {
			$('#article-email').css({display: "block"})
			
		})
		
		$('#bookmarkbutton').attr('href', '#TB_inline?height=250&width=200&inlineId=socialbookmarking').click(function(e) {
			$('#socialbookmarking').css({display: "block"})
		});
		
		$('#shareicons ul').eq(0).append('<li class=""><a href="" onclick="window.print(); return false" id="printbutton"><span class="hide">Print</span></a></li>')
	},				
	
	// Validation rules
	emailFormValidation: function () {
		if(!document.getElementById("article-email")) {
			return false;
		}
		$("#article-email").RSV({
			//onCompleteHandler: myOnComplete,
			displayType: "display-html",
			errorTextIntro: "Please correct the following errors:",
			errorFieldClass: "errorfield",
			rules: [
				"required,recipientemail,Enter an email address for the recipient",
				"valid_email,recipientemail,Enter a valid email address for the recipient",		
				"required,youremail,Enter an email address for the sender",
				"valid_email,youremail,Enter a valid email address for the sender",		
				"required,name,Enter your name",
				"length<1001,message,The message must be no longer than 1000 characters"
			]
		});
	},
	
	//Submit eamilarticle form
	submitEmailArticleForm: function () {
		if(!document.getElementById("article-email")) {
			return false;
		}
		$('#article-email div.js-button-container').click(function(e) {
			e.preventDefault();
			$('#article-email').submit();
		});
	}
};
		
		
$(document).ready(function() {
						   
	/*$("#towndesc").after('<div class="clearing m-top"><a href="#" id="expand" style="font-size:1.2em; float:right; padding-right:20px;">Expand text</a></div>');
	$("#towndesc").css({'overflow' : 'hidden', 'height' : '4.8em'});*/
	
	$("#expand").click(function(){
		if($("#towndesc").css("overflow") == "hidden")
		{
			$("#towndesc").css({'overflow' : 'auto', 'height' : 'auto'});
			$("#expand").text("Hide text");
		}
		else
		{
			$("#towndesc").css({'overflow' : 'hidden', 'height' : '4.8em'});
			$("#expand").text("Expand text");
		}
		
		return false;
	});
						   
	$('#ratingform #starrating').append('<ul class="starrating-stars" id="starrating-stars"></ul>');
	$('#comment-form #commentstarrating').append('<p class="rate-text">Rate this business</p><ul class="starrating-stars" id="comment-starrating-stars"></ul>');
	
	$("#ratingform").append("<input type='hidden' name='submitrating' value='rate'>");
	$("#comment-form").append("<input type='hidden' name='submitrating' value='rate'>");	

	$('#ratingform #starrating-stars').ratings(5).bind('ratingchanged', function(event, data) {
    	$('#ratingform #starrating-text').text(data.rating);
	});
	
	$('#comment-form #comment-starrating-stars').ratings(5).bind('ratingchanged', function(event, data) {
    	$('#comment-form #starrating-text').text(data.rating);
	});
	
	$("#ratingform #starrating-stars li").click(function(){
			document.ratingform.submit();
	});
	
	$(".city-tabs li").click(function(){
		$(".city-tabs li").removeClass("current");
		$(this).addClass("current");
	});	
	
	$(".panes h3").hide();
	$(".panes").show();
	ads.init();
	articleEmailForm.formVisibility();
	

	var vrules = [
				"required,recipientemail,Enter an email address for the recipient",
				"valid_email,recipientemail,Enter a valid email address for the recipient",		
				"required,youremail,Enter an email address for the sender",
				"valid_email,youremail,Enter a valid email address for the sender",		
				"required,name,Enter your name",
				"length<1001,message,The message must be no longer than 1000 characters"
				];
	
	validation.init(vrules, "article-email");

});
		