$(function() {
		Cufon.replace('h2, h3, h4, .header label, .forsale-list .days, p.out, .send-to-btn',
						{fontFamily: 'vw-bold'}
					);
		Cufon.replace('h1',
				{fontFamily: 'vw-bold',
				textShadow: '1px 1px #981116'
				}
			);
		
		 Cufon.replace('.gallery h2',
							 {fontFamily: 'vw-bold',
							 textShadow: '1px 1px #000'
							 });
		
		
		Cufon.replace('.send-to-btn', {
			hover: true
		});
		Cufon('.send-to-btn', {
			hover: {
				color: '#ffffff'
			}
		});

		
		$('.send-to-btn').click(function(e)	{
			e.preventDefault();	
			$(this).next().slideDown("slow");
			$(this).addClass('pressed');
		});
		$('.cancel-to-send').click(function(e)	{
			e.preventDefault();
			slideUpForm($(this).parents('div').eq(0));
		});
		
		$('.submit-send').click(function(e)	{
			var options = { 
				url:        'http://www.vw-ultima.si/si/fox/send.php',
				dataType:	'json',
				beforeSubmit:  showRequest,  // pre-submit callback 
				success:       showResponse  // post-submit callback 
			};   
		   $(this).parents('form').eq(0).ajaxForm(options); 
		   $(this).css('background-position','0px -80px');
		}); 
		
		SetInputVal('.emailTo', 'Vpišite e-naslov prijatelja');
		SetInputVal('.nameTo', 'Vaše ime');
		SetInputVal('.sender', 'Vpišite ime prijatelja');
		
		function SetInputVal(what, inpVal) {
			$(what).focus(function(e)	{
				var test2 = $(this).val();
				if (test2 == inpVal) {
					$(this).val('');
				}
			}); 
			$(what).blur(function(e)	{
				var test = $(this).val();
				if (test == '') {
					$(this).val(inpVal);
				}
			}); 
		}
		
		function showRequest(formData, jqForm, options) { 
			var queryString = $.param(formData); 
			return true; 
		} 
		 
		function showResponse(responseText, statusText, xhr, $form)  { 
			if (responseText.errors.email == "invalid format" || responseText.errors.email == "required" ) {
				if ($form.find("p:last span").text() != "") {
					$form.find("p:last span").remove();
				}
				$form.find("p:last").append("<span>Potrebno je vpisati pravilni e-mail naslov</span>");
			} else if (responseText.errors.name == "required") {
				if ($form.find("p:last span").text() != "") {
					$form.find("p:last span").remove();
				}
				$form.find("p:last").append("<span>Potrebno je vpisati ime po&#353;iljatelja.</span>");
			} else if (responseText.errors.sender == "required" ) {
				if ($form.find("p:last span").text() != "") {
					$form.find("p:last span").remove();
				}
				$form.find("p:last").append("<span>Potrebno je vpisati ime prijatelja</span>");
			} else {
				if ($form.find("p:last span").text() != "") {
					$form.find("p:last span").remove();
				}
				$form.find("p:last").append("<span style='color:#000000'>Hvala! Va&#353;e sporo&#269;ilo je bilo poslano</span>");
				slideUpForm($form.parents('div').eq(0).delay(3000));
			}
			$form.find(".submit-send").css('background-position','0px -40px');
		}
		
		function slideUpForm(what) {
			what.slideUp('slow', function() {
				what.prev().removeClass('pressed');
				what.find('p:last span').remove();
				what.find('input.emailTo').val('Vpišite e-naslov prijatelja');
				what.find('input.nameTo').val('Vaše ime');
				what.find('input.sender').val('Vpišite ime prijatelja');
			});
			
		}
	
		$('a.bottom').click(function(e)	{
				var bottom = $('ul.secondary > li').length * $('ul.secondary > li:eq(0)').outerHeight();
				
				var ulist = $('ul.secondary');
				
				bottom -= ulist.innerHeight();
				
				ulist.animate({scrollTop: bottom}, 1000, 'swing');
		});
		
		$('a.top').click(function(e)	{
			$('ul.secondary').stop().animate({scrollTop: 0}, 1000, 'swing');
		});
		
		var el = $('ul.secondary > li.selected');
		if (el.length > 0)
		{
			var bottom = $('ul.secondary > li').length * $('ul.secondary > li:eq(0)').outerHeight();
			
			if (el.position().top >= $('ul.secondary').height())
			{
				$('ul.secondary').scrollTop(bottom);
			}
		}
		
		$('.primary li:not(.sold-out) a, .gallery a').hover(
      function () {
        $(this).children('img.hover').fadeIn("quick");
      }, 
      function () {
        $(this).children('img.hover').fadeOut("quick");
      }
    );
		
		$('div.banner').flash({
				src: '/Static/flash/banner.swf',
				width: 940,
				height: 60
		});
		
	var domain = top.location.href.match(/(\/\/.*?)\/+/i)[1];
	$('a.external').live('click', (function(e){e.preventDefault();window.open($(this).attr('href'));}));
	$("a[href^='http']:not(a[href^='http:" + domain + "'])").addClass('external');

	$('.galerija').each(function ()
	{
		$('a', $(this)).lightBox({
			fixedNavigation: true,
			overlayBgColor: '#000',
			overlayOpacity: 0.8,
			imageBlank: '/Static/images/lightbox/lightbox-blank.gif',
			imageLoading: '/Static/images/lightbox/lightbox-loading.gif',
			imageBtnClose: '/Static/images/lightbox/lightbox-closelabel.gif',
			imageBtnPrev: '/Static/images/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: '/Static/images/lightbox/lightbox-btn-next.gif',
			txtImage: 'Slika',
			txtOf: 'od',
			containerResizeSpeed: 200

	});
	});

});
