var pkey = 0;
var portfolio;

$(document).ready(function() {
	$('#quote_type').selectmenu({style:'dropdown',menuWidth: 216, width:216});				   
						   
	Ajax({
		'method' : 'sitefunctions',
		'action' : 'getPortfolio',
		'callback' : function(response) {
			portfolio = response;
		},
		'class' : 'site'
	});
	
	$('#portfolio_img a.nav').click(function() {
		var classname = $(this).attr('class').split(' ').slice(-1); 
		if (classname == 'next') {
			pkey = (pkey == (portfolio.length-1)) ? 0 : pkey+1;
		}
			else {
				pkey = (pkey == 0) ? portfolio.length-1 : pkey-1
			}

		var id = name2url(portfolio[pkey]['title']);	
			
		$('#portfolio_img a:first-child').attr('title',portfolio[pkey]['title']);
		$('#portfolio_img a:first-child').attr('href',_DIR['TOP_LEVEL'] + 'portfolio/' + id + '/');
		$('#portfolio_img a:first-child img').attr('alt',portfolio[pkey]['title']);
		$('#portfolio_img a:first-child img').attr('src',_DIR['TEMPLATE_IMAGES'] + 'portfolio/sites/' + id + '/main.jpg');
			
		$('#portfolio_info a').html(portfolio[pkey]['title']);
		$('#portfolio_info a').attr('href',_DIR['TOP_LEVEL'] + 'portfolio/' + id + '/');
		$('#portfolio_info a').attr('title',portfolio[pkey]['title']);
		
		var description = portfolio[pkey]['description'].split('.');
		
		$('#portfolio_info p').html(description[0] + '.');
		
		return false;
	});
	
	$('form').submit(function () {
		var values = {
					'name' : {'type' : 'name'},
					'company' : {'type' : 'alphanum_s'},
					'website' : {'type' : 'url'},
					'email_address' : {'type' : 'email'},
					'phone_number' : {'type' : 'phone'},
					'quote_type' : {'type' : 'char'},
					'description' : {'type' : 'msg'}
					}
		var param = Validate.form(values,'quoteFrm','quote_error');
		if (param != false) {
			Ajax({
				'method' : 'sitefunctions',
				'action' : 'requestQuote',
				'parameters' : param,
				'callback' : function(response) {
					hideloader('quote_loader','quoteFrm');
					if (response['status'] === true) {
						clearform('quoteFrm');
						seterror('quote_error','Request has been sent. You will be hearing from us shortly.');
					}	
						else {
							if (response['error']) {
								seterror('quote_error',response['error']);
							}
								else {
									seterror('quote_error','There was an error. Please try again.');
								}
						}
				},
				'class' : 'site',
				'error' : 'quote_error',
				'loader' : {'form' : 'quoteFrm', 'div' : 'quote_loader'}
			});
		}
		return false;
	});	
});
