// set up the global ajax path
var ajaxpath = rel_url+'controllers/ajax/';

/**
 * add custom methods
 * for form validation here
 */ 
$.validator.addMethod('phone', function(_v, _e) {
	var re = /\(\d{3}\)\s?\d{3}\-\d{4}/;
	
	// only validate if string is present
	if(_v.length > 0) {
		if(_v.search(re) < 0) {
			return false;
		}
	}
	
	return true;
}, 'Please, provide a valid phone number (555) 555-5555');

/**
 * only allow up to N keywords
 */
$.validator.addMethod('keyword_limit', function(_v, _e, _p) {
	var words_array = _v.split(',');
	
	if(words_array.length > _p) {
		return false;
	}
	
	return true;
}, 'You have too many keywords.');

// validate focal length 000 mm
$.validator.addMethod('focal', function(_v, _e) {
	var re = /\d{2,4}(\.\d{1})?\smm/;
	
	if(_v.length > 0) {
		if(_v.search(re) < 0) {
			return false;
		}
	}
	
	return true;
}, 'Must be a valid focal length (i.e. 50 mm)');

// validate shutter speed format 
$.validator.addMethod('shutter', function(_v, _e) {
	var re = /(1\/)?\d{1,4}/;
	
	if(_v.length > 0) {
		if(_v.search(re) < 0) {
			return false;
		}
	}
	
	return true;
}, 'Must be a valid shutter speed(i.e. 1/200)');

// validate ISO setting 000 or 0000
$.validator.addMethod('iso', function(_v, _e) {
	var re = /\d{3,4}/;
	
	if(_v.length > 0) {
		if(_v.search(re) < 0) {
			return false;
		}
	}
	
	return true;
}, 'Must be a valid ISO number.');

// validate aperture value 00.0
$.validator.addMethod('aperture', function(_v, _e) {
	var re = /\d{1,2}(\.\d{1})?/;
	
	if(_v.length > 0) {
		if(_v.search(re) < 0) {
			return false;
		}
	}
	
	return true;
}, 'Must be a valid aperture setting (i.e. 4.5).');

$.validator.addMethod('currency', function(_v, _e) {
	var re = /(\d{1,4})?\.\d{2}/;
	
	if(_v.length > 0) {
		if(_v.search(re) < 0) {
			return false;
		}
	}
	
	return true;
}, 'Must be valid currency format (i.e. 10.51)');

/**
 * sets up:
 * 1. the main menu drop downs
 * 2. the global ajax loader
 */
$(function() {
	$.fck.config = {
			ToolbarSet: 'DPI',
			Width: '375px',
			Height: '215px',
			ImageBrowser: true,
			ImageUpload: true
		}

	$('textarea.fck').fck({
			path: rel_url + 'fckeditor/'
	});
	
	if($('a#signinclick').length > 0) {
		//$('#signinform').hide();
		$('#signinform').css({
			left: $('#signinform').prev('a#signinclick').position().left + 'px'
		});
	}
	
	//$('#ajaxloader').hide();
	$('#ajaxloader').ajaxStart(function() {
		$(this).css({display:'block'}).fadeIn('slow');
	});
	$('#ajaxloader').ajaxStop(function() {
		$(this).fadeOut('slow', function() {
			$(this).css({display:'none'});
		});
	});
	$('#ajaxloader').css({
		left: ($(document).width() / 2) + 'px',
		top: ($(document).height() / 2) + 'px'
	});
	
	$('a#signinclick').toggle(global_showloginform, global_hideloginform);
	
	$('ul#animated-panorama').animatedinnerfade({
		speed: 2000,
		timeout: 15000,
		type: 'sequence',
		containerheight: '402px',
		containerwidth: '600px',
		animationSpeed: 20000,
		animationtype: 'fade',
		// bgFrame: rel_url + 'img/frame-black.gif',
		bgFrame: 'none',
		controlBox: 'none',
		controlBoxClass: 'mycontrolboxclass',
		controlButtonsPath: 'img',
		displayTitle: 'yes' 
	});

	$('ul#animated-portfolio').animatedinnerfade({
		speed: 1000,
		timeout: 5000,
		type: 'random',
		containerheight: '300px',
		containerwidth: '270px',
		animationSpeed: 5000,
		animationtype: 'fade',
		bgFrame: 'none',
		controlBox: 'none',
		displayTitle: 'none'
	});
	
	$(".nav")
	.superfish({
		pathClass : 'current',
		animation : {opacity:'show'},
		delay : 1000
	});
});

function global_showloginform() {
	var curlink = $(this);
	
	$(curlink).html('Cancel');
	
	$(curlink).siblings('#signinform').fadeIn('slow', function() {
		$('form#loginForm').validate({
			rules: {
				userName: {required: true},
				userPass: {required: true}
			},
			submitHandler: global_procsignin
		});
	});
}

function global_hideloginform() {
	$(this).html('Sign In');
	
	$(this).siblings('#signinform').fadeOut('slow', function() {})
}

function global_procsignin() {

$.getJSON(secure_url + "login.php?jsonp=?",$('form#loginForm').serialize(), function(t) {
		if(t.login_success == 1) {
			window.location.href = t.http_referer; // redirect	
			
		} else {
			$('<div />').attr({
				'class': 'site-error'
			}).html('Login failed.').prependTo($('div#signinform')).fadeIn('slow');
		}
	});
}

function global_addToCart(_id, _o) {
	$.post(ajaxpath + 'cart.php', {
		mode: 'item',
		task: 'add',
		id: _id
	}, function(t) {
		$('<div />').attr({
			'class': 'cart-confirm'
		}).html('Item Added To Cart.').appendTo($(_o)).fadeIn('slow');		
	});
}

function global_preloadimages(_o, _precallback, _postcallback) {
	var imgary = new Array();
	
	$.each(_o, function() {
		imgary.push($(this).attr('src'));
	});
	
	$.preload(imgary, {
		onComplete: function(_t) {
			if(_t.done == _t.total) {
				_postcallback();
			} else {
				_precallback();
			}
		}
	});
}
