// JavaScript Document
jQuery(document).ready(function() {		
		$(".slideAd").toggle("fast");//show the slideAd step 1		
		$(this).toggleClass("active");//show the slideAd step 2				
		
		//attach the slideAd code to the div named "trigger"		
		$(".trigger").click(function(){
				$(".slideAd").toggle("fast");//show the slideAd step 1			
				$(this).toggleClass("active");//show the slideAd step 2			
				return false;		
		});		
		
		$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav		
		$('ul.topnav li a').hover(function() { //When trigger is clicked			
										   
		//Following events are applied to the subnav itself (moving subnav up and down)			
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click			
		$(this).parent().hover(function(){}, 
		   function(){				
				$(this).parent().find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up			
			});			
			//Following events are applied to the trigger (Hover events for the trigger)		
			}).hover(function() {			
				$(this).addClass("subhover"); //On hover over, add class "subhover"		
			}, function(){	//On Hover Out			
				$(this).removeClass("subhover"); //On hover out, remove class "subhover"		
		});
		
		$("ul.topnav li span").click(function() { //When trigger is clicked			
			//Following events are applied to the subnav itself (moving subnav up and down)
			$(this).parent().find("ul.subnav").slideDown('slow').show(); //Drop down the subnav on click			
			$(this).parent().hover(function() {			}, function(){				
				$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up			
			});			//Following events are applied to the trigger (Hover events for the trigger)
			}).click(function() {			$(this).addClass("subhover"); //On hover over, add class "subhover"		
		}, function(){	//On Hover Out			
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"		
		});			//set up an array of background images			

                var baseurl='http://remax.gr/';
		//var baseurl='http://remaxdev.itsnotnow.gr/';
		//var baseurl='http://localhost/remax/';
		var bkgImgs = ([
						[baseurl+'assetsfe/css/images/home_bckg/background1.jpg', 1000, 449],				
						[baseurl+'assetsfe/css/images/home_bckg/background2.jpg', 1000, 449],
						[baseurl+'assetsfe/css/images/home_bckg/background3.jpg', 1000, 449],
						[baseurl+'assetsfe/css/images/home_bckg/background4.jpg', 1000, 449],
						[baseurl+'assetsfe/css/images/home_bckg/background5.jpg', 1000, 449]
		]);			
		
		//prepare variables for rotating through the images			
		var delay = 7500;			
		var transition = 1100;			
		var imageIndex = 0; //bkgImgs.length-1; //initial index is the last picture, so the first rotation brings us to the beginning			
		var imageCount = bkgImgs.length-1;			
		
		//we create the getImage function for usage later			
		getImage = function(x){	
			return {
				background: 'url(' + bkgImgs[x][0] + ') no-repeat center center',
				width: bkgImgs[x][1],
				height: bkgImgs[x][2],
				left: $('.carousel').width()/2 - bkgImgs[x][1]/2,
				top: $('.carousel').height()/2 - bkgImgs[x][2]/2
			}
		}			
		
		//create the rotateImage function, for usage later			
		rotateImage = function(){
			if(delay < 9000){
				delay = delay + 500;
			}
			var oldpic = getImage(imageIndex);
			imageIndex++;
			if (imageIndex > imageCount)
				imageIndex = 0;
			
			var newpic = getImage(imageIndex);
			$('.display2').css(oldpic).show();
			$('.display1').hide().css(newpic).fadeIn(transition);
			$('.display2').fadeOut(transition);	
			
			//this function sets a timeout before calling itself again				
			setTimeout( function(){ rotateImage() }, delay );}			//create the startRotation function, for usage later			
			startRotation = function(){				//load initial image				
				var initialPic = getImage(0);				
				$('.display1').hide().css(initialPic).fadeIn(transition);				//this function sets a timeout before calling rotateImage	
				var initialdelay = 4800;				
				setTimeout( function(){ 
					rotateImage() 
					}, 
				initialdelay );
			}			//now we call the startRotation function to start the process	
			
			startRotation();						
	
});
