$(document).ready(function(){
    $('.navigation li').hover(function(){
       $(this).addClass('cSlided');
    },function(){                         
        $(this).removeClass('cSlided');
    });
});


var cycelContainer  = null;
var g_numSlides     = null;
var g_slideWidth    = null;
var g_curAnimate    = null;
var g_propertyItems = null;
var g_maxSlided     = null;
var g_stop			= 0;

function scroller()
{
	if ( g_stop )
	{
		g_stop--;
		return;
	}
	var current	= $('#iSlideClick li.cActive');
	var next = current.next();
	var nextpos	= next.position();
	var currentpos = current.position();
	
	var boxPosition = parseInt($('#iSliderShow').stop(true,false).css('left'));
	if ( nextpos == null )
	{
		next = $('#iSlideClick li').first();
		nextpos	= next.position();
	}
	
	
	$('#iSliderShow').empty().animate({left:'+='+(nextpos.left-boxPosition)},200).append(next.html()).removeClass(current.attr('class')).addClass(next.attr('class'));
	next.addClass('cActive');
	current.removeClass('cActive');
}

$(document).ready(function(){/*
    $.fn.cycle.updateActivePagerLink = function(pager,curindex){
        $('#iSlideClick li.active').removeClass('active');
        $('#i_'+curindex).addClass('active');
    };

    cycelContainer = $('#iSlideshow').cycle({
        fx:        'growY',   
        speed:      600,
        pager : '#iSlideClick',
        pagerAnchorBuilder:function(idx,slide)
        {
            return;
        }
    });*/
	setInterval( 'scroller()', 4000 );
	
	var firstSlide = $('#iSlideClick li').first().addClass('cActive');
	$('#iSliderShow').append( firstSlide.html() ).addClass( firstSlide.attr('class') );
	
	$('#iArea').click(function(e){
		e.preventDefault();
		if ( !$(this).hasClass('cShowed'))
		{
			$('#iBox').children('ul').remove();
			var clonedLocations = $('.cPropertyLocations').clone();
			var pos = $(this).addClass('cShowed').offset();
			$('#iBox').stop(true,true).append(clonedLocations).css('top',pos.top+31).fadeIn(500);
		}
		else
		{
			$(this).removeClass('cShowed');
			$('#iBox').stop(true,true).fadeOut(500,function(){$(this).children('ul').remove();});
		}
	});
    
    var errpos = $('a[name="error"]').offset();
    if ( errpos != null )
        $(document).scrollTop(errpos.top);
    
    $('#iBox .cClose').click(function(e){
        e.preventDefault();
        $('#iArea').removeClass('cShowed');
        $('#iBox').stop(true,true).fadeOut(500,function(){$(this).children('ul').remove();});
    });
	
	$('#iShowSearch').click(function(e){
		e.preventDefault();
		$(this).fadeOut(500);
		$('#iPropertySearch').fadeIn(1000);
	});
    
    $('#iSlideClick li a').click(function(e){
    	if ( $(this).attr('href')=='#' )
    		e.preventDefault();
    });
    
    $('#iSlideClick li').hover(function(){
    	if ( $(this).hasClass('cActive'))
    		return;
    	g_stop = 1;
    	var current	= $('#iSlideClick li.cActive');
    	var nextpos	= $(this).position();
    	
    	var boxPosition = parseInt($('#iSliderShow').stop(true,false).css('left'));
    	var apleft		= (nextpos.left-boxPosition);
    	$('#iSliderShow').empty().animate({left:'+='+apleft},200).append($(this).html()).removeClass(current.attr('class')).addClass($(this).attr('class'));
    	$(this).addClass('cActive');
    	current.removeClass('cActive');
    });
    
    $('#iRNextPage').click(function(e){
        e.preventDefault();
        var parent      = $('#iRecentProperties');
        var curActive   = parent.children( '#iRecentProperties li.cActive' );
        var nextActive  = curActive.next('li');
        if ( 0 )
            nextActive  = parent.first('li');
        nextActive.css('display','block').animate({left: '+=763px'},400,function(){
            $('#iRecentProperties li.cActive').css({'left':'-763px','display':'none'}).removeClass('cActive');
            $(this).addClass('cActive');
        })
    });
    
    $('#iRPrevPage').click(function(e){
        e.preventDefault();
        var parent      = $('#iRecentProperties');
        var curActive   = parent.children( '#iRecentProperties li.cActive' );
        var nextActive  = curActive.prev('li');
        if ( 0 )
            nextActive  = parent.first('li');
        nextActive.css('display','block').animate({left: '+=763px'},400,function(){
            $('#iRecentProperties li.cActive').css({'left':'-763px','display':'none'}).removeClass('cActive');
            $(this).addClass('cActive');
        })
    });
    
    g_propertyItems = $('#iPropertyMinImages li').size();
    g_maxSlided     = g_propertyItems * 100;
    $('#iNextImage').click(function(e){
        e.preventDefault();
        var cleft = parseInt($('#iPropertyMinImages').stop(true,true).css('left'));
//        alert( (-( cleft - 100 ))+' ?= '+( g_maxSlided - 300 ))
        if ( (-( cleft - 100 )) <= ( g_maxSlided - 300 ) )
        {
            $('#iPropertyMinImages').stop(true,true).animate({left: '-=100px'},250);
        }
    });
    
    $('#iPrevImage').click(function(e){
        e.preventDefault();
        var cleft = parseInt($('#iPropertyMinImages').stop(true,true).css('left'));
//        alert( (-( cleft - 100 ))+' ?= '+( g_maxSlided - 300 ))
        if ( (-( cleft + 100 ) ) >= 0 )
        {
            $('#iPropertyMinImages').stop(true,true).animate({left: '+=100px'},250);
        }
    });
    
    $('#iPropertyMinImages li a').click(function(e){
        e.preventDefault();
        $('#iPropertyImages li').each(function(){$(this).stop(true,true);});
        var bimage = $(this).attr('href');
        var simage = $('#iPropertyImages li img[src="'+bimage+'"]');
        var parentli = simage.parent('li');
        if ( parentli.hasClass('cActive') )
            return;
        $('#iPropertyImages li.cActive').fadeOut(1500,function(){$(this).removeClass('cActive')});
        parentli.fadeIn(1500).addClass('cActive');                                                    
    });
    
    $('#name, #email, textarea.cMinText, .cMessage .cBorder textarea, #iNewsLatter .cBorder input').focus(function(){
            var value = $(this).val();
            if ( value == 'Name' || value == 'Email' || value == 'Message' || value == 'your email' )
                $(this).val( '' );
    });
    $('#name, #email, textarea.cMinText, .cMessage .cBorder textarea, #iNewsLatter .cBorder input').focusout(function(){
        var value = $(this).val();
        var name = $(this).attr('name');
        if ( value == '' && name == 'name' )
            $(this).val( 'Name' );
        if ( value == '' && name == 'email' )
            $(this).val( 'Email' );
        if ( value == '' && name == 'message' )
            $(this).val( 'Message' );
        if ( value == '' && name == 'EMAIL' )
            $(this).val( 'your email' );
    }); 
    jQuery("#iSearchSelect").select_skin();
    
    $('#iPropertySearch ul a').click(function(e){
        e.preventDefault();
        $('#iPropertySearch ul li.cActive').removeClass('cActive');
        $('#iSearchSelect').empty()
        $(this).parent('li').addClass('cActive').children('select').children('option').each(function(){
            $('#iSearchSelect').append( $(this).clone() );
        });
        
        $('#iSearchSelect').select_unskin().select_skin();
    });
    
    $('#iSearchSelect').change(function(){
        $('#iSearchForm').submit();
    });
});
