// JavaScript Document 
$(function(){
	$("ul.sf-menu").supersubs({ 
		minWidth:    12,   // minimum width of sub-menus in em units 
		maxWidth:    27,   // maximum width of sub-menus in em units 
		extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
						   // due to slight rounding differences and font-family 
	}).superfish();  // call supersubs first, then superfish, so that subs are 
					 // not display:none when measuring. Call before initialising 
					 // containing tabs for same reason. 
	$('#fader').innerfade({ speed: 2000, timeout: 5000, type: 'sequence', containerheight: '270px' });
	$('#shop-fader').innerfade({ speed: 2000, timeout: 5000, type: 'sequence', containerheight: '424px' });
	$('#smallfader').innerfade({ speed: 2000, timeout: 5000, type: 'sequence', containerheight: '200px' });
	$('#tweetFade').innerfade({ speed: 1000, timeout: 9000, type: 'sequence', containerheight: '120px' });
	$('a[rel*=facebox]').facebox({ closeImage : '/themeTessuti/images/closelabel.gif' });
	

	
	// attach behaviours for slide down products
	$('#maxi').click(function(){
		$('#photoWrap').slideDown();
		$('#maxi').fadeOut();
		$.cookie('is_closed', 'false');
	});
	$('#mini').click(function(){
		$('#photoWrap').slideUp();
		$('#maxi').fadeIn();
		$.cookie('is_closed', 'true');
	});

	if($.cookie('is_closed') != 'true')
	{
		setTimeout(function(){
			$('#photoWrap').slideDown();
			$('#maxi').fadeOut();
		}, 2000);	
	}
	
	// form validate - http://docs.jquery.com/Plugins/Validation
	$("#frmContact").validate();
	
	// contact form slider
	$('#enquiryType').change(function(){
		if ($(this).val() == 'General Enquiry')
		{ 
			$('#postalDetails').slideUp(300);
		}
		else 
		{ 
			$('#postalDetails').slideDown(1700);
		}
	});
	// contact form - Country Slider
	$('#country').change(function(){
		if ($(this).val() == 'Australia')
		{ 
			$('#countryDetails').slideUp(250);
			$('#country').fadeTo(250, 1);
			$('#countryLabel').fadeTo(250, 1);
			$('#postCodeLink').fadeTo(250, 1);
		}
		else 
		{ 
			$('#countryDetails').slideDown(250); 
			$('#country').fadeTo(250, 0.15);
			$('#countryLabel').fadeTo(250, 0.15);
			$('#postCodeLink').fadeTo(250, 0.15);
			
			
			
		;
		}
	});
	
	// add the add, edit and delete widgets to list records
	$('a.dm_widget_list_add, a.dm_widget_list_edit, a.dm_widget_list_delete').click(function(){
		
		var widget_id = $(this).parents('.dm_widget').attr('id').replace('dm_widget_', '');
		var page_id = dm_configuration.page_id;
		var record_id = $(this).attr('id').replace('dm_widget_record_id_', '');
		var is_delete = $(this).is('a.dm_widget_list_delete') ? true : false;
		
		if(is_delete && !confirm('Are you sure?'))
		{
			return false;
		}
		
		if($(this).is('a.dm_widget_list_add'))
		{
			url_action = 'addListRecord'; 
		}
		else if($(this).is('a.dm_widget_list_edit'))
		{
			url_action = 'editListRecord'; 
		}
		else if($(this).is('a.dm_widget_list_delete'))
		{
			url_action = 'deleteListRecord';
		}
		
	    $.ajax({
	        url:      $.dm.ctrl.getHref('+/dmWidget/' + url_action),
	        data:     {widget_id: widget_id, dm_embed: 1, "page_id": page_id, "record_id": record_id},
	        success:  function(html)
	        {
	        	
	          if(is_delete)
	          {
	            document.location.reload();
	            return;
	          }
	          
	          $('<div class="diem-colorbox none"></div>')
	          .html(html)
	          .dmExtractEncodedAssets()
	          .find('a')
	          .colorbox({
	            width:"90%",
	            height:"90%",
	            iframe: true,
	            speed: 200,
	            opacity: 0.5,
	            onClosed: function()
	            {
	              document.location.reload();
	            }
	          })
	          .trigger('click');

	          var $close = $('#cboxClose').attr('rel', ''), interval = setInterval(function()
	          {
	            if($close.attr('rel') == 'dm_close')
	            {
	              clearInterval(interval);
	              $close.trigger('click');
	            }
	          }, 200);
	        }
	      });		
	});
	
	// track successful subscribers as a page view in analytics
	if($('#emailSubscriptionSuccessful').is('*'))
	{
		_gaq.push(['_trackPageview', '/_actions/subscribe']);
	}
	

}); 
 
