/**
 * This project is created by
 * PILLAX (http://www.pillax.com)
 */

/*******************************************************************
 * Quick search drodowns - START
 */
$(function(){

	$("#s1 a").click(function(){

		var id = $(this).attr('rel');

		$('#s2 a').remove();

		$.get('ajax_quick_search_elements.php',
			  {id:id}, 
			  function(data){
				$('#s2').append(data);
			  }
		)
	});
});

/**
 * Quick search drodowns - END
 ******************************************************************/


/*******************************************************************
 * Search box - START
 */
$(function(){
	$('#navigator .submit').click(function(){
		window.location = 'cat.php?category=' + $('#navigator #s1 b').text() + '&make=' + $('#navigator #s2 b').text() + '&is_new=' + $('#navigator #s3 b').text();
	});
});
/*
 * Search box - END
 *******************************************************************/

/*******************************************************************
 * bgMapCoord - START
 */
$(function(){

	var orig_span	= $('#obl span').text();
	var orig_b		= $('#obl b').text();

	$('#bgMapImg').maphilight();

	$('#obl a').click(function(){
		orig_span		= $(this).html();
		orig_b			= $(this).attr('rel');

		if(navigator.appName != 'Microsoft Internet Explorer')
		{
			history.pushState(
				null, 
				'zzz', 
				'contact.php?language=' + $('#lng').val() + '&obl=' + orig_b
			);
		}
	});

	$('#obl a').hover(
		function(){
			$('#a'+$(this).attr('rel')).mouseover();
		},
		function(){
			$('#a'+$(this).attr('rel')).mouseout();
		}
	);

	$('#obl a').click(function(){
		getOffices($(this).attr('rel'), $('#type b').text());
	});

	$('a#sofia').click(function(){
		getOffices($(this).attr('rel'), 0);
		$(this).blur();
		return false;
	});

	$('#type a').click(function(){
		if($('#obl b').text() != '-1')
		{
			getOffices($('#obl b').text(), $(this).attr('rel'));
		}
	});

	$('#bgMap area').hover(
		function(){
			$('#obl span').text($(this).attr('title'));
			$('#obl b').text($(this).attr('alt'));
		},
		function(){
			$('#obl span').text(orig_span);
			$('#obl b').text(orig_b);
		}
	);

	$('#bgMap area').click(function(){
		orig_span	= $(this).attr('title');
		orig_b		= $(this).attr('alt');
		$('#obl span').text(orig_span);
		$('#obl b').text(orig_b);
		getOffices(orig_b, $('#type b').text());

		if(navigator.appName != 'Microsoft Internet Explorer')
		{
			history.pushState(
				null, 
				'zzz', 
				'contact.php?language=' + $('#lng').val() + '&obl=' + orig_b
			);
		}

		return false;
	});

	//$('#bgMap #a22').click();
});

function getOffices(obl, type)
{
	$.post(
		'ajax_contact.php?language=' + $('#lng').val(),
		{obl:obl, type:type}, 
		function(data)
		{
			$('#contact #content').html(data);
		}
	);
}

/*
 * bgMapCoord - END
 *******************************************************************/

/*******************************************************************
 * Select - START
 */
$(function(){

	$('.select').hover(
		function()
		{
			$('a', this).css('display', 'block');
		},
		function()
		{
			$('a', this).css('display', 'none');
		}
	);

	$('.select a').live('click', function(){
		$('span', $(this).parent()).text($(this).text());
		$('b', $(this).parent()).text($(this).attr('rel'));
		$('a', $(this).parent()).css('display', 'none');
		return false;
	});
});
/*
 * Select - END
 *******************************************************************/

/*******************************************************************
 * Box - START
 */
$(function(){

	$('.bNav').hover(
		function()
		{
			$('a', $(this).parent()).removeClass('active');
			$(this).addClass('active');

			$('.entry', $(this).parent().parent()).removeClass('active');
			$('.entry:eq(' +  $(this).attr('rel') + ')', $(this).parent().parent()).addClass('active');
		},
		function()
		{
			
		}
	);

	$('.bNav').click(function(){
		$(this).blur();
		return false;
	});
});
/*
 * Box - END
 *******************************************************************/

/*******************************************************************
 * Common - START
 */
$(function(){

	// IE7 top menu 
	$('.dropdown').hover(
		function()
		{
			$('.innerPage').css({'z-index': '-1'});
		},
		function()
		{
			$('.innerPage').css({'z-index': '0'});
		}
	);


	$('.vRow a').click(function(){

		var title = $(this).attr('title');
		$('div#myContainer').html($('.vContent', $(this).parent()).html());

		$('div#myContainer').dialog({
			resizable: true,
			modal: true,
			width: 510,
			title: title,
			buttons: {
				'Close': function()
						  {
							$(this).dialog('close');
						  }
			}
		});

		$(this).blur();
		return false;
	});

	$('.submit').click(function(){
		$(this).blur();
		return false;
	});

	$('div#filter .submit').click(function(){
		$('#filterForm').submit();
	});

	$('#brandsCont').jCarouselLite({
		btnNext: '#brandsNext',
		btnPrev: '#brandsPrev',
		visible: 6
	});

	$('#smallImgCont').jCarouselLite({
		btnNext: '#sINext',
		btnPrev: '#sIPrev',
		visible: 4
	});

	$('#gSmallImgCont').jCarouselLite({
		btnNext: '#gSINext',
		btnPrev: '#gSIPrev',
		visible: 4,
		circular: false
	});
	$('#gSINext, #gSIPrev').click(function(){
		$(this).blur();
		return false;
	});

	$('#gSmallImgCont a').click(function(){
		$('#gMainImg').attr('src', 'pi_files/big_' + $(this).attr('rel'));
		$('#gMainImg').attr('alt', $(this).attr('title'));
		$(this).blur();
		return false;
	});

	$('.video').click(function(){

		var title	= $(this).attr('title');
		var url		= $(this).attr('href');

		$('div#myContainer').html(
			  '<div id="video_player"></div>'
			+ '<script type="text/javascript">'
			+ '  var flashvars = {"file":"' + url + '", "autostart":"true"};'
			+ '  var params = {"movie":"player.swf", "allowfullscreen":"true", "allowscriptaccess":"always"};'
			+ '  var attributes = {};'
			+ '  swfobject.embedSWF("player.swf", "video_player", "467", "275", "9.0.0", false, flashvars, params, attributes);'
			+ '</script>'
		);

		$('div#myContainer').dialog({
			resizable: true,
			modal: true,
			width: 500,
			title: title,
			buttons: {
				'Затвори': function()
						  {
							$(this).dialog('close');
						  }
			}
		});

		$(this).blur();
		return false;
	});


	$('#pShare').click(function(){

		$('div#myContainer').html(
			  '<form id="mailForm">'
				+ '<b>Email-а на човека, който ще получи препратка към тази страница</b><br />'
				+ '<input type="text" class="normal" value="" name="email" id="email" />'
				+ '<br /><br />'
				+ '<input type="hidden" value="' + $(this).attr('rel') + '" name="machineId" id="machineId" />'
			+ '</form>'
		);

		$('div#myContainer').dialog({
			resizable: true,
			modal: true,
			title: 'Изпрати до приятел',
			buttons: {

				'Изпрати': function()
						  {
							$('input').removeClass('errField');

							var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
							if($('#email').val() == '' || reg.test($('#email').val()) == false)
							{
								$('#email').addClass('errField');
							}
							else
							{
								$.post(
									'ajax_sendShare.php',

									{a: $('#mailForm').serialize()}, 

									function(data)
									{
										if(data.status == 1)
										{
											// On ok
											$('div#myContainer').html('<div class="inf_msg" style="display: block;">' + data.msg + '</div>');
										}
										else
										{
											// On error
											$('div#myContainer').html('<div class="err_msg" style="display: block;">' + data.msg + '</div>');
										}
									},

									'json'
								);
							}
						  },

				'Откажи': function()
						  {
							$(this).dialog('close');
						  }
			}
		});

		$(this).blur();
		return false;
	});


	$('.sendMail').click(function(){

		var title		= '';
		var mailForm	= '';


		if($('#lng').val() == 'bg')
		{
			var lng_your_name			= 'Вашето име';
			var lng_content				= 'Запитване';
			var lng_mach_part			= 'Машина, за която търсите резервна част';
			var lng_title_machine		= 'Запитване за машина';
			var lng_title_part			= 'Запитване за резервна част';
			var lng_title_common		= 'Изпращане на съобщение';
			var lng_send				= 'Изпрати';
			var lng_cancel				= 'Откажи';
		}
		else
		{
			var lng_your_name			= 'Your name';
			var lng_content				= 'Content';
			var lng_mach_part			= 'Machine you are looking for a replacement part';
			var lng_title_machine		= 'Inquire about this machine';
			var lng_title_part			= 'Request for Replacement';
			var lng_title_common		= 'Send a message';
			var lng_send				= 'Send';
			var lng_cancel				= 'Cancel';
		}

		if($(this).hasClass('machine'))
		{
			mailForm = 
				  '<b>' + lng_your_name + '</b><br />'
				+ '<input type="text" class="normal" value="" name="name" id="name" />'
				+ '<br /><br />'

				+ '<b>Email</b><br />'
				+ '<input type="text" class="normal" value="" name="email" id="email" />'
				+ '<br /><br />'

				+ '<b>GSM</b><br />'
				+ '<input type="text" class="normal" value="" name="gsm" id="gsm" />'
				+ '<br /><br />'

				+ '<input type="hidden" value="' + $(this).attr('rel') + '" name="machineId" id="machineId" />'
				+ '<input type="hidden" value="machine" name="rType" id="rType" />'

				+ '<b>' + lng_content + '</b><br />'
				+ '<textarea class="normal" name="mailBody" id="mailBody" rows="" colls="" style="height: 130px;"></textarea>';

			title = lng_title_machine;
		}
		else if($(this).hasClass('parts'))
		{
			mailForm = 
				  '<b>' + lng_your_name + '</b><br />'
				+ '<input type="text" class="normal" value="" name="name" id="name" />'
				+ '<br /><br />'

				+ '<b>Email</b><br />'
				+ '<input type="text" class="normal" value="" name="email" id="email" />'
				+ '<br /><br />'

				+ '<b>GSM</b><br />'
				+ '<input type="text" class="normal" value="" name="gsm" id="gsm" />'
				+ '<br /><br />'

				+ '<b>' + lng_mach_part + '</b><br />'
				+ '<input type="text" class="normal" value="" name="machine" />'
				+ '<br /><br />'

				+ '<input type="hidden" value="parts" name="rType" id="rType" />'

				+ '<b>' + lng_content + '</b><br />'
				+ '<textarea class="normal" name="mailBody" id="mailBody" rows="" colls="" style="height: 130px;"></textarea>';

			title = lng_title_part;
		}
		else
		{
			mailForm = 
				  '<b>' + lng_your_name + '</b><br />'
				+ '<input type="text" class="normal" value="" name="name" id="name" />'
				+ '<br /><br />'

				+ '<b>Email</b><br />'
				+ '<input type="text" class="normal" value="" name="email" id="email" />'
				+ '<br /><br />'

				+ '<b>GSM</b><br />'
				+ '<input type="text" class="normal" value="" name="gsm" id="gsm" />'
				+ '<br /><br />'

				+ '<input type="hidden" value="global" name="rType" id="rType" />'

				+ '<b>' + lng_content + '</b><br />'
				+ '<textarea class="normal" name="mailBody" id="mailBody" rows="" colls="" style="height: 130px;"></textarea>';

			title = lng_title_common;
		}

		$('div#myContainer').html('<form id="mailForm">' + mailForm + '</form>');


		$('div#myContainer').dialog({
			resizable: true,
			modal: false,
			title: title,
			buttons: [
				{
					text: lng_send,
					click: function() {
							$('input, textarea').removeClass('errField');

							if($('#name').val() == '')
							{
								$('#name').addClass('errField');
							}
							else if($('#email').val() == '')
							{
								$('#email').addClass('errField');
							}
							else if($('#gsm').val() == '')
							{
								$('#gsm').addClass('errField');
							}
							else if($('#mailBody').val() == '')
							{
								$('#mailBody').addClass('errField');
							}
							else
							{
								$.post(
									'ajax_sendMail.php?language=' + $('#lng').val(),

									{a: $('#mailForm').serialize()}, 

									function(data)
									{
										if(data.status == 1)
										{
											// On ok
											$('div#myContainer').html('<div class="inf_msg" style="display: block;">' + data.msg + '</div>');
										}
										else
										{
											// On error
											$('div#myContainer').html('<div class="err_msg" style="display: block;">' + data.msg + '</div>');
										}
									},

									'json'
								);
							}
						  }
				},
				{
					text: lng_cancel,
					click: function() {
						$(this).dialog('close');
					}
				}
			]


		});

/*
		$('div#myContainer').dialog({
			resizable: true,
			modal: true,
			title: title,
			buttons: {

				lng_send: function()
						  {
							$('input, textarea').removeClass('errField');

							if($('#name').val() == '')
							{
								$('#name').addClass('errField');
							}
							else if($('#email').val() == '')
							{
								$('#email').addClass('errField');
							}
							else if($('#gsm').val() == '')
							{
								$('#gsm').addClass('errField');
							}
							else if($('#mailBody').val() == '')
							{
								$('#mailBody').addClass('errField');
							}
							else
							{
								$.post(
									'ajax_sendMail.php',

									{a: $('#mailForm').serialize()}, 

									function(data)
									{
										if(data.status == 1)
										{
											// On ok
											$('div#myContainer').html('<div class="inf_msg" style="display: block;">' + data.msg + '</div>');
										}
										else
										{
											// On error
											$('div#myContainer').html('<div class="err_msg" style="display: block;">' + data.msg + '</div>');
										}
									},

									'json'
								);
							}
						  },

				lng_cancel: function()
						  {
							$(this).dialog('close');
						  }
			}
		});
*/

		return false;
	});

});




