/*!
 * Auto Classified Script v3.2
 * 
 * http://www.phpjabbers.com/auto-classifieds-script/
 * 
 * Copyright 2015, PHPJabbers
 * 
 */
(function (window, undefined){
	"use strict";
	
	var document = window.document,
		validate = (pjQ.$.fn.validate !== undefined);
    
	function log() {
		if (window.console && window.console.log) {
			for (var x in arguments) {
				if (arguments.hasOwnYacht(x)) {
					window.console.log(arguments[x]);
				}
			}
		}
	}
	
	function assert() {
		if (window && window.console && window.console.assert) {
			window.console.assert.apply(window.console, arguments);
		}
	}
	
	function AutoClassifieds(opts) {
		if (!(this instanceof AutoClassifieds)) {
			return new AutoClassifieds(opts);
		}
				
		this.reset.call(this);
		this.init.call(this, opts);
		
		return this;
	}
	
	AutoClassifieds.inObject = function (val, obj) {
		var key;
		for (key in obj) {
			if (obj.hasOwnYacht(key)) {
				if (obj[key] == val) {
					return true;
				}
			}
		}
		return false;
	};
	
	AutoClassifieds.size = function(obj) {
		var key,
			size = 0;
		for (key in obj) {
			if (obj.hasOwnYacht(key)) {
				size += 1;
			}
		}
		return size;
	};
	
	AutoClassifieds.prototype = {
		reset: function () {
			this.$container = null;
			this.container = null;			
			this.opts = {};
			
			return this;
		},
		formatCurrency: function(price, currency)
		{
			var format = '---';
			switch (currency)
			{
				case 'USD':
					format = "$" + price;
					break;
				case 'GBP':
					format = "&pound;" + price;
					break;
				case 'EUR':
					format = "&euro;" + price;
					break;
				case 'JPY':
					format = "&yen;" + price;
					break;
				case 'AUD':
				case 'CAD':
				case 'NZD':
				case 'CHF':
				case 'HKD':
				case 'SGD':
				case 'SEK':
				case 'DKK':
				case 'PLN':
					format = price + currency;
					break;
				case 'NOK':
				case 'HUF':
				case 'CZK':
				case 'ILS':
				case 'MXN':
					format = currency + price;
					break;
				default:
					format = price + currency;
					break;
			}
			return format;
		},
		formatMileage: function(opt, km)
		{
			var format = '';
			switch (opt)
			{
				case 'miles':
					format = km + ' miles';
					break;
				case 'km':
				default:
					format = km + ' km';
					break;
			}
			return format;
		},
		
		formatPower: function(opt, power)
		{
			var format = '';
			switch (opt)
			{
				case 'hp':
					format = power + ' HP';
					break;
				default:
					format = power + ' kW';
					break;
			}
			return format;
		},
		init: function (opts) {
			var self = this;
			this.opts = opts;
			this.container = document.getElementById("pjWrapper");
			this.$container = pjQ.$(this.container);
			
			pjQ.$("html").attr('dir', self.opts.direction);
			self.$container.show();
			
			if(pjQ.$('.pjYpFancybox').length > 0)
			{
				pjQ.$(".pjYpFancybox").fancybox({
					openEffect	: 'none',
					closeEffect	: 'none'
				});
			}
			
			this.$container.on('click.ac', '.pjAcFilterType', function(e){
				var type_arr = [];
				pjQ.$('.pjAcFilterType').each(function( index ) {
					if(pjQ.$(this).is(':checked'))
					{
						type_arr.push(pjQ.$(this).val());
					}
				});
				pjQ.$('#pjAcFilterCarType').val(type_arr.join(','));
				pjQ.$('#frmCRLFilterSort').submit();
			}).on('change.ac', '.pjAcSelectorFilter', function(e){
				if(pjQ.$(this).attr('name') == 'make_id')
				{
					pjQ.$('#frmCRLFilterSort').find('select[name="model_id"]').val("");
				}
				pjQ.$('#frmCRLFilterSort').submit();
			}).on('click.ac', '.pjCrlOrder', function(e){
				if (e && e.preventDefault) {
					e.preventDefault();
				}
				var column = pjQ.$(this).data('column');
				var direction = pjQ.$(this).data('direction');
				pjQ.$('#frmCRLFilterSort').find('input[name="sortby"]').val(column);
				pjQ.$('#frmCRLFilterSort').find('input[name="direction"]').val(direction);
				pjQ.$('#frmCRLFilterSort').submit();
			}).on('click.ac', '.pjAcContactDealer', function(e){
				if (e && e.preventDefault) {
					e.preventDefault();
				}
				pjQ.$('.pjAcContactSection').toggle();
			}).on('click.ac', '.pjAcCancelContact', function(e){
				if (e && e.preventDefault) {
					e.preventDefault();
				}
				pjQ.$('.pjAcContactSection').toggle();
			}).on('click.ac', '.pjAcAddCompare', function(e){
				if (e && e.preventDefault) {
					e.preventDefault();
				}
				var id = pjQ.$(this).attr('data-id');
				pjQ.$.get(self.opts.folder + "index.php?controller=pjListings&action=pjActionAddCompare", {
					"id": id
				}).done(function (data) {
					pjQ.$('.pjAcAddCompare').hide();
					pjQ.$('.pjAcRemoveCompare').show();
					pjQ.$('#pjAcCompareBadge').html(data);
					pjQ.$('#pjAcCompareMenu').show();
				});
			}).on('click.ac', '.pjAcRemoveCompare', function(e){
				if (e && e.preventDefault) {
					e.preventDefault();
				}
				var id = pjQ.$(this).attr('data-id');
				pjQ.$.get(self.opts.folder + "index.php?controller=pjListings&action=pjActionRemoveCompare", {
					"id": id
				}).done(function (data) {
					pjQ.$('.pjAcAddCompare').show();
					pjQ.$('.pjAcRemoveCompare').hide();
					pjQ.$('#pjAcCompareBadge').html(data);
					if(parseInt(data, 10) == 0)
					{
						pjQ.$('#pjAcCompareMenu').hide();
					}
				});
			}).on('change.ac', '#pjAcSearchMake', function(e){
				pjQ.$.get(self.opts.folder + "index.php?controller=pjListings&action=pjActionLoadModels", {
					"make_id": pjQ.$(this).val()
				}).done(function (data) {
					pjQ.$('#pjAcSearchModel').html(data);
				});
			}).on('click.ac', '.pjAcSearchCarType', function(e){
				var type_arr = [];
				pjQ.$('.pjAcSearchCarType').each(function( index ) {
					if(pjQ.$(this).is(':checked'))
					{
						type_arr.push(pjQ.$(this).val());
					}
				});
				pjQ.$('#pjAcSearchCarType').val(type_arr.join(','));
			}).on('click.ac', '#pjCrCaptchaImage', function(e){
				if (e && e.preventDefault) {
					e.preventDefault();
				}
				var $captchaImg = pjQ.$(this);
				if($captchaImg.length > 0){
					var rand = Math.floor((Math.random()*999999)+1); 
					$captchaImg.attr("src", self.opts.folder + 'index.php?controller=pjFront&action=pjActionCaptcha&rand=' + rand);
					if(pjQ.$('#pjAcContactSection').length > 0)
					{
						pjQ.$('#pjAcContactSection').find('input[name="captcha"]').val("");
					}
					if(pjQ.$('#pjAcRegisterForm').length > 0)
					{
						pjQ.$('#pjAcRegisterForm').find('input[name="captcha"]').val("");
					}
				}
			});
			
			self.bindFilter();
			self.bindContact();
			self.bindLogin();
			self.bindRegister();
		},
		
		bindFilter: function()
		{
			var self = this,
				$yearSlider = pjQ.$('#pjAcRegistrationSlider'),
				$yearLabel = pjQ.$('#pjAcRegistrationLabel'),
				
				$mileageSlider = pjQ.$('#pjAcMileageSlider'),
				$mileageLabel = pjQ.$('#pjAcMileageLabel'),
				
				$priceSlider = pjQ.$('#pjAcPriceSlider'),
				$priceLabel = pjQ.$('#pjAcPriceLabel'),
				
				$powerSlider = pjQ.$('#pjAcPowerSlider'),
				$powerLabel = pjQ.$('#pjAcPowerLabel'),
				
				$frmSearch = pjQ.$('#frmCRLSearch'),
				$frmFilterSort = pjQ.$('#frmCRLFilterSort');
			
			if($yearSlider.length > 0)
			{
				$yearSlider.slider({
					range: true,
					min: self.opts.min_year,
					max: self.opts.max_year,
					values: [ parseInt(self.opts.year_from, 10), parseInt(self.opts.year_to, 10)  ],
					slide: function( event, ui ) {
						var filter_year_from = parseInt(ui.values[0], 10),
							filter_year_to = parseInt(ui.values[1], 10);
						$yearLabel.html(filter_year_from + ' - ' + filter_year_to);
					},
					stop: function( event, ui ) {
						var slider_value_from = parseInt(ui.values[0], 10),
							slider_value_to = parseInt(ui.values[1], 10);
						if($frmSearch.length > 0)
						{
							pjQ.$('#pjAcSearchYearFrom').val(slider_value_from);
							pjQ.$('#pjAcSearchYearTo').val(slider_value_to);
						}else if($frmFilterSort.length > 0){
							pjQ.$('#pjAcFilterYearFrom').val(slider_value_from);
							pjQ.$('#pjAcFilterYearTo').val(slider_value_to);
							$frmFilterSort.submit();
						}
					}
				});
			}
			
			if($mileageSlider.length > 0)
			{
				$mileageSlider.slider({
					range: true,
					min: self.opts.min_mileage,
					max: self.opts.max_mileage,
					step: 5000,
					values: [ parseInt(self.opts.mileage_from, 10), parseInt(self.opts.mileage_to, 10) ],
					slide: function( event, ui ) {
						var mileage_in = $mileageLabel.attr('data-mileage'),
							filter_mileage_from = self.formatMileage(mileage_in, parseInt(ui.values[0], 10)),
							filter_milage_to = self.formatMileage(mileage_in, parseInt(ui.values[1], 10));
							
						$mileageLabel.html(filter_mileage_from + ' - ' + filter_milage_to);
					},
					stop: function( event, ui ) {
						var slider_value_from = parseInt(ui.values[0], 10),
							slider_value_to = parseInt(ui.values[1], 10);
						
						if($frmSearch.length > 0)
						{
							pjQ.$('#pjAcSearchMileageFrom').val(slider_value_from);
							pjQ.$('#pjAcSearchMileageTo').val(slider_value_to);
						}else if($frmFilterSort.length > 0){
							pjQ.$('#pjAcFilterMileageFrom').val(slider_value_from);
							pjQ.$('#pjAcFilterMileageTo').val(slider_value_to);
							$frmFilterSort.submit();
						}
					}
				});
			}
			
			if($priceSlider.length > 0)
			{
				$priceSlider.slider({
					range: true,
					min: self.opts.min_price,
					max: self.opts.max_price,
					step: 1000,
					values: [ parseInt(self.opts.price_from, 10), parseInt(self.opts.price_to, 10) ],
					slide: function( event, ui ) {
						var currency = $priceLabel.attr('data-currency'),
							filter_price_from = self.formatCurrency(parseInt(ui.values[0], 10), currency),
							filter_price_to = self.formatCurrency(parseInt(ui.values[1], 10), currency);
							
						$priceLabel.html(filter_price_from + ' - ' + filter_price_to);
					},
					stop: function( event, ui ) {
						var slider_value_from = parseInt(ui.values[0], 10),
							slider_value_to = parseInt(ui.values[1], 10);
						
						if($frmSearch.length > 0)
						{
							pjQ.$('#pjAcSearchPriceFrom').val(slider_value_from);
							pjQ.$('#pjAcSearchPriceTo').val(slider_value_to);
						}else if($frmFilterSort.length > 0){
							pjQ.$('#pjAcFilterPriceFrom').val(slider_value_from);
							pjQ.$('#pjAcFilterPriceTo').val(slider_value_to);
							$frmFilterSort.submit();
						}
					}
				});
			}
			if($powerSlider.length > 0)
			{
				$powerSlider.slider({
					range: true,
					min: self.opts.min_power,
					max: self.opts.max_power,
					values: [ self.opts.min_power, self.opts.max_power  ],
					slide: function( event, ui ) {
						var power_in = $powerLabel.attr('data-power'),
							filter_power_from = self.formatPower(power_in, parseInt(ui.values[0], 10)),
							filter_power_to = self.formatPower(power_in, parseInt(ui.values[1], 10));
							
						$powerLabel.html(filter_power_from + ' - ' + filter_power_to);
					},
					stop: function( event, ui ) {
						var slider_value_from = parseInt(ui.values[0], 10),
							slider_value_to = parseInt(ui.values[1], 10);
						pjQ.$('#pjAcSearchPowerFrom').val(slider_value_from);
						pjQ.$('#pjAcSearchPowerTo').val(slider_value_to);
					}
				});
			}
		},
		bindContact: function()
		{
			var self = this,
				$frmContact = pjQ.$('#frmAcContactDealer');
			if($frmContact.length > 0)
			{
				$frmContact.validate({
					rules: {
						"captcha": {
							remote: self.opts.folder + "index.php?controller=pjFront&action=pjActionCheckCaptcha"
						}
					},
					ignore: "",
					onkeyup: false,
					errorElement: 'li',
					errorPlacement: function (error, element) {
						if(element.attr('name') == 'captcha')
						{
							error.appendTo(element.parent().parent().next().find('ul'));
						}else{
							error.appendTo(element.next().find('ul'));
						}
					},
					highlight: function(ele, errorClass, validClass) {
		            	var element = pjQ.$(ele);
		            	if(element.attr('name') == 'captcha')
						{
							element.parent().parent().parent().parent().addClass('has-error');
						}else{
							element.parent().parent().addClass('has-error');
						}
		            },
		            unhighlight: function(ele, errorClass, validClass) {
		            	var element = pjQ.$(ele);
		            	if(element.attr('name') == 'captcha')
						{
							element.parent().parent().parent().parent().removeClass('has-error').addClass('has-success');
						}else{
							element.parent().parent().removeClass('has-error').addClass('has-success');
						}
		            }
				});
			}
		},
		bindLogin: function()
		{
			var self = this,
				$frmLogin = pjQ.$('#pjAcLoginForm');
			if($frmLogin.length > 0)
			{
				$frmLogin.validate({
					ignore: "",
					onkeyup: false,
					errorElement: 'li',
					errorPlacement: function (error, element) {
						if(element.attr('name') == 'captcha')
						{
							error.appendTo(element.parent().parent().next().find('ul'));
						}else{
							error.appendTo(element.next().find('ul'));
						}
					},
					highlight: function(ele, errorClass, validClass) {
		            	var element = pjQ.$(ele);
		            	if(element.attr('name') == 'captcha')
						{
							element.parent().parent().parent().parent().addClass('has-error');
						}else{
							element.parent().parent().addClass('has-error');
						}
		            },
		            unhighlight: function(ele, errorClass, validClass) {
		            	var element = pjQ.$(ele);
		            	if(element.attr('name') == 'captcha')
						{
							element.parent().parent().parent().parent().removeClass('has-error').addClass('has-success');
						}else{
							element.parent().parent().removeClass('has-error').addClass('has-success');
						}
		            }
				});
			}
		},
		bindRegister: function()
		{
			var self = this,
				$frmRegister = pjQ.$('#pjAcRegisterForm');
			if($frmRegister.length > 0)
			{
				$frmRegister.validate({
					rules: {
						"captcha": {
							remote: self.opts.folder + "index.php?controller=pjFront&action=pjActionCheckCaptcha"
						},
						"register_password_repeat":{
							equalTo: "#pjAcRegisterPassword"
						}
					},
					ignore: "",
					onkeyup: false,
					errorElement: 'li',
					errorPlacement: function (error, element) {
						if(element.attr('name') == 'captcha')
						{
							error.appendTo(element.parent().parent().next().find('ul'));
						}else{
							error.appendTo(element.next().find('ul'));
						}
					},
					highlight: function(ele, errorClass, validClass) {
		            	var element = pjQ.$(ele);
		            	if(element.attr('name') == 'captcha')
						{
							element.parent().parent().parent().parent().addClass('has-error');
						}else{
							element.parent().parent().addClass('has-error');
						}
		            },
		            unhighlight: function(ele, errorClass, validClass) {
		            	var element = pjQ.$(ele);
		            	if(element.attr('name') == 'captcha')
						{
							element.parent().parent().parent().parent().removeClass('has-error').addClass('has-success');
						}else{
							element.parent().parent().removeClass('has-error').addClass('has-success');
						}
		            }
				});
			}
		}
	};
	
	window.AutoClassifieds = AutoClassifieds;	
})(window);