/*
 * sets the bDig javascript class for the bdig website
*/
function ptp()
{
var ptpCoords;
this.doMap = false;
//runs when the main document body is ready to run (uses the jquery framework)
$(document).ready(function(){

	/*
	 * hide all objects with the class hide
	*/
	$('.hide').hide();
	
	/*
	 * sets the about us truncate
	*/
	$('.cvtext').jTruncate({
	    length: 175,
	    minTrail: 50,
	    moreText: 'Read More',
	    lessText: 'Read Less',
	    ellipsisText: '...'
	    });
	
	/*
	 * shows the second row of properties
	*/
	$('#more_properties').click(function (e){
		e.preventDefault();
		//set doSHow to true, becomes false when a row has been shown
		var doShow = true;
		//set the more button to a variabl
		var moreButt = $(this);
		//by default the more button should be hidden after the first row is shown
		var doHide = true;
		//loop through each thumbs row, the first row to be hidden is shown
		$('#home_properties .property_row').each(function(){
			if($(this).is(":hidden")){
				//if doShow is true, no orws have yet been shown
				if(doShow){
					$(this).show("slow");
					$('#less_properties').show();
					doShow = false;
				}
				else{
					//still looping even though a row has been shown, do not hide the more button yet
					doHide = false;
				}
			}
		});
		//if doHide is true, there should be no remaining rows to show, set the more button to hidden
		if(doHide)
			{
			moreButt.hide();
			}
		
	});
	
	/*
	 * Jquery Reverse Plugin
	*/
	jQuery.fn.reverse = function() {
		return this.pushStack(this.get().reverse(), arguments);
	};


	/*
	 * hide the second row of properties
	*/
	$('#less_properties').click(function (e){
		e.preventDefault();
		//count the rows visible after hiding a row
		var visRemainingCount = 0;
		//set the more button to a variabl
		var lessButt = $(this);
		//by default the more button should be hidden after the first row is shown
		var doHide = true;
		//loop through each thumbs row, the first row to be hidden is shown
		$('#home_properties .property_row').reverse().each(function(){
			if($(this).is(":visible")){

				//if doShow is true, no orws have yet been shown
				if(doHide){
					$(this).hide("slow");
					$('#more_properties').show();
					doHide = false;
				}
				else{
					visRemainingCount ++;
				}
			}
		});
		if(visRemainingCount < 2)
			{
			lessButt.hide();
			}
	})
	
	/*
	 * clears the basic search box on click if the content is at default
	*/
	$('#SearchKeywords ').click(function(){
		if($(this).attr('value') == 'area/keyword/postcode')
			{
			$(this).attr('value','');
			}
		});
	
	/*
	 * sets the tiny carousel
	*/
	//first check there enough images to perform a carousel
	if($("#tiny_images ul > li").size() > 1)
		{
		/*
		 * sets the carousel controls
		*/
		function mycarousel_initCallback(carousel) {
			$('#tiny_scroll_left').bind('click', function(e) {
				e.preventDefault();
				carousel.prev();
				return false;
			});

			$('#tiny_scroll_right').bind('click', function(e) {
				e.preventDefault();
				carousel.next();
				return false;
			});
		}
		$('#tiny_images').jcarousel({
			wrap: 'circular',
			initCallback: mycarousel_initCallback,
			scroll: 4,
			visible: 4
			});
		}
	else
		{
		$('#tiny_scroll_left').click(function(e){
			e.preventDefault();
			});
		$('#tiny_scroll_right').click(function(e){
			e.preventDefault();
			});
		}
	
	/*
	 * sets the property detail image swap
	*/
	$('.small_thumbs a').click(function(e){
		e.preventDefault();
		$('#main_property_image').fadeIn(500);
		//get the source file for the image
		var newSource = $(this).attr('href');
		//set the large image to use the old source as the background image
		//$("#main_property_image").css("background-color","#FFFFFF");
		$("#main_property_image").css("background-image", "url("+ newSource +")"); 
		//create an object of the new image
		var newImg = new Image();
		//newImg.src = newSource;
		$(newImg).load(function(){
			$("#main_property_image img").animate({"opacity": "0"}, 400,'',function(){
				$("#main_property_image img").attr("src",newSource);
				$("#main_property_image img").animate({"opacity": "1"}, 1);
				});
			}).attr('src',newSource);
		});
	
	
	//check the latidude and longditude when the page loads
	//checkLatLong();
	/*
	 set the google map
	*/
	if($('#property_map').length > 0){
		//setup the google map
		//set the latlng object for the map centre
		var latlngMap = new google.maps.LatLng(propertyLat,propertyLong);
		//set the latlng object for the marker
		var latlngMarker = new google.maps.LatLng(propertyLat,propertyLong);
		//set the map options
		var myOptions = {
		  zoom: 14,
		  center: latlngMap,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("property_map"),
			myOptions);
		
		//set the marker image
		var image = 'images/map_icon.png';
		var houseTitle = '';
		var marker = new google.maps.Marker({
			position: latlngMarker, 
			map: map, 
			title:houseTitle,
			icon: image,
			draggable: false
		});
		
		//set the street view
		//street view code goes here
		var latlngSView = new google.maps.LatLng(propertyLat,propertyLong);
		var panoramaOptions = {
		  position: latlngSView
		};
		var panorama = new  google.maps.StreetViewPanorama(document.getElementById("street_view"), panoramaOptions);
	}
	
	/*
	 * set the show map button
	*/
	$('#show_map').click(function(e){
		//prevent default button action
		e.preventDefault();
		//set the button states
		$('#show_map').addClass('pressed');
		$('#show_image').removeClass('pressed');
		$('#show_street_view').removeClass('pressed');
		//fade the maps
		$('#main_property_image').fadeOut(500);
		$('#under_offer').fadeOut(500);
		$('#street_view').fadeOut(500);
		});
	
	/*
	 * sets the show image button
	*/
	$('#show_image').click(function(e){
		e.preventDefault();
		//set the button states
		$('#show_map').removeClass('pressed');
		$('#show_image').addClass('pressed');
		$('#show_street_view').removeClass('pressed');
		//fade the maps
		$('#main_property_image').fadeIn(500);
		$('#under_offer').fadeIn(500);
		});
	
	/*
	 * sets the show street view button
	*/
	$('#show_street_view').click(function(e){
		e.preventDefault();
		//set the button states
		$('#show_map').removeClass('pressed');
		$('#show_image').removeClass('pressed');
		$('#show_street_view').addClass('pressed');
		//fade the maps
		$('#street_view').fadeIn(500);
		$('#main_property_image').fadeOut(500);
		$('#under_offer').fadeOut(500);
		});
	
		
	/*
	 * sets the home report button
	*/
	$('#home_report').fancybox({
				'padding'			: 0,
				'width'				: 700,
				'height'			: 500,
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'type'				: 'ajax',
				'scrolling'			: 'no',
				'onComplete'		: function()
					{
					/*
					 * sets the tell a friend bubble action
					*/
					$('#hm_button').click(function(e){
						//prevet defauilt button action
						e.preventDefault();
						/*
						 * check if the required fields have been filled, if not, show the required requied text
						*/
						var bool1 = ($('#your_name').attr('value') != '');
						var bool2 = ($('#your_email').attr('value') != '');
						if(bool1 && bool2)
							{
							//submit the form via ajax
							$("#hm_form").ajaxSubmit(function(data){
								//output the data
								$('#hm_form_holder').html(data);
								});
							}
						else
							{
							if(!bool1)
								{
								$('#your_name_required').show();
								}
							if(!bool2)
								{
								$('#your_email_required').show();
								}
							}
						
						
						});	
					}
			})
		
	/*
     * Sets up the Send to  Friend Button
    */
	$('#send_friend').fancybox({
		'width'				: 700,
		'height'			: 500,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'ajax',
		'scrolling'			: 'no',
		'padding'			: 0,
		'onComplete'		: function()
			{
			//alert("fancy box has loaded");
			$('#basic_submit').click(function(e){
				//prevent default button action
				e.preventDefault();
				//check if the friends email was submitted
				if($('#friend_email').attr('value') != '')
					{
					$("#sendfriend_form").ajaxSubmit(function(data){
						//output the data
						$('#send_friend_inner').html(data);
						});
					}
				else
					{
					$('#error_message').show();
					}
				});
			
			
			
			}	
		})
	
	/*
	 * Runs the carousel of images on the homepage (header tag)
	*/
	$('#home_carousel').innerfade({
				speed: 1000,
				timeout: 10000,
				type: 'sequence',
				containerheight: '137px'
			});
	
	// set up all anchor elements with a "movie" class to work with Shadowbox
    var pap = Shadowbox.setup("#shadowlinks a", {
        gallery:            "Big Pics"
    });

	/*
	 * main image click functions
	*/
	$('#main_property_image').click(function(e){
		var imgPath = $(this).find('img').attr('src').split('/')[$(this).find('img').attr('src').split('/').length - 1];
		//loop through the shadow links and find ther correct image
		$('#shadowlinks a').each(function(){
			var iPath = $(this).attr('href').split('/')[$(this).attr('href').split('/').length - 1];
			if(iPath == imgPath){
				$(this).trigger('click')
			}
		});

	});
	
	//end of the on page ready functions
	});


/*
 * logs a button press
*/
function logButton()
{
//
}
this.logButton = logButton;



	
}

var ptp =  new ptp();
