(function($){
	$(function(){

		// Page Gallery
		$('#productGallery div.gallery').jqSlideshow({
			items: 'dl'
		});
		// Dialog popup
		$('#dialogbox').dialog({
			bgiframe: true,
			modal: true,
			width: 580,
			minHeight: 200,
			autoOpen:false,
			position:['center', 80],
			title:false,
			resizable:false,
			draggable:false,
			open: function(e, ui){
				$('div.ui-widget-overlay').css({opacity:0.8});
				var  p = $(this).parent().position();
				$(this).parent().css({'left': p.left-120});
			}
		});
		$('#defaultDialog').dialog({
			bgiframe: true,
			modal: true,
			width: 580,
			minHeight: 200,
			autoOpen:true,
			position:['center', 80],
			title:false,
			resizable:false,
			draggable:false,
			open: function(e, ui){
				$('div.ui-widget-overlay').css({opacity:0.8});
				var  p = $(this).parent().position();
				$(this).parent().css({'left': p.left-120});
			}
		});
		// Popup links load into dialog box
		$('#homepageposts a, li.popup a, a.popup').click(function(){
			$('#dialogbox').html('').dialog('open');
			var url = $(this).attr('href');
			$.get(url, function(data){
				$('#dialogbox').html( $(data).fadeIn() );
				jqueryFormAjaxInit();
			});
			return false;
		});
		// close dialog when clicking off
		$('div.ui-widget-overlay').live('click', function(){
			$('#dialogbox').dialog('close');
		});

	});
	
function jqueryFormAjaxInit(){
	try {
		if (typeof _wpcf7 == 'undefined' || _wpcf7 === null)
			_wpcf7 = {};
	
		_wpcf7 = jQuery.extend({ cached: 0 }, _wpcf7);
	
		jQuery('div.wpcf7 > form').ajaxForm({
			beforeSubmit: function(formData, jqForm, options) {
				jqForm.wpcf7ClearResponseOutput();
				jqForm.find('img.ajax-loader').css({ visibility: 'visible' });
				return true;
			},
			beforeSerialize: function(jqForm, options) {
				jqForm.find('.wpcf7-use-title-as-watermark.watermark').each(function(i, n) {
					jQuery(n).val('');
				});
				return true;
			},
			data: { '_wpcf7_is_ajax_call': 1 },
			dataType: 'json',
			success: function(data) {
				var ro = jQuery(data.into).find('div.wpcf7-response-output');
				jQuery(data.into).wpcf7ClearResponseOutput();
	
				if (data.invalids) {
					jQuery.each(data.invalids, function(i, n) {
						jQuery(data.into).find(n.into).wpcf7NotValidTip(n.message);
					});
					ro.addClass('wpcf7-validation-errors');
				}
	
				if (data.captcha)
					jQuery(data.into).wpcf7RefillCaptcha(data.captcha);
	
				if (data.quiz)
					jQuery(data.into).wpcf7RefillQuiz(data.quiz);
	
				if (1 == data.spam)
					ro.addClass('wpcf7-spam-blocked');
	
				if (1 == data.mailSent) {
					jQuery(data.into).find('form').resetForm().clearForm();
					ro.addClass('wpcf7-mail-sent-ok');
	
					if (data.onSentOk)
						jQuery.each(data.onSentOk, function(i, n) { eval(n) });
				} else {
					ro.addClass('wpcf7-mail-sent-ng');
				}
	
				if (data.onSubmit)
					jQuery.each(data.onSubmit, function(i, n) { eval(n) });
	
				jQuery(data.into).find('.wpcf7-use-title-as-watermark.watermark').each(function(i, n) {
					jQuery(n).val(jQuery(n).attr('title'));
				});
	
				ro.append(data.message).slideDown('fast');
			}
		});
	
		jQuery('div.wpcf7 > form').each(function(i, n) {
			if (_wpcf7.cached)
				jQuery(n).wpcf7OnloadRefill();
	
			jQuery(n).wpcf7ToggleSubmit();
	
			jQuery(n).find('.wpcf7-acceptance').click(function() {
				jQuery(n).wpcf7ToggleSubmit();
			});
	
			jQuery(n).find('.wpcf7-exclusive-checkbox').each(function(i, n) {
				jQuery(n).find('input:checkbox').click(function() {
					jQuery(n).find('input:checkbox').not(this).removeAttr('checked');
				});
			});
	
			jQuery(n).find('.wpcf7-use-title-as-watermark').each(function(i, n) {
				var input = jQuery(n);
				input.val(input.attr('title'));
				input.addClass('watermark');
	
				input.focus(function() {
					if (jQuery(this).hasClass('watermark'))
						jQuery(this).val('').removeClass('watermark');
				});
	
				input.blur(function() {
					if ('' == jQuery(this).val())
						jQuery(this).val(jQuery(this).attr('title')).addClass('watermark');
				});
			});
		});
	
	} catch (e) {
	}
}
})(jQuery);
