$(document).ready(function() {
	$('.ch_button').bind('click', function(el) {
		var next = $(this).next();
		if(next.hasClass('std_jip_act_wr_active')) {
			next.removeClass('std_jip_act_wr_active');
			$(this).removeClass('ch_button_active');
		} else {
			$('.std_jip_act_wr').removeClass('std_jip_act_wr_active');
			$('.ch_button').removeClass('ch_button_active');
			next.addClass('std_jip_act_wr_active');
			$(this).addClass('ch_button_active');
		}
		return false;
	});
	
	$(".accordion").accordion({
		autoHeight: false,
//		fillSpace: true,
		navigation: true ,
		navigationFilter: function() {
			var pathname = $(location).attr('href'); //window.location.pathname;
			if(this.href.toLowerCase() == pathname) {
				$(this).addClass('active');
			}
			return this.href.toLowerCase() == pathname;
		}
	});
	
	$(".tabs").tabs();
});

block_sett = {message: '<h1>Подождите окончания запроса...</h1>', css: {}, cssOverlay: {}//, 
//				css: {width: '350px', border: 'none', padding: '5px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', 'border-radius': '10px', opacity: .7, color: '#fff'}, 
//				cssOverlay: {backgroundColor: '#f0f0f0', zIndex: 2000}
			};

jQuery(document).ready(function() {
	
//alert('sss');
	
$('body').delegate('.ajax_form_reload', 'submit', function(event) {
	$(this).ajaxSubmit({
		success: function() {window.location.reload(true);},
		beforeSubmit: blockAll,
		clearForm: false,
		resetForm: false
	});
	return false;
});
	

$('body').delegate('.ajax_form', 'submit', function(event) {
//	alert('sss2');
	$(this).ajaxSubmit({
//		url: '/admin_property/create',
//		type: 'post',
		success: showFBResponse,
		beforeSubmit: blockAll,
		clearForm: false,
		resetForm: false
	});
//	alert('sss3');
	return false;
});

$('body').delegate('.removed_wp_by_click', 'click', function(event) {
	$(this).parent('.removed_cont').remove();
	event.preventDefault();
});

$('body').delegate('.ui-button + .removed_wp_by_click', 'hover', function() {
	$(this).toggleClass('ui-state-hover');
});

/*
$('body').delegate('#contact_us', 'submit', function(event) {
//	alert('sss2');
	$(this).ajaxSubmit({
//		url: '/admin_property/create',
//		type: 'post',
		success: showMsgResponse,
		beforeSubmit: blockFancy,
		clearForm: false,
		resetForm: false
	});
//	alert('sss3');
	return false;
});
*/

/******************************** only for img ***********************/
/*
$('body').delegate('.ajax_form_img', 'submit', function(event) {
	$(this).ajaxSubmit({
		iframe: true,
		target: '.update_area',
//		url: '/admin_property_img/add_photo/{$#request.id}',
		type: 'post',
		clearForm: false,
		resetForm: true
	});
	event.preventDefault();
});


$('body').delegate('.ajax_form_img #filephoto', 'change', function(e) {
//	alert('ddd');
	//alert($(this).val());
	$('#add_photo').submit();
});
*/
/*********************** end only for img *****************************/









/*	$('.ajax_form').submit(function() {
		alert('sss2');
		$(this).ajaxSubmit({
//			url: '/admin_property/create',
//			type: 'post',
			success: showFBResponse,
			beforeSubmit: blockFancy,
			clearForm: false,
			resetForm: false
		});
		alert('sss3');
		return false;
	});*/
//	alert('sss4');

	/*
	$('body').delegate('.mod_win_opnr', 'click', function(e) {
		var $link = $(this);
		var $dialog = $('<div class="mod_win"></div>')
		.load($link.attr('href'))
		.dialog({
//			autoOpen: false,
			title: '<b>' + $link.attr('title') + '</b>',
			width: 600,
			height: 600,
			modal: true,
//			draggable: true,
 			buttons:{
 				"Отправить" : function() {
 					$('.mod_win').block(block_sett);
   	 				$('#contact_us').submit();
 				}, 
 				"Закрыть" : function() {
			       	$(this).dialog("destroy");
			       	$(this).remove();
			    }
 			},
			close: function(event, ui) {
				$(this).dialog("destroy");
				$(this).remove();
//				$('.dlg_box').remove();
			}
		}).block(block_sett);
//	$link.click(function() {
//		$dialog.dialog('open').block();

		return false;
//	});

	
	
//	$.fancybox({'href' : this.href, 'autoDimensions' : false, 'showCloseButton' : false, 'modal' : true, 'height' : 550, 'width' : 900, 'onStart' : blockAll, 'onComplete' : unblockAll});
//	$.fancybox.hideActivity();
	event.preventDefault();
	});*/
//	$(".admin_lc_nav").resizable({handles: "e"});
	




$('.tip_rel_cont').qtip({
   content: {
      text: function() {
    	  return $('.' + $(this).attr('tip_rel')).html();
      }, 
      title: {
    	  text: function(api) {
              return $(this).attr('title');
           },
         button: false
      }
   },
   position: {
	   at: 'bottom center', // Position the tooltip above the link
       my: 'top center',
       viewport: $(window) // Keep the tooltip on-screen at all times
   },
   show: {
	   event: 'mouseenter',
       solo: true // Only show one tooltip at a time
   },
   hide: 'mouseleave',
   style: {
      classes: 'ui-tooltip-shadow ui-tooltip-rounded'
   }
});

$(".jqui_button").button();

	$('.mod_win').bind( "dialogbeforeclose", function(event, ui) {
		$(this).dialog("destroy");
		$(this).remove();
	});
	
$('body').delegate('.admin_rel_tip', 'focus', function(event) {
	$(this).qtip({
			overwrite: false,
		   content: {
			      text: function() {
			    	  return $('.' + $(this).attr('tip_rel')).html();
			      },
			      title: {
/*				    	  text: $('.' + $(this).attr('tip_titile_rel')).html()
		/*	    	  text: alert(this) function(api) {
			              return $(this).attr('title');
			           }*/
			      }
			   },
			   position: {
				   at: 'right center',
			       my: 'left center',
			       viewport: $(window)
			   },
			   show: {
				   event: event.type,
				   ready: true,
			       solo: true
			   },
			   hide: 'unfocus',
			   style: {
			      classes: 'ui-tooltip-shadow ui-tooltip-rounded'
			   }
			}, event);
});

$('body').delegate('.admin_rel_mo_tip', 'mouseover', function(event) {
	$(this).qtip({
			overwrite: false,
		   content: {
			      text: function() {
			    	  return $('.' + $(this).attr('tip_rel')).html();
			      },
			      title: {
/*				    	  text: $('.' + $(this).attr('tip_titile_rel')).html()
		/*	    	  text: alert(this) function(api) {
			              return $(this).attr('title');
			           }*/
			      }
			   },
			   position: {
				   at: 'right center',
			       my: 'left center',
			       viewport: $(window)
			   },
			   show: {
				   event: event.type,
				   ready: true,
			       solo: true
			   },
			   hide: 'mouseleave',
			   style: {
			      classes: 'ui-tooltip-shadow ui-tooltip-rounded'
			   }
			}, event);
});
	
	$('.main_area_ajax').delegate('.ajax_action', 'click', function(event) {
		blockAll();
		var $link = $(this);
		var $dialog = $('<div class="dlg_box"></div>')
			.load($link.attr('href'), function() {unblockAll();})
			.dialog({
//				autoOpen: false,
				title: '<b>' + $link.attr('title') + '</b>',
				width: 900,
				height: 550,
				modal: true,
//				draggable: true,
				close: function(event, ui) {
					$(this).dialog("destroy");
					$(this).remove();
				}
			});
			
			return false;
		event.preventDefault();
	});
	
	
	
	$('.main_area_ajax').delegate('.action_reload', 'click', function(event) {
		blockAll();
		$.ajax({
			type		: "get",
			cache		: false,
			url			: this.href,
			complete	: function() {
/*				if(typeof _rtp != 'undefined') {
					window.location.assign(_rtp);
				} else window.location.reload();
*/				window.location.reload();
			}
		});
		event.preventDefault();
	});
	
	$('.main_area_ajax').delegate('.action_reload_to', 'click', function(event) {
		blockAll();
		$.ajax({
			type		: "get",
			cache		: false,
			url			: this.href,
			complete	: function() {
				if(typeof _rtp != 'undefined') {
					window.location.assign(_rtp);
				} else window.location.reload();
			}
		});
		event.preventDefault();
	});
	
	$("body").delegate('.button_reload_page', 'click', function(e) {
		window.location.reload();
	});
	
/*	$("body").delegate('.button_cancel', 'click', function(e) {
		$('.dlg_box').dialog('close');
//		$.fancybox.close();
	});
*/		
	$(".fancy_image").fancybox();
	
	$('.fan_img').fancybox({'showCloseButton'   : false,
								'titlePosition' 	: 'inside',
								'titleFormat'		: formatTitle});


	
//	$(function() {

	    $('.tip_trigger').each(function() {        
	        var tip = $(this).find('.tip');

	        $(this).hover(
	            function() { tip.appendTo('body'); },
	            function() { tip.appendTo(this); }
	        ).mousemove(function(e) {
	            var x = e.pageX + 20,
	                y = e.pageY + 20,
	                w = tip.width(),
	                h = tip.height(),
	                dx = $(window).width() - (x + w),
	                dy = $(window).height() - (y + h);

	            if ( dx < 20 ) x = e.pageX - w - 20;
	            if ( dy < 20 ) y = e.pageY - h - 20;

	            tip.css({ left: x, top: y });
	        });         
	    });

//	});
	
});

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div style="text-align:left;"><span><a href="javascript:;" onclick="$.fancybox.close();" style="color: #333333; float: right; font-size: 12px; font-weight: bold;text-decoration: none;">закрыть</a></span>' + (title && title.length ? '<b>' + title + '</b>' : '' + title ) + '<br />Изображение ' + (currentIndex + 1) + ' из ' + currentArray.length + '</div>';
}

function showFBResponse(responseText, statusText, xhr, form) {
//	unblockFancy();
	$('.dlg_box').html(responseText);
//	$('.dlg_box').unblock();
	unblockAll();
//	$.fancybox(responseText, {'autoDimensions'	: false, 'showCloseButton' : false, 'height' : 550, 'width' : 900});
}

function showMsgResponse(responseText, statusText, xhr, form) {
	$('.mod_win').html(responseText);
	$('.mod_win').unblock();
}

function blockAll() {
	$.blockUI(block_sett);
}

function unblockAll() {
	$.unblockUI();
}

/*function blockFancy() {
	$('.dlg_box').block(block_sett);
}

function unblockFancy() {
	$('.dlg_box').unblock();
}*/
