$(document).ready(function () {
	$('.reply').click(function () { return false; });

	// Slider
	$.fn.cycle.transitions.ears = function(cont, slides, opts) {	
	    opts.before.push(function(curr, next, opts) {
			$('#slider img').hide();
	      	
			$(this).find('.content_left').find('.wrap').css({
				'marginLeft': '210px'
			});
			
	  		$(this).find('.content_left').find('.wrap').animate({
				'marginLeft': '-=207px'
			}, 1000);
			
	      	$(this).find('.content_right').find('.wrap').css({
				'marginLeft': '-210px'
			});
	  		
			$(this).find('.content_right').find('.wrap').animate({
				'marginLeft': '+25px'			
			}, 1000);
			
			$(this).find('img').fadeIn();									
	      	$(this).show();
	      	
	    $('#slider .item').each(function () {
	       $(this).css({'z-index': '1'});
	    });
	    $(this).css({'z-index': '2'});
	    
		});
		
		opts.fxFn = function(curr, next, opts, cb, fwd) {
	  		$(curr).find('.content_left').find('.wrap').animate({
					'marginLeft': '+=210px'
				}, 500);
			
	  		$(curr).find('.content_right').find('.wrap').animate({
					'marginLeft': '-=210px'
				}, 500);	
			
			if (cb) cb();
		};
	};	
	
	// Homepage slider definition
	$('#slider').cycle({
		fx: 		'ears', 			// Effect
		next:   	'#next', 			// Div ID for next arrow 
		prev:   	'#prev',			// Div ID for previous arrow
		pager: 		'#slider_pager', 	// Div ID for bullets
		timeout: 	5000,				// Slideshow speed
		pagerAnchorBuilder: function(idx, slide) { 
		        return '<a href="#" title="' + (idx + 1) + '"><img src="wp-content/themes/glamorous/images/slider-bullet-light.png" width="13" height="13" alt="' + idx + '" /></a>'; 
		},
		updateActivePagerLink: function(pager, currSlide, clsName) {
			$(pager).each(function() {
				$(this).find('a').find('img').attr('src', 'wp-content/themes/glamorous/images/slider-bullet-light.png').parent().filter('a:eq('+currSlide+')').find('img').attr('src', 'wp-content/themes/glamorous/images/slider-bullet-dark.png');
				$('#slider_pager a img').ifixpng();
			});			
		}
	});	
		
	// Font replacement
	Cufon.replace('.title');
	Cufon.replace('#header_sub h1');	
	
	
	// Menu hover effect
	$('.menu li').hover(
		function () { 
			$(this).find('ul:first').css({'left': 'auto'});
		}, 
		function () {
			$(this).find('ul:first').css({'left': '-9999px'}); 
		}
	);	
	$('.menu ul ul').hover(
		function () {
			$(this).parent().find('a:first').css({'backgroundColor': '#' + $.cookie('glamorous_color')});
		}, 
		function () {
			$(this).parent().find('a:first').css({'backgroundColor': ''});
	});
	
	// Form validation
	$('#contact_form').validate();	

	// Set variation color
	function newVariation(hex) {
		// Default color
		if (!hex) hex = '238EA8';

		$('#bg_top').css({'backgroundColor': '#' + hex});
		$('#bg_header').css({'backgroundColor': '#' + hex});
		$('#header_sub h1').css({'color': '#' + hex});
		$('#main_menu .menu ul').css({
			'borderColor': '#' + hex,
			'backgroundColor': '#' + hex
		});
		$('#main_menu .menu li li a').hover(
			function () {
				$(this).css({'backgroundColor': '#' + hex})
			},
			function () {
				$(this).css({'backgroundColor': 'transparent'})
			}
		);
		Cufon.replace('#header_sub h1');
		return false; 	
	}
	
	// Set color variation from cookie
	if ($.cookie('glamorous_color')) {
		newVariation($.cookie('glamorous_color'));
	}
	
	// Reset color variations to default
	$('#reset_color').click(function () {
		//$.cookie('glamorous_color', '238EA8'); 
		//$('#color_picker div').css('backgroundColor', '#' + '238EA8');
		$.cookie('glamorous_color', null);
		newVariation();
		return false;
	});
	
	if ($.cookie('glamorous_color')) 
		$('#color_picker div').css('backgroundColor', '#' + $.cookie('glamorous_color'));
	else {
		//$.cookie('glamorous_color', '238EA8');
		$('#color_picker div').css('backgroundColor', '#' + '238EA8');
	}
	
	// Color picker
	$('#color_picker').ColorPicker({
		color: '#238ea8',
		onHide: function (colpkr) {
			$(colpkr).fadeOut(500);
			return false;
		},
		onSubmit: function(hsb, hex, rgb, el) {
			$(el).val('#' + hex);
			$(el).ColorPickerHide();
			$.cookie('glamorous_color', hex);
			$('#color_picker div').css('backgroundColor', '#' + hex);
		},
		onBeforeShow: function () {
			if ($.cookie('glamorous_color')) {
				$(this).ColorPickerSetColor($.cookie('glamorous_color'));
			}
			else {
				$(this).ColorPickerSetColor('#238ea8');
			}
		},
		onChange: function (hsb, hex, rgb) {
			newVariation(hex);
			$.cookie('glamorous_color', hex);
			$('#color_picker div').css('backgroundColor', '#' + hex);		
		}
	})
	.bind('keyup', function(){
		$(this).ColorPickerSetColor(this.value);
	});
		
	// AJAX form
	$('form#contact_form').submit(function() {
		if ($('label.error:visible').length !== 0) {
			return false;
		}
		
		var s = $(this).serialize();
		
		if (($(this).attr('action') === '') || ($(this).attr('action')==='#')) 
			action = '?'; 
		else 
			action = $(this).attr('action');

		$.ajax({
			type: $(this).attr('method'),
			data: s,
			url: action,
			success: function(result) {
				if (result == '1') {
					alert('E-mail sent');
					$('form#contact_form')[0].reset();
				}
				else { 
					alert('E-mail can not be sent!');
				}
				
				return false;
			}
		});	

		return false;
	});	
	
	// Submenu hover
	$('#content .box .list li a').hover(
		function () { 
			$(this).css({'color': '#' + $.cookie('glamorous_color') });
		}, 
		function () {
			$(this).css({'color': '#666666' });
		}
	);	
	
	// Comment decoration
	$('.comment-body').each(function () {
		var reply = $(this).find('.reply');
		$(this).remove('.reply');
		$(this).find('.vcard').prepend(reply);
	});	
});





