$(document).ready(function() { 
	if($('#contact-popup').length){
		$('#contact-popup-intro .close').click(function(){
			$('#contact-popup, #contact-popup-bg').hide();
			return false;
		})
		
		$('a.popup').click(function(){
			theHeight = $(document).height();		
			$('#contact-popup-bg').css('height', theHeight); //resize the background color
			$('#contact-popup, #contact-popup-bg').show();
			return false;
		})
		
		$('#contact-popup-form input').focus(function(){
			if($(this).hasClass('active')){
				
			}else{
				$(this).val('').addClass('active');
			}
		});
		
		$('#contact-popup-form input').blur(function () {
			if($(this).hasClass('active') && $(this).val()=='' && $(this).attr('default')) {
				$(this).val($(this).attr('default'));
				$(this).removeClass('active');
			}
		});
		
		$('#contact-popup-status p.failure a').click(function(){
			$('#contact-popup-status, #contact-popup-status p.success, #contact-popup-status p.failure').fadeOut('slow',function(){
				$('#contact-popup-form').fadeIn();
			});	
			return false;		
		})
		
		function processJson(data) { 
			var isValid = (data.valid)
			if (isValid == 1){
				//valid
				$('#contact-popup-form').fadeOut('slow',function(){
					$('#contact-popup-status, #contact-popup-status p.success').fadeIn();
				});
			}else if (isValid == 0){
				//invalid
				$('#pleaseNoteText').fadeIn();
				/*$('#contact-popup-form').fadeOut('slow',function(){
					$('#contact-popup-status, #contact-popup-status p.failure').fadeIn();
				});*/			
			}else{
				alert('hmm');
			}
			}
	
		$('#jsonForm').ajaxForm({ 
			dataType:  'json', 
			success:   processJson 
		}); 
	}

	// Homepage slideshow
	if ($('#hero-slider').length){
		$('#hero-slider').anythingSlider({
			// Appearance
			width               : null,      // Override the default CSS width
			height              : null,      // Override the default CSS height
			resizeContents      : true,      // If true, solitary images/objects in the panel will expand to fit the viewport
			// Navigation
			startPanel          : 1,         // This sets the initial panel
			hashTags            : false,      // Should links change the hashtag in the URL?
			buildArrows         : false,      // If true, builds the forwards and backwards buttons
			buildNavigation     : true,      // If true, buildsa list of anchor links to link to each panel
			navigationFormatter : null,      // Details at the top of the file on this use (advanced use)
			// Slideshow options
			autoPlay            : true,      // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
			startStopped        : false,     // If autoPlay is on, this can force it to start stopped
			pauseOnHover        : true,      // If true & the slideshow is active, the slideshow will pause on hover
			resumeOnVideoEnd    : true,      
			stopAtEnd           : false,     // If true & the slideshow is active, the slideshow will stop on the last page
			playRtl             : false,     // If true, the slideshow will move right-to-left
			startText           : null,   // Start button text
			stopText            : null,    // Stop button text
			delay               : 9700,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
			animationTime       : 900,       // How long the slideshow transition takes (in milliseconds)
			easing              : "swing"    // Anything other than "linear" or "swing" requires the easing plugin
		});
	}
	
	// Homepage article tabs
	if($('#welcome .tabs').length) {
		$('#welcome-tab1').addClass('current');
		
		$('#welcome .panes').anythingSlider({
			width			: 515,
			height			: 278,
			resizeContents	: false,
			buildArrows		: false,
			buildNavigation	: false,
			startPanel		: 1,		// Sets the initial panel
			autoPlay		: false,	// don't auto run ever
			startStopped	: true,		// make sure we aren't running
			animationTime	: 500,		// time to transition in milliseconds
			easing			: 'linear'
		});
		
		$("#welcome .tabs a").bind('mouseenter click',
			function() {
				// get the hovered id
				var hoveredId = $(this).attr('id');
				
				// pull the number off of the id
				var hoveredId = hoveredId.replace('welcome-tab', '');
				
				// if current is greater, cycle forward, else cycle backward
				$('#welcome .panes').anythingSlider(hoveredId);
				
				// remove current class and set to new item
				$('#welcome .tabs .current').removeClass('current');
				$(this).addClass('current');
				
				return false;
			}
		);
	}
	
	if($('.about-box').length){
		$('.main-content .about-box').last().addClass('last');
	}
	
	if($('.box_area').length){
		if($('.box_area > div').length < 4){
			$('.box_area').parent().parent().parent().css('border-right', '1px solid #d6d6d6');
		}
	}
	
	if($('ul.video-tabs').length) {
		if($("ul.video-tabs").attr('starttab')!="") {
			$("ul.video-tabs").tabs("div.panes > div", {initialIndex: $("ul.video-tabs").attr('startTab')});
		} else {
			$("ul.video-tabs").tabs("div.panes > div");
		}
	}
	
	if($('#featured_listing .properties').length) {
		$('#featured_listing .properties .slides').anythingSlider({
			// Appearance
			width               : 430,			// Override the default CSS width
			height              : 110,			// Override the default CSS height
			resizeContents      : true,			// If true, solitary images/objects in the panel will expand to fit the viewport
			// Navigation
			startPanel          : 1,			// This sets the initial panel
			hashTags            : false,		// Should links change the hashtag in the URL?
			buildArrows         : false,			// If true, builds the forwards and backwards buttons
			buildNavigation     : false,		// If true, buildsa list of anchor links to link to each panel
			navigationFormatter : null,			// Details at the top of the file on this use (advanced use)
			// Slideshow options
			autoPlay            : false,		// This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
			startStopped        : true,			// If autoPlay is on, this can force it to start stopped
			pauseOnHover        : true,			// If true & the slideshow is active, the slideshow will pause on hover
			resumeOnVideoEnd    : true,			
			stopAtEnd           : false,		// If true & the slideshow is active, the slideshow will stop on the last page
			playRtl             : false,		// If true, the slideshow will move right-to-left
			startText           : null,			// Start button text
			stopText            : null,			// Stop button text
			delay               : 10000,		// How long between slideshow transitions in AutoPlay mode (in milliseconds)
			animationTime       : 500,			// How long the slideshow transition takes (in milliseconds)
			easing              : "swing"		// Anything other than "linear" or "swing" requires the easing plugin
		});
		
		$('#featured_listing #arrow-left').click(function() {
			$('#featured_listing .properties .slides').data('AnythingSlider').goBack();
			return false;
		});
		$('#featured_listing #arrow-right').click(function() {
			$('#featured_listing .properties .slides').data('AnythingSlider').goForward();
			return false;
		});
	}
	
	if($('#seller-slideshow').length) {
		$('#seller-slideshow .slides').anythingSlider({
			// Appearance
			width               : 300,			// Override the default CSS width
			height              : 151,			// Override the default CSS height
			resizeContents      : true,			// If true, solitary images/objects in the panel will expand to fit the viewport
			// Navigation
			startPanel          : 1,			// This sets the initial panel
			hashTags            : false,		// Should links change the hashtag in the URL?
			buildArrows         : false,			// If true, builds the forwards and backwards buttons
			buildNavigation     : false,		// If true, buildsa list of anchor links to link to each panel
			navigationFormatter : null,			// Details at the top of the file on this use (advanced use)
			// Slideshow options
			autoPlay            : true,			// This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
			startStopped        : false,		// If autoPlay is on, this can force it to start stopped
			pauseOnHover        : false,		// If true & the slideshow is active, the slideshow will pause on hover
			resumeOnVideoEnd    : true,			
			stopAtEnd           : false,		// If true & the slideshow is active, the slideshow will stop on the last page
			playRtl             : false,		// If true, the slideshow will move right-to-left
			startText           : null,			// Start button text
			stopText            : null,			// Stop button text
			delay               : 9500,		// How long between slideshow transitions in AutoPlay mode (in milliseconds)
			animationTime       : 650,			// How long the slideshow transition takes (in milliseconds)
			easing              : "swing"		// Anything other than "linear" or "swing" requires the easing plugin
		});
		
		$('#seller-slideshow .arrows .arrow-left').click(function() {
			$('#seller-slideshow .slides').data('AnythingSlider').goBack();
			return false;
		});
		$('#seller-slideshow .arrows .arrow-right').click(function() {
			$('#seller-slideshow .slides').data('AnythingSlider').goForward();
			return false;
		});
	}
	
	if($('#article-listing-main #graph .dot').length) {
		$('#article-listing-main #graph .dot').hover(function() {
			var id = $(this).attr('id');
			id = id.replace('dot', '');
			
			var tooltip = $('#article-listing-main #graph #dot-tooltip' + id);
			
			var top = $(this).offset().top - tooltip.height() - $('#article-listing-main').offset().top;
			var left = $(this).offset().left - (tooltip.width()/2) - $('#article-listing-main').offset().left + 5;
			
			tooltip.css({
				'top':		top,
				'left':		left,
				'display':	'block'
			});
		}, function() {
			var id = $(this).attr('id');
			id = id.replace('dot', '');
			
			setTimeout("unhover(" + id + ");", 100);
		});
		
		$('#article-listing-main #graph .dot-tooltip').hover(function() {
			
			$(this).attr('stayHovered', 1);
			
		}, function() {
			$(this).attr('stayHovered', 0);
			$(this).hide();
		});
		
		$('#article-listing-main .article-link, #article-listing-main .dot').click(function() {
			var id = $(this).attr('id');
			id = id.replace('dot', '');
			id = id.replace('article-link', '');
			
			$('#article-listing-main .article-listing-article').hide();
			$('#article-listing-main #article' + id).show();
			
			return false;
		});
	}
	
	$('.toggle_area a').click(function(){
		if($('.toggle_area a').hasClass('close')){
			$('.searchcontainer > div').fadeIn();
			$('.searchcontainer').show('fast');
			$('.toggle_area a').removeClass('close');		
		}else{
			$('.searchcontainer > div').fadeOut();
			$('.searchcontainer').hide('fast');
			$('.toggle_area a').addClass('close');			
		}
		return false;
	})
	
	//testimonials top scroller
	$('#testimonial #mygallery').scrollable({ circular:true }); 
	$('#testimonial #mygallery .panel ul li a').click(function(){ //changes the top video when clicked on
		t = $(this).parent().attr('class');
		$('#testimonial .top_bg .data_container').hide();
		newContent = $('#testimonial .top_bg #count' + t).html();
		$('#testimonial .top_bg #count' + t).show().html('');
		$('#testimonial .top_bg #count' + t).html(newContent);
		return false;
	})

	//testimonials bottom scroller
	$('#quote_area #mygallery_t').scrollable({ circular:true }).autoscroll({interval: 8000 }); 
	
	//property results page scrollers
	$('.scrollable1').scrollable({ circular:true, next: '#next1', prev: "#prev1" }).navigator({ navi: '#navi1' });
	$('.scrollable2').scrollable({ circular:true, next: '#next2', prev: "#prev2"  }).navigator({ navi: '#navi2' });
	
	// featured property scroller
	$('#mygallery .belt').scrollable({circular:true, next: '#next3', prev: '#prev3'}).navigator({navi: '#navi3'});

	$('.panel ul li:last .box').addClass('last');
	
	if($('#toggle-property-search').length) {
		$('#toggle-property-search a').click(function() {
			if($('#toggle-property-search span').hasClass('down')){

				$('.main-content .left_img').animate({
					height: 278
				});
				$('#buyer-search').animate({
					height: 267
				});					
				$('#toggle-property-search span').toggleClass('down');
			}else{
				$('.main-content .left_img, #buyer-search').animate({
					height: 75
				});
				$('#buyer-search').animate({
					height: 64
				});				
				$('#toggle-property-search span').toggleClass('down');
			}
		});
	}

	if($('#one').length) {    
		$('#one').liteAccordion();

		//property slideshow
		Galleria.loadTheme('/scripts/themes/classic/galleria.classic.min.js');
		$('#property-image-slideshow').galleria({
			width: 650,
			height: 416,
			autoplay: 8000,
			imageCrop: true,
			imageMargin: 0
		});
	}
	
	if($('#alternatelisting').length) {    
		$('.gallery').cycle({ 
			timeout: 0,
			next: '.gallerynext',
			prev: '.galleryprev' 
		});

	}

	$('#welcome .tabs li:last').addClass('last');
		
	/******************************** Partner Page ************************************/
	if($('#our_partners').length) {
		$('#our_partners .tab_area a.first').hover(function() {
			$('#our_partners .tab_area').addClass('first_selected');
		}, function() {
			if(!$(this).hasClass('selected')) {
				$('#our_partners .tab_area').removeClass('first_selected');
			}
		});
		
		$('#our_partners .tab_area a').click(function() {
			// Get the category id
			categoryId = $(this).attr('categoryid');
			
			$('#our_partners .tab_area').removeClass('first_selected');
			if($(this).hasClass('first')) {
				$('#our_partners .tab_area').addClass('first_selected');
			}
			
			$('#our_partners .tab_area a').removeClass('selected');
			$(this).addClass('selected');
			
			$('#our_partners .tab1_data').hide();
			$('#tab_data_'+categoryId).show();
			
			return false;
		});
	}
});

function unhover(id) {
	if($('#article-listing-main #graph #dot-tooltip' + id).attr('stayHovered') != 1) {
		$('#article-listing-main #graph #dot-tooltip' + id).hide();
	}
}

function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),
		'sharer',
		'toolbar=0,status=0,width=626,height=436'
	);
	return false;
}

function tweet_click() {
	u = location.href;
	window.open(
		'https://twitter.com/share?url='+encodeURIComponent(u),
		'Tweet',
		'toolbar=0,status=0,width=626,height=436'
	);
	return false;
}

