jQuery.noConflict();
jQuery(document).ready(function() {
	jQuery("#videopopup").fancybox({
		'width'				: 540,
		'height'			: 510,
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	if (jQuery('#homeSlideshow').length) {
		var slideshow_timer = window.setInterval(goSlide, 5000, "next");
	}
		
	var startwirl = window.setInterval(function() {
		jQuery('.pulsate')
			.animate({scale:'0.5'},500)
			.animate({scale:'2'},100)
			.animate({scale:'1'},1000);
	}, 2500)
	
	if (jQuery('#thumbs').length) {
		var gallery = jQuery('#thumbs').galleriffic({
    	    delay:                     3000, // in milliseconds
    	    numThumbs:                 10, // The number of thumbnails to show page
    	    preloadAhead:              10, // Set to -1 to preload all images
    	    enableTopPager:            false,
    	    enableBottomPager:         true,
    	    maxPagesToShow:            7,
	        imageContainerSel:         '#slideshow',
	        controlsContainerSel:      '#controls',
	        captionContainerSel:       '#caption',
	        loadingContainerSel:       '#loading',
	        renderSSControls:          true,
	        renderNavControls:         true,
	        playLinkText:              '<div> </div> click here for slideshow',
	        pauseLinkText:             '<div> </div> click here to stop slideshow',
	        prevLinkText:              'Previous',
	        nextLinkText:              'Next',
	        nextPageLinkText:          'Next &rsaquo;',
	        prevPageLinkText:          '&lsaquo; Prev',
	        enableHistory:             false,
	        enableKeyboardNavigation:  true,
	        autoStart:                 false,
	        syncTransitions:           false,
	        defaultTransitionDuration: 1000,
	        onSlideChange:             undefined,
	        onTransitionOut:           undefined,
	        onTransitionIn:            function(slide, caption, isSync) {
				slide.fadeTo(1000,1.0);
				caption.fadeTo(1000,1.0);
	
				jQuery('#controls').removeClass('nodisplay');
				jQuery('div.hotspot').each(function(index) {
					var coords = this.attributes.title.value;
					coords = coords.split(',');
					var spottext = '<div class="hotstar"><img src="http://'+location.hostname+'/wp-content/themes/PAC-Theme/images/pac-logo-small.png" alt="PAC Logo" /></div>';
					var spottext = spottext + this.innerHTML;
					var hotspot = "<div class='activehotspot' style='position:absolute; width:15px; height:15px; top:" + coords[1] + "px; left:" + coords[0] + "px;'><a href='#'><img class='pulsate' src='http://"+location.hostname+"/wp-content/themes/PAC-Theme/images/pac-star.png' alt='' /></a><div class='hotspottext'>" + spottext + "</div></div>";
					jQuery(slide).prepend(hotspot);
				});
				jQuery('div.activehotspot').hover(
					function() {
						jQuery(this).children('div.hotspottext').show();
					},
					function() {
						jQuery(this).children('div.hotspottext').hide();
					}
				);
			},
	    });
	}
	
	jQuery('a.floorplan-button').click(function() {
		jQuery('div.box_fullsize').removeClass('floorplan-index');
		jQuery('div.box_fullsize').addClass('floorplan-list');
		jQuery('div.floorplan-display').hide();
		var floorID = jQuery(this).attr('href');
		jQuery(floorID).toggle();
		return false;
	});
	
	jQuery('#share-print a').click(function() {
		window.print();
		return false;
	});

	jQuery('#slideshowDots a').click(function() {
		clearInterval(slideshow_timer);
		goSlide(jQuery(this).attr('title'));
		return false;
	});
	jQuery('#arrowLeft').click(function() {
		clearInterval(slideshow_timer);
		goSlide("previous");
		return false;
	});
	jQuery('#arrowRight').click(function() {
		clearInterval(slideshow_timer);
		goSlide("next");
		return false;
	});
	
	jQuery('#compare_button').change(function() {
		window.location = jQuery(this).val();
	});
	
	jQuery('select.compare_button_double').change(function() {
		var fpid_new = jQuery(this).val();
		var comparelink = jQuery(this).parent().next().next('div.compare-link').html();
		var fpid_old = jQuery(this).parent().next('div.keep-fp').html();
		if (jQuery(this).parent().next('div.keep-fp').attr('title') == 1) {
			comparelink += '?fpid1='+fpid_old+'&fpid2='+fpid_new;
		} else {
			comparelink += '?fpid1='+fpid_new+'&fpid2='+fpid_old;
		}
		window.location = comparelink;
	});
	
	if (jQuery('ul.features-gallery').length) {
		jQuery('ul.features-gallery li').hide();
		jQuery('ul.features-gallery li:first-child').show();
		var rotateimg = window.setInterval(rotateImages, 2500);
	}
	jQuery('ul.features-list').each(function() {
		jQuery(this).children('li:first-child').addClass('redtext');
	});
	
	function rotateImages() {
		
		jQuery('ul.features-gallery li:visible').each(function() {
			var prevPicID = "#" + jQuery(this).attr('id') + "-list";
			jQuery(prevPicID).removeClass('redtext');
			jQuery(this).fadeOut('slow');
		});
		
		jQuery('ul.features-gallery').each(function() {
			if (jQuery(this).children('li:visible').next('li').length) {
				jQuery(this).children('li:visible').next('li').fadeIn('slow');
				var picID = "#" + jQuery(this).children('li:visible').next('li').attr('id') + "-list";
				jQuery(picID).addClass('redtext');
			} else {
				jQuery(this).children('li:first-child').fadeIn('slow');
				var picID = "#" + jQuery(this).children('li:first-child').attr('id') + "-list";
				jQuery(picID).addClass('redtext');
			}
		});
	}
	
	function goSlide(index) {
		
		var marg = parseFloat(jQuery('#homeSlideshow').css('marginLeft'));
		
		if (isNaN(index)) {
			if (index == "next") {
				if (marg < -((jQuery('#slideshowDots a').length-1) * 940)) {
					var marginOffset = '0px';
					index = 0;
				} else {
					var marginOffset =  (marg-942) + 'px';
					index = (marg / -942)+1;
				}
			} else {
				if (marg > -50) {
					var marginOffset = '-'+((jQuery('#slideshowDots a').length-1)*942)+'px';
					index = jQuery('#slideshowDots a').length-1;
				} else {
					var marginOffset = marg + 942;
					index = marginOffset / -942;
				}
			}
		} else {
			var marginOffset = (index*-942) + 'px';
		}
		updateSlideshowControls(index);
		
		var startPerc = randomXToY(1,1.5,2);
		var sWidth = 942*startPerc;
		var sLeft = randomXToY(-(sWidth-942),0,2);
		var sHeight = 366*startPerc;
		var sTop = randomXToY(-(sHeight-366),0,2);
		sWidth += "px";
		sLeft += "px";
		sHeight += "px";
		sTop += "px";
		var endPerc = randomXToY(1,1.5,2);
		var eWidth = 942*endPerc;
		var eLeft = randomXToY(-(eWidth-942),0,2);
		var eHeight = 366*endPerc;
		var eTop = randomXToY(-(eHeight-366),0,2);
		eWidth += "px";
		eLeft += "px";
		eHeight += "px";
		eTop += "px";
		jQuery('#homeSlideshow').animate({ 'marginLeft':marginOffset }, 1500, function() {
			jQuery('#homeSlideshow img:eq('+(index-1)+')').css({ top:'0', left:'0', width:'942px', width:'366px' });
		});
		jQuery('#homeSlideshow img:eq('+index+')').css({ width:sWidth, height:sHeight, top:sTop, left:sLeft });
		jQuery('#homeSlideshow img:eq('+index+')').animate({ width:eWidth, height:eHeight, top:eTop, left:eLeft }, 4500);
	}
	
	function whySliders() {
		jQuery('.sliderImg').each(function(index) {
			var panelNum = jQuery(this).attr('alt');
			panelNum = panelNum.slice(panelNum.indexOf('-')+1);
			var panelDelay = 200 * index;
			var panelPos = jQuery(this).css('marginLeft');
			panelPos = panelPos.slice(0,panelPos.indexOf('px'));
			panelPos = (panelPos / -307)+1;
			if (panelPos == panelNum) {
				var marginOffset = '0px';
			} else {
				var marginOffset = (panelPos * -307) + 'px';
			}
			jQuery(this).delay(panelDelay).animate({ 'marginLeft':marginOffset }, 1500);
		});
	}
	
	function faderChange() {
		jQuery('ul.fader').each(function(index) {
			if (jQuery(this).children('li:visible').length > 1) {
				jQuery(this).children('li:visible:last').fadeOut('slow');
			} else {
				var ulFader = jQuery(this);
				jQuery(this).children('li:last').fadeIn('slow', function() {
					ulFader.children('li').show();
				});
			}
		});
	}
	
	function updateSlideshowControls(index) {
		jQuery('#slideshowDots a').removeClass('active');
		jQuery('#slideshowDots a:eq('+index+')').addClass('active');
		var imgAlt = jQuery('#homeSlideshow img:eq('+index+')').attr('alt');
		jQuery('#slideshowText h2').text(imgAlt);
	}
	
	function randomXToY(minVal,maxVal,floatVal) {
		var randVal = minVal+(Math.random()*(maxVal-minVal));
		return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
	}
	
});

