jQuery(document).ready( function(){
	
	jQuery('#MainImageContainer').cycle( {
		delay: -1000,
		timeout: 6000,
		speed: 3000
	});
    
    jQuery('#SearchForm_SearchForm_Search').focus(function() {
    	jQuery(this).val('');
    });
    
	jQuery('#Content a, #SearchResults a, #MainHotLinks a, #SideBar a').each(function() {
		var fileURL = jQuery(this).attr('href');
		if (fileURL) {
			if (fileURL.match("\.doc$")) {
				jQuery(this).addClass('file');
				jQuery(this).css('background','transparent url(themes/winediscoverynz/images/icons/icon_doc.gif) no-repeat scroll 0 1px');
			}
			if (fileURL.match("\.pdf$")) {
				jQuery(this).addClass('file');
				jQuery(this).css('background','transparent url(themes/winediscoverynz/images/icons/icon_pdf.gif) no-repeat scroll 0 1px');
			}
			if (fileURL.match("\.xls$")) {
				jQuery(this).addClass('file');
				jQuery(this).css('background','transparent url(themes/winediscoverynz/images/icons/icon_xls.gif) no-repeat scroll 0 1px');
			}
			if (fileURL.match("\.ppt$")) {
				jQuery(this).addClass('file');
				jQuery(this).css('background','transparent url(themes/winediscoverynz/images/icons/icon_ppt.gif) no-repeat scroll 0 1px');
			}
			if (fileURL.match("\.pptx$")) {
				jQuery(this).addClass('file');
				jQuery(this).css('background','transparent url(themes/winediscoverynz/images/icons/icon_ppt.gif) no-repeat scroll 0 1px');
			}
		}
	});
	
	jQuery('.downloadCSV').click(function() {
		theID = jQuery(this).attr('id');
		newID = '#Download'+theID;
		alert(newID);
		jQuery(newID).table2CSV();
		return false;
	});
    
    jQuery('.package').click(function() {
    	// switch checkboxes
    	jQuery('.package .inline').removeClass('checkedPackage');
    	jQuery(this).children('.inline').addClass('checkedPackage');
    	jQuery('.package').removeClass('on');
    	jQuery(this).addClass('on');
    	
    	// get id values into array
    	triggers = jQuery(this).attr('href').split("|");
    	jQuery('.progSection').hide();
		jQuery.each(triggers, function(index, value) { 
			if (jQuery(value).is(':visible')) {
				
			} else {
				jQuery(value).fadeIn();
			} 
		});
    	
    	
    	return false;
    });
    
    if ( jQuery('#ProgrammeSidebar #SideBar').length ) {
	    var top = jQuery('#ProgrammeSidebar #SideBar').offset().top - parseFloat(jQuery('#ProgrammeSidebar #SideBar').css('marginTop').replace(/auto/, 0));
		jQuery(window).scroll(function (event) {
			// what the y position of the scroll is
			var y = jQuery(this).scrollTop();
			
			// whether that's below the form
			if (y >= top) {
				// if so, ad the fixed class
				jQuery('#ProgrammeSidebar #SideBar').addClass('fixed');
			} else {
				// otherwise remove it
				jQuery('#ProgrammeSidebar #SideBar').removeClass('fixed');
			}
		});
	}
	
	jQuery('#OpenAPEXPopup').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'width'			:	700, 
		'height'		:	500,
		'autoDimensions':	false, 
		'titleShow'		:	false
	});
	
	// smooth scroll headers
/*	if (jQuery('#ProgrammeMagic').length) {
		var $container = jQuery('#ProgrammeMagic');
		var $headers = $container.find('h4');
		var $fakeHeader = $headers.filter(':first').clone();
		var zIndex = 2;
		var containerTop = $container.offset().top + parseInt($container.css('marginTop')) + parseInt($container.css('borderTopWidth'));
		
		$container.wrap('<div class="box" />');
		$fakeHeader.css({ 
		  zIndex: 1, 
		  position: 'absolute', 
		  width: $headers.filter(':first').width() 
		});
		$container.before($fakeHeader.text($headers.filter(':first').text()));
		
		$headers.each(function () {
			var $header = jQuery(this);
			var height = $header.outerHeight();
			var width = $header.outerWidth();
			
			$header.css({
				position: 'absolute',
				width: $header.width(),
				zIndex: zIndex++
			});
			
			// create the white space
			var $spacer = $header.after('<div />').next();
			$spacer.css({
				height: height,
				width: width
			});
		});
		
		$container.scroll(function () {
		  $headers.each(function () {
		    var $header = $(this);
		    var top = $header.offset().top;
		    
		    if (top < containerTop) {
		      $fakeHeader.text($header.text());
		      $fakeHeader.css('zIndex', parseInt($header.css('zIndex'))+1);
		    }
		  });
		});
	}
*/	    
});
