$(function(){	

// Search Blinker function 
    $('.blink').
        focus(function() {
            if(this.title==this.value) {
                this.value = '';
            }
        }).
        blur(function(){
            if(this.value=='') {
                this.value = this.title;
        }
    });


// Home Page Slider Function

	var main_slider_speed = 'slow'; //Adjust main slider animation speed 

	current_slide = 0;
	
	function move_to(to_index) {
		if ($('span.pointer:animated').length) {
			return false;
		}
		var slider_img_width = parseInt($('.holder img').width());
		var pointer_pos;
		
		next_move = -1 * to_index * slider_img_width;	
		if(to_index == 0) {
			pointer_pos = 145;
		} else if(to_index == 1) {
			pointer_pos = 467;
		} else if (to_index == 2) {
			pointer_pos = 785;
		}
		$('.holder-cnt').animate({left: next_move + "px"}, main_slider_speed);	
		$('.slider-subnav').removeClass('active');
		$('.slider-subnav:eq(' + to_index + ')').addClass('active');
		$('span.pointer').animate({left: pointer_pos + "px"}, main_slider_speed);		
	}
	$('.banner-nav').click(function() {
		var total_slides = 3, next_slide, next_move;
		current_slide = $('.slider-subnav').index($('.slider-subnav.active'));
		if($(this).hasClass('prev')) {			
			if(current_slide == 0) {
				next_slide = total_slides - 1;
			} else {
				next_slide = current_slide - 1;
			}						
		} else if($(this).hasClass('next')) {		
			if(current_slide == total_slides - 1) {
				next_slide = 0;
			} else {
				next_slide = current_slide + 1;
			}						
		}		
		move_to(next_slide);
		return false;
	});
	
//Home Page Slider 3 Links Navigation Function	
	$('a.slider-subnav').click(function() {
		var to_index = $('a.slider-subnav').index(this);
		move_to(to_index);
		return false;
	});


//Automatic testimonial change function
	var testimonial_timeout = 10000; //Adjust testimonial change timeout
	var testimonial_speed = 1000; //Adjust testimonial change speed
	
	
	function change_testimonial() {
		var current_testim = $('#testimonials p').index($('#testimonials p:visible'));
		var next_testim;
		if(current_testim == $('#testimonials p').length - 1) {
			next_testim = 0;
		} else {
			next_testim = current_testim + 1;
		}		
		$('#testimonials p:eq(' + current_testim + ')').fadeOut(testimonial_speed);
		$('#testimonials p:eq(' + next_testim + ')').fadeIn(testimonial_speed);		
	}
	
	var interval = setInterval(change_testimonial, testimonial_timeout);




// Latest work slider function
	
	var work_slider_speed = 300; //Adjust latest work slider speed

	var current_work_slide = 0;
	
	$('.latest-work-nav').click(function() {	
		var current_work_slide = $('.latest-work-slider-cnt a').index($('.latest-work-slider-cnt a.active'));
		var next_work_slide, next_work_move;
		var work_slide_width = parseInt($('.latest-work-slider-cnt a').width());
		var total_work_slides = $('.latest-work-slider-cnt a').length;
		if($(this).hasClass('prev')) {			
			if(current_work_slide == 0) {
				next_work_slide = total_work_slides - 1;
			} else {
				next_work_slide = current_work_slide - 1;
			}						
		} else if($(this).hasClass('next')) {		
			if(current_work_slide == total_work_slides - 1) {
				next_work_slide = 0;
			} else {
				next_work_slide = current_work_slide + 1;
			}						
		}
		$('.latest-work-slider-cnt a').removeClass('active');
		$('.latest-work-slider-cnt a:eq(' + next_work_slide + ')').addClass('active');
		next_work_move = -(next_work_slide*work_slide_width);					
		$('.latest-work-slider-cnt').animate({left: next_work_move + "px"}, work_slider_speed);			
		return false;
	});


//Showcase Paging function 

	var showcase_paging_speed = 1000; // Adjust showcase paging fading speed

	$('#showcase .page-nav .pages a').click(function() {
		var current_page = $('#showcase .page-nav .pages a').index($('#showcase .page-nav .pages a.active'));	
		var next_page = $('#showcase .page-nav .pages a').index($(this));		
		if(current_page != next_page) {			
			if($('.projects-page:animated').length == 0) {				
				$('.projects-page:eq(' + current_page + ')').fadeOut(showcase_paging_speed);
				$('.projects-page:eq(' + next_page + ')').fadeIn(showcase_paging_speed);
				$('#showcase .page-nav .pages a.active').removeClass('active');
				$('#showcase .page-nav .pages a:eq(' + next_page + ')').addClass('active');
				$('span.current-page-cnt').text(next_page + 1);				
			}
		}
		return false;
	});
	
	
	
	/** Custom Select **/
	
	$('select.custom-select').each(function(){
		var options = $(this).find('option');

		var selected_value = options.eq(0).html();
		
		var options_html = '';
		options.each(function(){
			options_html += '<li><a href="#">' + $(this).html() + '</a></li>';
			if($(this).attr('selected'))
				selected_value = $(this).html();
		});
		
		var html = '';
		html += '<div class="select">';
		html += '<a href="#" class="select-value">' + selected_value + '</a>';
		html += '<div class="select-dd"><ul>';
		html += options_html;
		
		html += '</ul></div></div>';
		
		$(this).wrap('<div class="custom-select-holder">');
		$(this).before( html );
		
	});
	
	$('.select-value').live('click',function(e){
																					 
		e.preventDefault();
		
		var parent = $(this).parent();
		var select_dd = parent.find('.select-dd');
		
		if( select_dd.css('display') == 'none' ) {
			select_dd.show();
		}else {
			select_dd.hide();
		}
		
		$('body').click(function(){ select_dd.hide(); });
		
	});
	
	$('.select-dd').each(function(){
		var i=0;
		$(this).find('a').each(function(){
			this._index = i;
			i++;
		});
	})
	
	$('.select-dd a').live('click',function(e){
																	 
		e.preventDefault();
		
		var parent = $(this).parent().parent().parent().parent();
		var value = $(this).html();
		var index = this._index;
		var holder = parent.parent();
		
		parent.find('.select-value').html(value);
		parent.find('.select-dd').hide();
		
		holder.find('option').attr({ 'selected' : false });
		holder.find('option:eq('+index+')').attr({ 'selected' : true });
		if(global.calculate_cost_exists){
			global.calculate_cost();	
		}
	});


	
});


