var last_ajax_url = '';

$(document).ready(function(){
	$.ajaxSetup({
		cache: true,
		error: function() {
			alert('Your browser does not support modern JavaScript. It is recommended that you upgrade to the latest version or you may experience errors while browsing.');
		},
		timeout: 25000
	});
	
	// make external pages load in new window
	$('a[rel=external_link]').livequery('click', function() {
		window.open($(this).attr('href'));
		return false;
	});

	$('a.zoom_image, a.zoom_image_gallery').lightBox(
		{
			imageLoading: '/design/lightbox/lightbox-ico-loading-'+window.f_lang.lang_id+'.gif',
			imageBtnClose: '/design/lightbox/lightbox-btn-close-'+window.f_lang.lang_id+'.gif',
			imageBtnPrev: '/design/lightbox/lightbox-btn-prev-'+window.f_lang.lang_id+'.gif',
			imageBtnNext: '/design/lightbox/lightbox-btn-next-'+window.f_lang.lang_id+'.gif',
			imageBlank: '/design/lightbox/lightbox-blank.gif',
			txtImage: window.f_lang.image,
			txtOf: window.f_lang.out_of
		}
	);

	$('#top-menu li').hover(function() {
		$(this).children('ul:first').show();
	}, function() {
		$(this).children('ul:first').hide();
	});
	
	
	/**********************************
	 * TRIAL CREATION FORM
	 **********************************/
	// hide second tab content on startup
	$('.tab1-content').hide();
		
	// toggle form tabs
	function togleTabs(index) {
		$('.tab'+(index==1 ? 0 : 1)+'-content').slideUp(
			300, 
			function(){
				$('.tab'+index+'-content').delay(200).slideDown(800)
			}
		);
		
		$('.tab'+index).removeClass('inactive').addClass('active');
		$('.tab'+(index==1 ? 0 : 1)).removeClass('active').addClass('inactive');
	}
	
	// form validation for both steps
	function formValidation(index) {
		
	}
	
	// form tabs toggle action binding
	$('.ui-form-header').each(function(index) {
		// tab
		$(this).bind('click', function() {
			formValidation(index);
			togleTabs(index)
		});
		
		// buttons
		$('.tab'+(index==1 ? 0 : 1)+'-content').children(":button").filter(".next, .previous").click(function() {
			formValidation(index);
			togleTabs(index);
	    });
	});
	
	// images on step 2
	$('#trial_creation_form .item').each(function() {
		$(this).bind('click', function() {
			$(this).children('input:radio').attr('checked', 'checked');
		});
	});


	/**********************************
	 * HOMEPAGE CAROUSEL
	 **********************************/
	$('.carousel > .hidden').show();
	$('.carousel').infiniteCarousel({
		displayTime: 7000,
		textholderHeight: .25,
		displayProgressBar: 0,
		imagePath: '/img/',
		displayThumbnailBackground: false,
		autoHideControls: true
	});
	
	
	/**********************************
	 * HOMEPAGE REFERENCES QTIP
	 **********************************/
	$('.references > .item > .preview > a > img[alt], .references_list > .item > a > img[alt]').each(function()
	{
		$(this).qtip({
			content: $(this).attr('alt'), // Use the alt attribute of the img element for the content
			style: {
                  border: {
                     width: 5,
                     radius: 5
                  },
                  padding: 10, 
                  textAlign: 'center',
                  tip: true, // Give it a speech bubble tip with automatic corner detection
                  name: 'dark' // Style it according to the preset 'cream' style
               },
			position: {
                  corner: {
                     tooltip: 'bottomRight', // Use the corner...
                     target: 'topMiddle' // ...and opposite corner
                  }
               }
		});
	});
	
	
	/**********************************
	 * PRICELIST QTIP
	 **********************************/
	$('.modules_pricelist > table > tbody > tr > td > a[title]').each(function()
	{
		$(this).qtip({
			content: $(this).attr('title'), // Use the alt attribute of the img element for the content
			style: {
                  border: {
                     width: 5,
                     radius: 5
                  },
                  padding: 10, 
                  textAlign: 'center',
                  tip: true, // Give it a speech bubble tip with automatic corner detection
                  name: 'dark' // Style it according to the preset 'cream' style
               },
			position: {
                  corner: {
                     tooltip: 'bottomLeft', // Use the corner...
                     target: 'topMiddle' // ...and opposite corner
                  }
               }
		});
	});


	/**********************************
	 * HOMEPAGE PRODUCTS
	 **********************************/
	$('.product_cz').each(function(index) {
		// whole product div area is clickable
		$(this).bind('click', function() {
			window.location = $(this).children('.detail_button').children('a').attr('href');
		});
	});
	
	
	/**********************************
	 * BUTTONS
	 **********************************/
	$('.button_orange, .button_green, .button_blue, .submits').each(function(index) {
		// whole button div area is clickable
		$(this).bind('click', function() {
			window.location = $(this).children('a').attr('href');
		});
	});
	
	
	/**********************************
	 * CALLBACK OVERLAY
	 **********************************/
	// load step1
	$('#footer-callback a[rel]').overlay({
		onBeforeLoad: function() {
			// grab wrapper element inside content
			var wrap = this.getOverlay().find('.contentWrap');
			
			// load the page specified in the trigger
			$.ajax({
				url: this.getTrigger().parent().parent().attr('action'),
				success: function(data, textStatus, XMLHttpRequest) {
					data = $(data).find('#callback_form');
					data.find('#callback_overlay_tel').attr('value', $('.callback_tel').attr('value'));
					wrap.html(data);
				}
			});
		}
	});
	
	// load step 2
	$('#callback_form form').livequery('submit', function() {
		$("#callback_form a[rel]").overlay({
			load: true,
			onBeforeLoad: function() {
				// grab wrapper element inside content
				var wrap = this.getOverlay().find('.contentWrap');
				
				// postdata
				var ajaxPostData = new Array();
				ajaxPostData.push('tel=' + $('#callback_overlay_tel').attr('value'));
				ajaxPostData.push('otazka=' + $('#callback_overlay_otazka').attr('value'));
				ajaxPostData.push('cislo1=' + $('#callback_overlay_cislo1').attr('value'));
				ajaxPostData.push('cislo2=' + $('#callback_overlay_cislo2').attr('value'));
				ajaxPostData.push('question=' + $('#callback_overlay_question').attr('value'));
				ajaxPostData.push('callback_form=1');
				
				// load the page specified in the trigger
				$.ajax({
					url: this.getTrigger().parent().parent().attr('action'),
					type: 'POST',
					data: ajaxPostData.join('&'),
					success: function(data, textStatus, XMLHttpRequest) {
						data = $(data).find('#callback_form');
						wrap.html(data);
					}
				});
			}
		});
		return false;
	});

});
