var vSlideNav = new Array('Feature', 'Latest', 'Popular', 'Property');
var intervalSlide;
var intervalSlideSection;
var intervalSlideTime = 5000;
var intervalSlideDir = 1;

window.addEvent('domready', function() {	
	//HOME PAGE SLIDE SHOW BUTTONS
	if($('propertySlideMenu') != null){
		propertySlideMenuEls = $$('#propertySlideMenu a'); //$('propertySlideMenu').getElementsByTagName('a');
		for(i=0; i < propertySlideMenuEls.length; i++){
			$(propertySlideMenuEls[i].id.split('main').join('images')).current = 1;
			propertySlideMenuEls[i].set('href','#');
			//Set intveral timer to cycle through current show
			if(i==0){
				intervalSlideSection = propertySlideMenuEls[i].id.replace('main','');
			}
			//Set buttons to swap shows
			propertySlideMenuEls[i].addEvent('click', function(e) {
				rel = this.getAttribute('rel').split('[')[1].split(']').join('');
				clearInterval(intervalSlide);
				for(j=0; j< vSlideNav.length; j++){
					if($('main' + vSlideNav[j]) != null){
						//Reset to first slide
						$('nav'+vSlideNav[j]).getElementsByTagName('a')[$('images'+vSlideNav[j]).current].set('class','');
						propertySlideNavEls = $$('#images'+vSlideNav[j]+' .slideimageitem');
						for(k=0; k< propertySlideNavEls.length; k++){
							if( $(propertySlideNavEls[k].id) != null){
								if( k > 0 ){
									$(propertySlideNavEls[k].id).morph({'opacity':0});
								}else{
									$(propertySlideNavEls[k].id).morph({'opacity':1});
								}
							}
						}
						if(vSlideNav[j] == rel){
							intervalSlideDir = 1;
							intervalSlideSection = rel;
							intervalSlide = setInterval(callNextSlide, intervalSlideTime);
							$('main'+vSlideNav[j]).set('class','current');
							$('images'+vSlideNav[j]).current = 1;
							$('nav'+vSlideNav[j]).getElementsByTagName('a')[1].set('class','current');
							$('images'+vSlideNav[j]).setStyle('top',0);
							$('images'+vSlideNav[j]).setStyle('left',0);
							$('images'+vSlideNav[j]).setStyle('opacity',0);
							$('images'+vSlideNav[j]).setStyle('visibility','visible');
							$('images'+vSlideNav[j]).setStyle('display','block');
							$('images'+vSlideNav[j]).morph({'opacity':1});
							$('nav'+vSlideNav[j]).setStyle('opacity',0);
							$('nav'+vSlideNav[j]).setStyle('visibility','visible');
							$('nav'+vSlideNav[j]).setStyle('display','block');
							$('nav'+vSlideNav[j]).morph({'opacity':1});
						}else{
							$('main'+vSlideNav[j]).set('class','');
							$('images'+vSlideNav[j]).current = 1;
							$('images'+vSlideNav[j]).morph({'opacity':0});
							$('nav'+vSlideNav[j]).morph({'opacity':0});
						}
					}
				}
				return false;
			});
		}
	}
	
	//SLIDE SHOW GROUP SUB NAVIGATION (BACK 1 2 3 4 ... 10 NEXT)
	if($('propertySlideNav') != null){
		checkslides = $$('#propertySlides img');
		for(h=0;h<checkslides.length;h++){
			if(!checkslides[h].src || checkslides[h].src == '' || checkslides[h].src == window.location.href){
				checkslides[h].src = '/images/property/empty_image.png';
			}else{
				now = new Date();
				checkslides[h].src =checkslides[h].src + '?' + now.getTime();
			}
		}
		
		for(k=0;k<vSlideNav.length;k++){
			if($( 'nav'+vSlideNav[k] ) != null){
				if( vSlideNav[k] == 'Property' && $('tab_property') != null ){
					intervalSlideDir = 1;
					intervalSlideSection = 'Property';
					$('images'+intervalSlideSection).current = 1;
				}

				propertySlideNavEls = $$('#images'+vSlideNav[k]+' .slideimageitem'); //$( 'nav'+vSlideNav[k] ).getElementsByTagName('a');
				for(j=0; j< propertySlideNavEls.length; j++){
					if( $(propertySlideNavEls[j].id) != null){
						if( j > 0 ){
							$(propertySlideNavEls[j].id).morph({'opacity':0});
						}else{
							$(propertySlideNavEls[j].id).morph({'opacity':1});
						}
					}
				}


				propertySlideNavEls = $$('#nav'+vSlideNav[k]+' a'); //$( 'nav'+vSlideNav[k] ).getElementsByTagName('a');
				for(j=0; j< propertySlideNavEls.length; j++){
					if( propertySlideNavEls[j].getAttribute('rel') != null ){
						propertySlideNavEls[j].set('href','#');
												
						propertySlideNavEls[j].addEvent('click', function(e) {
							rel = this.getAttribute('rel');
							type = rel.split('[')[0];
							currentRel = rel.split('[')[1].split(']').join('');
							if(currentRel != 'next' && currentRel != 'back' && $(type + '_' + currentRel) != null){
								oldCurrent = $(this.parentNode.parentNode.id.split('nav').join('images')).current;
								newCurrent = $(this.parentNode.parentNode.id.split('nav').join('images')).current = parseInt(currentRel);
								//$(this.parentNode.parentNode.id.split('nav').join('images')).set('tween', {duration:1000,transition:'expo:out'});
								//setTo = $(type+'_'+currentRel).offsetLeft * -1
								//$(this.parentNode.parentNode.id.split('nav').join('images')).tween('left', setTo );							
								if( $(type+'_'+oldCurrent) != null ){
									$(type+'_'+oldCurrent).morph({'opacity':0});
								}
								if( $(type+'_'+newCurrent) != null ){
									if(type.indexOf('application') == -1){
										nextImg = $$('#'+type+'_'+newCurrent + ' img')[0];
										nextImg.removeClass('hidden');
									}
									$(type+'_'+newCurrent).morph({'opacity':1});
								}
							}else{
								newCurrent = (currentRel != 'next') ? $(this.parentNode.parentNode.id.split('nav').join('images')).current - 1 : $(this.parentNode.parentNode.id.split('nav').join('images')).current + 1;
								if($(type+'_'+newCurrent) == null){
									//newCurrent = (current == 'next') ? 1 : parseInt(this.parentNode.parentNode.getElementsByTagName('a').length) - 2;
									newCurrent = $(this.parentNode.parentNode.id.split('nav').join('images')).current;
								}else{
									oldCurrent = $(this.parentNode.parentNode.id.split('nav').join('images')).current
									$(this.parentNode.parentNode.id.split('nav').join('images')).current = newCurrent;
									//$(this.parentNode.parentNode.id.split('nav').join('images')).set('tween', {duration:1000,transition:'expo:out'});
									//setTo = $(type+'_'+newCurrent).offsetLeft*-1;
									//$(this.parentNode.parentNode.id.split('nav').join('images')).tween('left', setTo);
									if( $(type+'_'+oldCurrent) != null ){
										$(type+'_'+oldCurrent).morph({'opacity':0});
									}
									if( $(type+'_'+newCurrent) != null ){
										if(type.indexOf('application') == -1){
											nextImg = $$('#'+type+'_'+newCurrent + ' img')[0];
											nextImg.removeClass('hidden');
										}
										$(type+'_'+newCurrent).morph({'opacity':1});
									}
								}
							}
							if($(type+'Caption') != null && $(type+'image_'+newCurrent) != null){
								$(type+'Caption').set('html', '&mdash; '+ $(type+'image_'+newCurrent).alt); 
							}
							
							//alert(current + ' : ' + newCurrent);
							updateClassList = $$('#'+this.parentNode.parentNode.id+' a');
							for(i=1; i < updateClassList.length-1; i++){
								if(i != newCurrent){
									updateClassList[i].removeClass('current');
								}else{
									updateClassList[i].addClass('current');
								}
							}
							return false;
						 });					
					}
					if(j==1){
						propertySlideNavEls[j].fireEvent('click');
					}
				}
			}
		}
	}
	
	//GOOGLE MAPS NAVIFATION
	if($('mapView')){
		mapViewEls = $$('#mapView a'); //.getElementsByTagName('a');
		if($('mapControl_in') != null){
			$('mapControl_in').href="#";
			$('mapControl_in').addEvent('click', function(e){
				gMap.zoomIn();
				return false;
			})
		}
		if($('mapControlmap_out') != null){
			$('mapControlmap_out').href="#";
			$('mapControlmap_out').addEvent('click', function(e){
				gMap.zoomOut();
				if(gMap.getZoom() < 2){
					gMap.setZoom(2);
				}
				return false;
			})
		}
		
		for(i=0;i<mapViewEls.length;i++){
			if(mapViewEls[i].id.indexOf('mapView')!=-1){
				if(mapViewEls[i].getAttribute('class') == 'current'){
					$('mapView').current = mapViewEls[i].id;
				}
				mapViewEls[i].set('href','#');
				mapViewEls[i].addEvent('click', function(e){
					view = this.id.split('_')[1];
					switch(view){
						case 'streetview':
							$('propertyLocationPano').removeClass('hidden');
						break;
						case 'terrain':
							$('propertyLocationPano').addClass('hidden');
							gMap.setMapType(G_PHYSICAL_MAP);
						break;
						case 'satellite':
							$('propertyLocationPano').addClass('hidden');
							gMap.setMapType(G_SATELLITE_MAP);
						break;
						case 'map':
							$('propertyLocationPano').addClass('hidden');
							gMap.setMapType(G_NORMAL_MAP);
						break;
					}
					$($('mapView').current).set('class', '');
					$('mapView').current = this.id;
					$(this.id).set('class', 'current');
					return false;
				})
			}
		}
	}

	if( $('showContactForm') != null && $('contactForm') != null ){
		$('contactForm').set('class','hidden');
		$('showContactForm').addEvent('click', function(){
			$('ownerSummaryContact').set('class', 'hidden')
			$('contactForm').set('class', '');
			return false;
		})
	}
	
	setAddFavAjax();
	
});

window.addEvent('load', function() {
	intervalSlide = setInterval(callNextSlide, intervalSlideTime);
});

function setAddFavAjax(){
	links = $$('#inner a');
	for(l=0;l<links.length;l++){
		if(links[l].getAttribute('rel') && links[l].getAttribute('rel').indexOf('addfav') != -1){
			links[l].set('href', '#');
			links[l].addEvent('click', function(e){
				e.stop();
				addFavourite(this);
				return false;
			});
		}
		
	}
}

function addFavourite(obj){
	var propertyid = parseInt(obj.getAttribute('rel').split('[')[1].replace(']',''));
	var linkid = obj.id;
	(obj.parentNode).id = obj.id + 'Parent';
	$(obj.id+'Parent').addClass('ajax-loading');
	new Request({
		   url: '/index.php',
		   method: 'get',
		   data: 'class=property&action=addfavourite&view=ajax&property_id=' + propertyid,
		   onFailure: function(instance){
				$(linkid).parentNode.removeClass('ajax-loading');
		   },
		   onSuccess: function(response){
				if(response != ''){
					if(response.indexOf('<root>') != -1){
						status = tagContents('status', response); 
						if(status == true || status == 'true' || status == ''){
							$(linkid).addClass('hidden');
							$('go'+linkid).removeClass('hidden');
						}
					}
				}
				$(linkid).parentNode.removeClass('ajax-loading');
		   }
		}).send();
	return false;
}

function callNextSlide(){
	//alert('intervalSlideSection ' + intervalSlideSection);
	if( $('images'+intervalSlideSection) != null ){
		old =$('images'+intervalSlideSection).current;
		next = $('images'+intervalSlideSection).current = $('images'+intervalSlideSection).current + intervalSlideDir;
		imgCont = $$('#images'+intervalSlideSection + ' img');
		iSSL = (intervalSlideSection != 'Property') ? intervalSlideSection.toLowerCase() : 'propertyimages';
		if($(iSSL +'_' + next) == null){
			//intervalSlideDir = (intervalSlideDir > 0) ? -1 : 1;
			next = $('images'+intervalSlideSection).current = 1; //$('images'+intervalSlideSection).current + (intervalSlideDir*2);
		}
		$('images'+intervalSlideSection).set('tween', {duration:1000,transition:'expo:out'});
		if( $(iSSL +'_' + next) != null ){
			//setTo = $(iSSL+'_'+next).offsetLeft * -1
			//$('images'+intervalSlideSection).tween('left', setTo );
			if( $(iSSL +'_' + old) != null ){
				$(iSSL +'_' + old).morph({'opacity':0});		
			}
			nextImg = $$('#'+iSSL +'_' + next + ' img')[0];
			nextImg.removeClass('hidden');
			
			$(iSSL +'_' + next).morph({'opacity':1});
			if($(iSSL+'Caption') != null && $(iSSL+'image_'+next) != null){
				$(iSSL+'Caption').set('html', $(iSSL+'image_'+next).alt); 
			}
			updateClassList = $$('#nav'+intervalSlideSection+' a');
			for(i=1; i < updateClassList.length-1; i++){
				if(i != next){
					if(updateClassList[i]){
						updateClassList[i].removeClass('current');
					}
				}else{
					if(updateClassList[i]){
						updateClassList[i].addClass('current');
					}
				}
			}
		}
		
		if(intervalSlide){
			clearInterval(intervalSlide);
		}
		intervalSlide = setInterval(callNextSlide, intervalSlideTime);
	}
}
