$(document).ready(function() {
	$('.option, .optionSelected').click(function(){
		var check = $('input', $(this)).attr('checked')==true
		$(this).attr('class', (check ? 'optionSelected' : 'option'));
	});

	$('#cancelSearch').click(function(){
		$('#jobs_search_exec').val("cancel_search");
		$('#jobSearchForm').submit();
	});
	

	$('#button, #buttonSmall, #pages, #prev, #next').click(function(){
		if($(this).attr("id")=="button" || $(this).attr("id")=="buttonSmall")
			$('#jobs_search_exec').val("on");
		$('#jobSearchForm').submit();
	});
	
	
	$('.head-weight, .head-color_desc, .head-stone_shape, .head-stone_clarity, .head-price, .head-catalog_num, .head-certificate_type', $('.dim-res-head')).click(function(){
		$('#order_field').val($(this).attr('field'));
		$('#order_dir').val($(this).attr('order'));
		$('#diamonds-search').submit();	
	})

	$('.dim-res-image, .dim-res-weight, .dim-res-color_desc, .dim-res-stone_shape, .dim-res-stone_clarity, .dim-res-catalog_num, .dim-res-price, .dim-res-wire-price, .dim-res-certificate_type, .dim-res-view').click(function(){
		var di_id = $(this).parent().attr('id').split('diamond_');
		di_id = di_id[1];
		window.location = non_def_lang+'/view_diamonds/'+di_id+'.htm';
	});
	
	$('[class^=item]').hover(function(){
		if($(this).attr('class')!='open'){
			$(this).addClass('over');
		}
		$('.dim-res-details', $(this)).show();
	}, function(){
		if($(this).attr('class')!='open'){
			$(this).removeClass('over');
		}
		$('.dim-res-details', $(this)).hide();
	});
	
	$('#qsrc_price, #qsrc_size, #qsrc_intensity, #sort_by').change(function(){
		var action_prefix = "";
		action = $('#diamonds_checked').attr('action').split("/");
		if(action[action.length-1])
			action = action[action.length-1];
		else
			action = action[action.length-2];
			
		//if($('#sort_by').val() && $('#sort_by').val()!="date")
			//action_prefix = "/sort_"+$('#sort_by').val();
		if($('#qsrc_size').val() && $('#qsrc_size').val()!=-1)
			action_prefix += "/"+$('#qsrc_size').val()+"-carat";
		if($('#qsrc_price').val() && $('#qsrc_price').val()!=-1)
			action_prefix += "/"+$('#qsrc_price').val()+"-price";
		
		$('#diamonds_checked').attr('action', non_def_lang+action_prefix+"/"+action);

		$('#order_dir').val("ASC");
		if($('#sort_by').val()=='date'){
			$('#order_dir').val("DESC")
		}
		$('#quick_src_done').val('1');
		$('#diamonds_checked').submit();
	});
	
	$('.head-weight, .head-color_desc, .head-stone_shape, .head-stone_clarity, .head-price, .head-certificate_type', $('.dim-res-head')).click(function(){
		$('#sort_by').val($(this).attr('field'));
		$('#order_dir').val($(this).attr('order'));
		$('#diamonds_checked').submit();
	});

	// contact us about empty list
	$('#add_msg').click(function(){
		var err = new Array(), i=0 ;
		
		if (!$.trim($('#message').val()) || !$.trim($('#name').val()) || !$.trim($('#email').val())){
			err[i++] = view_labels["miss_fields"];
		}		
		
		var str = $.trim($('#email').val()); 
		if (str){ 
			if (!validEmail(str)) { 
				err[i++] = view_labels["err_email"];
			}
		}
		
		if (err.length){
			$('.dim-add-msg-error').remove();
			$('.dim-add-msg-form').prepend('<div class="dim-add-msg-error">'+view_labels["errors"]+':<ul><li>'+err.join('</li><li>')+'</li></ul></div>');
		}
		else{
			$('.dim-add-msg-error').remove();
			var data =$("input[type=checkbox][checked], input[type=text], input[type=hidden], select, textarea", $('.dim-add-msg-form')).serialize();
			
			$.post(non_def_lang+'/contact_empty_list/results_diamonds', data, function(msg){
				$('.dim-add-msg').css('display', "none");
				$("input[type=checkbox][checked], input[type=text], select, textarea").val("");
				if(msg!=0){
					$('.dim-add-msg-success').css('display', "block");
					setTimeout("$('.dim-add-msg-success').hide('slow');", 3000);
				}
			});
		}
	});

	$('.star, .star-dis').click(function(){
		var di_id = $(this).parent().parent().attr('id').split('diamond_');
		di_id = di_id[1];
		var act = ($(this).attr('class')=="star" ? "star-dis" : "star");
		if(act == "star-dis" && !confirm(remove_wish_labels))
			return;
		var this_span = $(this);
		$.post(non_def_lang+'/'+act+'/results_diamonds/'+di_id+'.htm', "", function(msg){
			if(wish_list)
				$(this_span).parent().parent().remove();
			else{
				$(this_span).attr('class', act);
				$(this_span).html('<img height="13" width="13" src="/_media_static/global/diamonds/'+act+'.gif" alt="" />');
			}
		})
	});
	
	/* tooltips */
	$(".wish-list-offer").tooltip({

		// use div.tooltip as our tooltip
		tip: '.wish-list-offer-tooltip',

		// use the fade effect instead of the default
		effect: 'fade',

		// make fadeOutSpeed similar to the browser's default
		fadeOutSpeed: 100,

		// the time before the tooltip is shown
		predelay: 100,

		// tweak the position
		position: "top left",
		offset: [10, 20]
	});
	
	$(".wish-list-offer-dis").tooltip({

		// use div.tooltip as our tooltip
		tip: '.wish-list-offer-tooltip-dis',

		// use the fade effect instead of the default
		effect: 'fade',

		// make fadeOutSpeed similar to the browser's default
		fadeOutSpeed: 100,

		// the time before the tooltip is shown
		predelay: 100,

		// tweak the position
		position: "top left",
		offset: [10, 20]
	});

	$('.wish-list-offer, .wish-list-offer-dis, .wish-list-offer-submit').click(function(){
		var offer_div = $(this);
		var di_id = $(this).parent().parent().attr('id').split('diamond_');
		var offer;
		di_id = di_id[1];
		switch(offer_div.attr('class')) {
			case "wish-list-offer-dis":
					offer = "remove";
				break;
			case "wish-list-offer":
					offer = "add";
				break;
			case "wish-list-offer-submit":
					if(!confirm(offer_labels['submit_sure']))
						return;
					offer = "submit";
				break;
		}
		
		$.post(non_def_lang+'/offer/results_diamonds/'+di_id+'.htm&offer='+offer, "", function(msg){
			switch(msg) {
				case "submit_msg_done":
				case "can_not_submit":
					$('.wish-list-offer-submit').hide();
				case "can_not_add":
				case "can_not_remove":
						$('.wish-list-offer-txt').html(offer_labels[msg]);
						$('#inline', $('.wish_list_msg')).trigger('click');
					break;
				case "submit_msg":
					break;
				case "add_three":
						$('.wish-list-offer-txt').html(offer_labels["submit_msg"]);
						$('#inline', $('.wish_list_msg')).trigger('click');
						msg = "add";
				case "add":
						offer_div.hide();
						$('.wish-list-offer-dis', offer_div.parent()).show();
					break;
				case "remove":
						offer_div.hide();
						$('.wish-list-offer', offer_div.parent()).show();
					break;
			}
		})
	});

	$('#inline', $('.wish_list_msg')).fancybox({
		'hideOnContentClick': true,
		'zoomSpeedIn': 300,
		'zoomSpeedOut':	300, 
		'overlayShow': true,
		'centerOnScroll': false,
		'frameWidth': 300,
		'frameHeight': 100,
		'overlayColor': '#333333',
		'overlayOpacity': 0.9
	});		
	
	$('.iframe', $('.wish-list-offer-read-more-div')).fancybox({
		'hideOnContentClick': true,
		'zoomSpeedIn': 300,
		'zoomSpeedOut':	300, 
		'overlayShow': true,
		'centerOnScroll': false,
		'frameWidth': 600,
		'frameHeight': 500,
		'overlayColor': '#333333',
		'overlayOpacity': 0.9
	});		
	
});