// Admin tabs plugin
(function() {
  jQuery.fn.prevWrap = function(){
	var prev = jQuery(this).prev();
	if(prev.length) return prev;
	return jQuery(this).siblings().filter(':last-child');
  };
  jQuery.fn.nextWrap = function(){
	var next = jQuery(this).next();
	if(next.length) return next;
	return jQuery(this).siblings().filter(':first-child');
  };
})();


(function() {
  jQuery.fn.i3Tabs = function(){
	  var container = $(this);
	  var tabHeaders = container.children('.mainTabs').children('li');
	  var tabs = container.find('.MSTabs_container');
	  tabs.hide();
	  tabHeaders.children('a').click(function(e){
		e.preventDefault();
		tabHeaders.removeClass('active');
		jQuery(this).parent().addClass('active');
		tabs.filter('.active').hide().removeClass('active');
		jQuery(this.hash).addClass('active').show();
	  });
	  var current = tabHeaders.filter('.active');
	  if(current.length)
	  {
		jQuery(current).children('a').click();
	  } else {
		tabHeaders.filter(':first-child').children('a').click();
	  }
	  // Add scrolling
	  container.children('.mainTabs').wheel(function(e,d){
		if(d>0)
		{
		  tabHeaders.filter('.active').prevWrap().children('a').click();
		}
		else
		{
		  tabHeaders.filter('.active').nextWrap().children('a').click();
		}
		return false;
	  });
  };
	
	
		
	$('a','div.banner').attr('target','_blank'); 
	$('div.banner').click(function(){
		$.post('/pages_actions/cms_banner_click.php',{id: $(this).attr('rel')});  
	});
		
})();             


$(document).ready(function() {
   
jQuery.fn.countyAutocomplete = function() {
  this.autocomplete('/pages_actions/system_counties.php', {
    width: 330,
    matchContains: true,
    formatResult: function(item) {
      return item[1];
    }
  });
  /*
  this.each(function(){
    var input = $(this);
    $('<a href="#" class="delete_input">delete</a>').click(function(e){
      e.preventDefault();
      input.fadeOut('fast',function(){
        $(this).remove();
      });
      $(this).remove();
    }).insertAfter(input);
  });
  */   
}

	$('.date-pick').datepicker({dateFormat:'dd/mm/yy',  yearRange: "-90:+15"});
	
	$(".media").media({autoplay: true});
	$('.show_media').hide();
	$('.enable_preview').click(function(){    
		$('.show_media').hide(); 
		  $('#media-'+$(this).attr('rel')).toggle();
	});
	
	$('.slider').slider(
	{
		min:1,
		max:10,
		steps:10,
		change: function()
		{
			rel = $(this).attr('rel');
			$('#attribute'+rel).val($('#slider'+rel).slider('value'));
		}
	});                                      
														 
	$('#a_toggle').click(function(){$('#CMS-ANNOUNCEMENTS').slideToggle();});                                                     
	$('#a_infoToggle').click(function(){$('#imageInfo').slideToggle();});
	
	$.fn.media.defaults = { 
	preferMeta:    1,         // true if markup metadata takes precedence over options object 
	autoplay:      1,         // normalized cross-player setting 
	bgColor:       '#ffffff', // background color 
	params:        {},        // added to object element as param elements; added to embed element as attrs 
	attrs:         {},        // added to object and embed elements as attrs 
	flashvars:     {},        // added to flash content as flashvars param/attr 
	flashVersion:  '8',       // required flash version 
 
	// default flash video and mp3 player 
	// @see: http://jeroenwijering.com/?item=Flash_Media_Player 
	flvPlayer:     '/_ui/flash/mediaplayer.swf', 
	mp3Player:     '/_ui/flash/mediaplayer.swf', 
	 
	// Silverlight options 
	// @see http://msdn2.microsoft.com/en-us/library/bb412401.aspx 
	silverlight: { 
		inplaceInstallPrompt: 'true', // display in-place install prompt? 
		isWindowless:         'true', // windowless mode (false for wrapping markup) 
		framerate:            '24',   // maximum framerate 
		version:              '0.9',  // Silverlight version 
		onError:              null,   // onError callback 
		onLoad:               null,   // onLoad callback 
		initParams:           null,   // object init params 
		userContext:          null    // callback arg passed to the load callback 
	} 
	}; 
	
	$("input:text, textarea, input:password").each(function(){
		if(this.value == '')
			this.value = this.title;
	});
	$("input:text, textarea, input:password").click(function(){
		if(this.value == this.title)
			this.value = '';
	});
	$("input:text, textarea, input:password").blur(function(){
		if(this.value == '')
			this.value = this.title;
	});
	$("input:image, input:button, button:submit").click(function(){
		$(this.form.elements).each(function(){
			if(this.type =='text' || this.type =='textarea' || this.type =='password'){
				if(this.value == this.title && this.title != ''){
					this.value='';
				}
			}
		});
	});                            
	
	function serialize(s)
	{
		serial = $.SortSerialize(s);
		alert(serial.hash);
	};    
	
	$('.toggleLink').click(function(e){
	  e.preventDefault();
	  $(this.hash).slideToggle();
	});
	
	$('.select_all').click(function(e){
		e.preventDefault();
		$('input:checkbox').attr('checked',true); 
	}); 
	$('.select_invert').click(function(e){
		e.preventDefault();         
		$('input:checkbox').each(function(){       
			if($(this).attr('checked'))
				$(this).attr('checked',false);
			else
			{
				if(!$(this).attr('disabled'))
				$(this).attr('checked',true); 
			}
		})
	}); 
	
	window.getCart = function(callback)
	{
		$.get('/pages_actions/ecom_cart.php', {mode: 'view'}, function (cart) {
			callback(cart);
		}, 'json');
	}
	window.repopulateCart = function(callback)
	{                
		getCart(function (cart) {
            var contents = cart.products_count + ' items / &pound;' + cart.overall_price;
            
            /* it's necessary to put contents into innerHTML of an tag to display pound sign
             - putting it directly into document.title will result in &pound; being displayed */
            
            var container = document.createElement('div');
            container.innerHTML = contents;
            
			$('#siteCart').html('( ' + container.innerHTML + ' )');
            
            if (String(window.location).indexOf('/cart/')!=-1)
                document.title = 'Your basket contents - ' +  container.innerHTML;
                
            if (callback)
                callback(cart);
		});
	}
 });
 
  function updateHiddenField(field,theValue){
	$('#'+field).val(theValue).change();
  }
 
	function popUp(URL) 
	{
		day = new Date();
		id = day.getTime();
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=700,left = 440,top = 250');");
	}                  
	function GetValueFromChild(myVal)
	{
		document.getElementById('cms_templates_id').value = myVal;
	}

	