/**
 * Rounded Corners
 * 
 * html block elements with classes defined in rounded_corners.css
 * are modified by this javascript file.  In all cases, the rounded
 * corner divs should be placed inside of a container (as all the different
 * parts of the rounded corner div are floated left).  
 * 
 * There is a special case when an element is assigned display: none when the page
 * loads.  For these special elements, an exact width must be given.
 * 
 * @dependencies: rounded_corners.css
 * 
 * @optional header html can be assigned to a div with class="rounded_corners_fold_header"
 * which will be automatically moved to the top left corner div, currently supports:
 * - rounded_corners_gray
 * - rounded_corners_gray2
 * - rounded_corners_fold
 * - rounded_corners_white
 * 
 * ex using rounded_corners_fold:
 * 
 * 	<div class="rounded_corners_fold">
 * 		<div class="rounded_corners_fold_header">My Header HTML</div>
 * 		<p>My normal content</p>
 * 	</div>
 * 
 * results:
 * 
 * 	<div class="rounded_corners_fold_top_left">
 * 		<div class="rounded_corners_fold_header>My Header HTML</div>
 * 	</div>
 * 	<div class="rounded_corners_fold_top_right"></div>
 * 	<div class="rounded_corners_fold"> ... etc
 */
$(document).ready(function(){

	// rounded corners: white
	$('.rounded_corners_white').livequery(function(){
		// store if rounded corners have been applied to this element
		// this is needed for elements such as the multi template help menu in import domains
		// the element is initially created then moved via jquery to another element, this action
		// would have caused the rounded corners to be added twice
		if($(this).data('rounded') != true){
			if($(this).width() > 0){
				$(this).width($(this).width() - 2);
				var width = $(this).width() - 23;
				
				width = width.toFixed(0);
				$(this)
					.before('<div class="rounded_corners_white_top_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left")+ '"></div><div class="rounded_corners_white_top_right"></div>')
					.after('<div class="rounded_corners_white_bottom_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + ';"></div><div class="rounded_corners_white_bottom_right"></div>');
			// this case is for elements that start with display: none
			// in ie they do not have a width set, so we have to use the css value
			// this means elements that start with display: none MUST have a css width specified
			} else {
				//$(this).width($(this).css('width').replace('px', '') - 2);
				var width = $(this).css('width').replace('px', '') - 23;
				width = width.toFixed(0);
				$(this)
					.before('<div class="rounded_corners_white_top_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left")+ '"></div><div class="rounded_corners_white_top_right"></div>')
					.after('<div class="rounded_corners_white_bottom_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left") + ';"></div><div class="rounded_corners_white_bottom_right"></div>');				
			}
			$(this).data('rounded', true);
		}
	});

	// rounded corners: white
	$('.rounded_corners_gray2').livequery(function(){
		$(this).width($(this).width() - 2);
		var width = $(this).width() - 23;
		$(this).css('background-color', '#f3f3f3');
		$(this)
			.before('<div class="rounded_corners_gray2_top_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left")+ '"></div><div class="rounded_corners_gray2_top_right"></div>')
			.after('<div class="rounded_corners_gray2_bottom_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + ';"></div><div class="rounded_corners_gray2_bottom_right"></div>');
	});

	$('.rounded_corners_gray2_bottom').livequery(function(){
		$(this).width($(this).width() - 2);
		var width = $(this).width() - 23;
		$(this).css('background-color', '#f3f3f3');
		$(this).after('<div class="rounded_corners_gray2_bottom_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + ';"></div><div class="rounded_corners_gray2_bottom_right"></div>');
	});
	
	// rounded corners: square (same as white but with the upper right corner as a square)
	$('.rounded_corners_square').livequery(function(){
		// store whether rounded corners have been applied to this elemenet yet
		// we want to avoid doing this twice
		if($(this).data('rounded') != true){
			$(this).width($(this).width() - 2);
			var width = $(this).width() - 23;
			$(this)
				.before('<div class="rounded_corners_gray2_top_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left")+ '"></div><div class="rounded_corners_square_top_right"></div>')
				.after('<div class="rounded_corners_gray_bottom_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + ';"></div><div class="rounded_corners_gray_bottom_right"></div>');
			$(this).data('rounded', true);
		}
	});

	// rounded corners: gray
	$('.rounded_corners_gray').livequery(function(){
		if($(this).data('rounded') != true){
			if($(this).width() > 0){
				$(this).width($(this).width() - 2);
				var width = $(this).width() - 23;
				$(this)
					.before('<div class="rounded_corners_gray_top_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left")+ '"></div><div class="rounded_corners_gray_top_right"></div>')
					.after('<div class="rounded_corners_gray_bottom_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left") + ';"></div><div class="rounded_corners_gray_bottom_right"></div>');
			} else {
				var width = $(this).css('width').replace('px', '') - 23;
				$(this)
					.before('<div class="rounded_corners_gray_top_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left")+ '"></div><div class="rounded_corners_gray_top_right"></div>')
					.after('<div class="rounded_corners_gray_bottom_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left") + ';"></div><div class="rounded_corners_gray_bottom_right"></div>');			
			}
			$(this).prev().prev().html($(this).find('.rounded_corners_gray_header'));
			$(this).data('rounded', true);
		}
	});
	
	// rounded corners: fold
	$('.rounded_corners_fold').livequery(function(){
		if($(this).data('rounded') != true){		
			$(this).width($(this).width() - 2);
			var width = $(this).width() - 23;
			$(this)
				.before('<div class="rounded_corners_fold_top_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left")+ '"></div><div class="rounded_corners_fold_top_right"></div>')
				.after('<div class="rounded_corners_fold_bottom_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + ';"></div><div class="rounded_corners_fold_bottom_right"></div>');
			$(this).prev().prev().html($(this).find('.rounded_corners_fold_header'));
			$(this).data('rounded', true);
		}
	});
	
	// cont tab (little orange ribbon)
	$('.cont_tab').livequery(function(){
		if($(this).data('rounded') != true){
			$(this).prepend('<div class="cont_tab_right"></div>');
			$(this).data('rounded', true);
		}
	});
	
	// orange button
	$('.orange_button').livequery(function(){
		if($(this).data('rounded') != true){
			$(this).after('<div class="orange_button_right"></div>');
			$(this).data('rounded', true);
		}
	});
	
	// contextual help: any class with class help and a span inside
	$('.help_text').livequery(function(){
		if($(this).data('rounded') != true){
			var text = $(this).html();

			// help popup width is 300 (subtract 25 for the right corner div)
			var width = 275;
			
			$(this)
				.html('<div class="container_bloc clear" style="border-left:1px solid #C7C7C7;border-right:1px solid #C7C7C7;">' + text + '</div>')
				.prepend('<div class="rounded_corners_gray2_top_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left")+ '"></div><div class="rounded_corners_gray2_top_right"></div>')
				.append('<div class="rounded_corners_gray2_bottom_left" style="width: ' + width + 'px; padding-left: ' + $(this).css("padding-left")+ '; padding-right: ' + $(this).css("padding-left") + '; margin-left: ' + $(this).css("margin-left") + ';"></div><div class="rounded_corners_gray2_bottom_right"></div>');
			$(this).data('rounded', true);
		}
	});
	

	/**
	 * jQuery modal dialog popup
	 * 
	 * You can add the optional wrap: true parameter which will 
	 * wrap the content inside of a second rounded corner div.
	 * 
	 * NOTE: elements that begin with display: none require a width to be set 
	 */
	$('.ui-dialog').livequery(function(){
		var dialog = $(this).find('.ui-dialog-content');
		
		// flag to indicate whether to add another rounded corner wrapper 
		// around the content (Optional) default is false
		var wrap = dialog.dialog("option", "wrap");
		
		if(typeof(wrap) != "boolean"){
			wrap = false;
		}
		
		$(this).wrapInner('<div class="rounded_corners_gray2 container_bloc" />');
			
		// the inner div does not set the data key 'rounded' to true like the other elements
		// becasue msgAlert will call dialog() and have a cached value of true
		// when in fact the rounded corners have been replaced by a call to html()
		if(wrap && dialog.children('.rounded_corners_white').length == 0){
			dialog.wrapInner('<div class="rounded_corners_white container_bloc" />');
		}
		
		// Add some margin between dialog buttons
		$(this).find('button').addClass("orange_button")
		.wrap('<span style="float: left; margin-left: 10px;"></span>');
		
		$(this).bind("dialogclose", function(event, ui){
			$(this).remove();
		});
	});

	// orange small button
	$('.orange_small_button').livequery(function(){
		//if($(this).parent().has(".orange_small_button_right"))return;
		if($(this).data('rounded') != true){
			$(this).after('<div class="orange_small_button_right"></div>');
			$(this).data('rounded', true);
		}
	});
	
	// ui tabs Active
	$('.ui-tabs .ui-tabs-nav .ui-tabs-selected a').livequery(function(){
		
		if($(this).siblings('div').length > 0){
			$(this).siblings('div').attr('class', 'selected-right');
		} else {
			$(this).after('<div class="selected-right"></div>');
			
			// this element is adding automatically by the jquery tabs plugin
			// so we can't add a class to the <a> element it generates, therefor the default
			// behavior is to add the "remove" icon to all tabs unless otherwise specified
			// by a class <a class="add_new_tab"> or <a class="default_tab">
			if(!$(this).hasClass("add_new_tab") && !$(this).hasClass("default_tab")){
				$(this).siblings('.selected-right').prepend('<div class="remove_tab_link"  onClick=""></div>');
			}
		}
	});
	
	// ui tabs inactive
	$('.ui-tabs .ui-tabs-nav li:not(.ui-tabs-selected) a').livequery(function(){
		
		// :not(.ui-tabs-paging-prev):not(.ui-tabs-paging-next)
		
		if($(this).siblings('div').length > 0){
			$(this).siblings('div').attr('class', 'default-right');
		} else {
			
			$(this).after('<div class="default-right"></div>');
			
			if($(this).hasClass("default_tab")){
				// do nothing
				
			} else if($(this).hasClass("add_new_tab")){
				
				// add plus icon
				$(this).siblings('.default-right').prepend('<div class="add_new_tab_link"  onClick="addNewPage();return false;"></div>');

			} else if(!$(this).parent().hasClass('ui-tabs-paging-prev') && !$(this).parent().hasClass('ui-tabs-paging-next') ){
				
				// default behavior add remove icon
				$(this).siblings('.default-right').prepend('<div class="remove_tab_link"  onClick=""></div>');
			} 
		}
	});
	
});
