$(document).ready(function() {
	
	$('.top-bar ul li a').hover(function() {
		$('.top-bar').css('backgroundColor', $(this).css('color') );
	});
	
	$('form').each(function() {
		var $form = $(this);
		var $overlabel_fields = $form.find('p:not(.no-overlabel)');
		
		$overlabel_fields.find('label').overlabel();
	});
	
});

$(document).ready(function() {
	
	$('.homepage-slideshow').each(function(n) {
		$(this).addClass('slideshow-' + n);
		
		var $slideshow = $(this).find('.slideshow');
		var $pager = $('<div class="pager"><ul></ul></div>').insertAfter($slideshow).find('ul');
		var $modal = $(this).find('.modal');
		var $modal_content = $modal.find('.inside');
		
		$modal_content.cycle({
			'fx': 'fade',
			'timeout': 0,
			'speed': 0
		})
		
		$slideshow.cycle({
			'fx': 'fade',
			'pager': $pager,
			'pagerAnchorBuilder': function(idx, slide) {
				return '<li><a href="#">' + idx + '</a></li>'; 
			},
			'before': function() {
				var index = $slideshow.find('img').index( this );
				
				$modal_content.cycle( index );
			}
		})
		
	})
	
});


$(document).ready(function() {
	
	var selector = [
		['.top-bar ul li a', 'AvantGardeBold'],
		['.tabs-container .tab a', 'AvantGardeBold'],
		['.tabs-container .homepage-slideshow .modal .inside h2', 'AvantGardeBold'],
		['.tabs-container .homepage-slideshow .modal p.description', 'AvantGardeMedium'],
		['.tabs-container .homepage-slideshow .modal p.big', 'AvantGardeDemi'],
		['.tabs-container .homepage-slideshow .modal p.meta', 'AvantGardeMedium'],
		['.tabs-container .tab-content h1', 'AvantGardeDemi'],
		['.striped-box .inside h2', 'AvantGardeDemi'],
		['.footer p', 'AvantGardeExtraLight'],
		['form p.text label', 'AvantGardeMedium'],
		['form p.submit:not(.bold) button', 'AvantGardeMedium'],
		['form p.submit.bold button', 'AvantGardeBold']
	];
	
	_.each(selector, function(element) {
		Cufon.replace( element[0], {
			fontFamily: element[1],
			hover: true
		})
	});
	
	Cufon.now();
});

$(document).ready(function() {
	
	$('.tabs-container').each(function() {
		var $tabs = $(this).find('.tab');
		var $tabs_content = $(this).find('.tabs-content');
		var active = $tabs.index( $(this).find('.tab.active') );
		active = ( active < 0 ? 0 : active );
		
		if( $tabs_content.length > 0 ) {

			$tabs_content.cycle({
				'fx': 'fade',
				'speed': 0,
				'timeout': 0,
				'before': function() {
					$tabs_content.animate({
						'height': $(this).outerHeight()
					}, 0 );
				}
			});

			$tabs_content.cycle( active );
			$tabs.eq(active).addClass('active');
			
			$tabs.each(function(n) {
				var $this = $(this);
				
				$this.click(function() {
					$tabs_content.cycle(n);
					$tabs.removeClass('active');
					$(this).addClass('active');
					
					return false;
				})
				
			});
		}
	})
	
});

$(document).ready(function(){
    var images = [
    "images/controls.png",
    "images/loading.png",
    "images/loading2.png"
    ];

    for(i=0;i<images.length;i++)
    {
        var img = new Image();
        img.src=images[i];
    }

    $('#subscribe_form').validate({
        rules:{
            'name':{
                required:true
            },
            'email':{
                required:true,
                email:true
            }
        },
        messages:{
            'name':{
                required:"Please enter your name."
            },
            'email':{
                required:'Please enter a valid email.',
                email:'Please enter a valid email.'
            }
        },
        submitHandler:function(form){
            $.fn.colorbox({href:'processing.php',width:330,height:120,iframe:true,close:'',scrolling:false});

            $(form).ajaxSubmit({
                resetForm:true,
                clearForm:true,
                url:'addLead.php',
                method:'post',
                dataType:'json',
                beforeSubmit:function(options ,$form){
                    return true;
                },
                success:function(responseText ,statusText, xhr, $form){
                    if(responseText.result==1)
                        url = 'processed.php?result=success';
                    else
                        url = 'processed.php?result=failure&message='+responseText.message;

                    $.fn.colorbox({href:url,width:330,height:150,iframe:true,scrolling:false,close:'Close'});
                }
            });
        }
    });
});