	$(document).ready(function(){
		if(typeof do_resize_window!="undefined" && do_resize_window == 1) {
			set_window();
		}
		
		$("#also_bought_div").show();  
		// get the window size
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape" || navigator.appName=="Opera") {
			 	window_width = window.innerWidth;
			 	window_height = window.innerHeight;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
	  			window_width = document.body.offsetWidth;
				window_height = document.body.offsetHeight;
				window_width = document.body.offsetWidth < 365 ? 365 : document.body.offsetWidth;
	 		}
		}
		else if(jQuery.browser.safari){
			window_width = window.innerWidth < 356 ? 365 : window.innerWidth;
			window_height = window.innerHeight < 400 ? 400 : window.innerHeight;
			
		}
		$(".content_loading").hide();  

		var item_count = 10;

		// number to show based on the window width
		if(typeof( window.also_bought_products_per_page ) === 'undefined') {
			var number_to_show = Math.floor((window_width-200)/160);
		} else if( also_bought_products_per_page > 1 ) {
			var number_to_show = also_bought_products_per_page;
		} else {
			var number_to_show = Math.floor((window_width-200)/160);
		}
		
		var page_count = Math.ceil(item_count/number_to_show);
		$(".also_bought_page_count").text(page_count);	              	
		
		var strip_count = $(".also_bought_section").length;

		// going to initialise page numbers below
		page_numbers = new Array();

		// show everything
		if (strip_count != 0) {
			for ( var n = 0; n < strip_count; n++ ) {
				for ( var i = 1; i <= number_to_show; i++ ) {
					$("#also_bought_subtable_"+n+"_"+i).show();
				}
				
				// initialise page number
				page_numbers[n] = 1;				
			}
		}
				
		// need to give this a bit of a clean up
		var table_width = window_width-100;
		var subtable_width = window_width-360;

		// CLICK LEFT ARROW
		$("a.also_bought_arrow_left").click(function(event) {
			var strip_number = $(this).parents("div").find("input.strip_number").val();
			var generated_url = $(this).parents("div").find("input.xml_url").val();

			// we don't want to let the page number get below 1.  That would be crazy.
			if (page_numbers[strip_number] > 1) {
	     		page_numbers[strip_number]--;
    	 		display_strip(page_numbers[strip_number], number_to_show, strip_number, generated_url, false);
    	 	} 
    	 	
			event.preventDefault();
		});		

		// CLICK RIGHT ARROW
		$("a.also_bought_arrow_right").click(function(event) {
			var strip_number = $(this).parents("div").find("input.strip_number").val();
			var generated_url = $(this).parents("div").find("input.xml_url").val();
			var page_count = $("#also_bought_page_count_"+strip_number).text();			
		
			// Can't be going beyond the final page.  There be monsters.
			if (page_numbers[strip_number] < page_count ) {
	     		page_numbers[strip_number]++;
	     		display_strip(page_numbers[strip_number], number_to_show, strip_number, generated_url, false);
	     	}
	     		
     		event.preventDefault();
		});		

		// Load the data for the page - this is before any clicks happen
		var strip_count = 0;
		$("#also_bought_div .xml_url").each( function() {
			var generated_url = $(this).val();
			display_strip(page_numbers[strip_count], number_to_show, strip_count, generated_url, false);
			strip_count++;
		});
		
		// add click handler for merchandise_feedback
		$(".merchandise_feedback_link").click(function(event) {
			// get the format id from the href
			var class_id = $(this).attr('href');

			var strip_number = $(this).parents("div").find("input.strip_number").val();
			var generated_url = $(this).parents("div").find("input.xml_url").val();

			// need to get the strip number here
			merchandise_feedback(class_id, number_to_show, strip_number, generated_url);
//			$('#merchandise_feedback_single_'+strip_number+' > a').css({color: 'white'});
			
//			$('#merchandise_feedback_single_'+strip_number).show();			
			event.preventDefault();
		});
	/* end document ready */	
	});

	/**
	 *  Where all the magic happens.  This method takes loads the XML document containing our products, 
	 *  parses the results and sets up the display
	 * 	parameters:
	 * 	page_number: the number of the page (left-right scrolling) - starts at 1
	 *  number_to_show: dependent on screen width	
	 *  generated_url: the url of the script to generate the XML
	 *  merch_feedback_recalculate: if the user has clicked a 'don't show me' link then we might need to 
	 *  	recalculate the number of pages if we're running out of products
	 */
	function display_strip(page_number, number_to_show, strip_number, generated_url, merch_feedback_recalculate) {
		$(".also_bought_title").show();
	
		// remove all separators
		$("#also_bought_subtable_"+strip_number).find("td.separator").hide();
		var strip_type = $("#strip_type_"+strip_number).val();
	
		var where_to_start = (page_number-1) * number_to_show;
		var where_to_end = where_to_start + number_to_show;

		$("#also_bought_subtable_"+strip_number).hide();  			
		$("#content_loading_"+strip_number).show();  
	
		// Get this show on the road
	    $.ajax({
	        url: generated_url,
	        type: 'GET',
	        dataType: 'xml',
	        timeout: 50000,
	        error: function(){
				$("#content_loading_"+strip_number).hide();  
				$("#error_loading_"+strip_number).show();  		
	        },
	        success: function(xml){
				$("#content_loading_"+strip_number).hide();  
	        
				$("#also_bought_subtable_"+strip_number).show();  		
				$("#separator_"+strip_number+'_'+counter).show();
				
	        	// counter!
	        	var counter = 1;
	        	
	        	var items_found_in_xml = $(xml).find('item').slice(where_to_start,where_to_end).length;
	        	var total_items_in_xml = $(xml).find('item').length;
	        	
	        	// if there's not enough results to fill the total number of pages then we renumber the page count
	        	if (page_number==1) {
		        	var page_count = Math.ceil(total_items_in_xml/number_to_show);
					$("#also_bought_page_count_"+strip_number).text(page_count);	              	
				}        	
		        	
	            $(xml).find('item').slice(where_to_start,where_to_end).each(function() {
					// set values for the items	            
	                var title = $(this).find('title_cut').text();
	              	var artist = $(this).find('artist_cut').text();
	              	var label = $(this).find('label_cut').text();
	              	var cover_image = $(this).find('cover_image').text();
	              	var class_id = $(this).find('class_id').text();
	              	
	              	var currency_symbol = $(this).find('currency_symbol').text();
	              	var price_with_vat  = $(this).find('currency_symbol').text() + $(this).find('price_with_vat').text();
	              	 
	              	var listen_link = $(this).find('listen_link').text();
	              	var merchandise_class = $(this).find('merchandise_class').text();
	              	var label_encoded = $(this).find('label_encoded').text();
	              	var artist_encoded = $(this).find('artist_encoded').text();
	              	var popup_url = $(this).find('popup_url').text();
	              	var url = $(this).find('url').text();
	              	
	              	// 0005212: Change url structure of information landing pages for a trial period
					// Temporary
	              	var nonpps_img_lnk = $(this).find('nonpps_img_lnk').text();
	              	
	              	var title_id = $(this).find('title_id').text();
	              	var product_id = $(this).find('product_id').text();
	              	var mix_title = $(this).find('mix_title_cut').text();
	              	var display_format = $(this).find('display_format').text();	              	

	              	// merchandise is now only going to display a title
	              	if (strip_type=='merchandise') {
	              		$("#also_bought_label_"+strip_number+"_"+counter).hide();	              		
	              		$("#also_bought_artist_"+strip_number+"_"+counter).hide();
	              		
		              	// merchandise specific - append the class name
		              	var merchandise_class = $(this).find('merchandise_class').text();
	           			$("#merchandise_feedback_link_"+strip_number+"_"+counter+"_item").text(merchandise_class);
	           			$("#merchandise_feedback_link_"+strip_number+"_"+counter).attr('href', class_id);
		              	
		              	// single class merch specific
		              	$("#merchandise_feedback_link_single_"+strip_number+"_link").attr('href', class_id);
	           			$("#merchandise_feedback_link_single_"+strip_number+"_item").text(merchandise_class);
	           			$("#merchandise_feedback_single_"+strip_number).show();
	           			$("#also_bought_single_title_"+strip_number).text(merchandise_class);

		                $("#also_bought_title_"+strip_number+"_"+counter).text(title);
		                $("#also_bought_title_"+strip_number+"_"+counter).css({height:400});

	              		
	              	} else {
	              		
	              		// NOT MERCHANDISE
		              	// physical and digital audio - add listen link and bind it to juno player
		              	$("#listen_link_"+strip_number+"_"+counter).attr('href', listen_link).bind('click', function() {
							return load_m3u_playlist("http://" + location.hostname + '/' + listen_link);
						});
			              	
		              	var track_values = $(this).find('title_id').text()+'-'+$(this).find('product_id').text()+'-'+$(this).find('track_number').text()+'-'+$(this).find('media_id').text()+'-';
		                $("#also_bought_artist_"+strip_number+"_"+counter).text(artist);
		                $("#also_bought_title_"+strip_number+"_"+counter).text(title);

						// if we don't have a mix title, display the label in that slot.  
						// otherwise display mix title then label
		                if (mix_title!=''&label!='') {
			                $("#also_bought_mix_title_"+strip_number+"_"+counter).text(mix_title);
			                $("#also_bought_label_"+strip_number+"_"+counter).text('('+label+')');
		                } else if (mix_title==''&label!='') {
		                
			                //$("#also_bought_label_"+strip_number+"_"+counter).text(mix_title);
			                if (strip_type=='tracks') {
			                	// DIGITAL AUDIO - label div will be a blank space, label name will go in mix title div
				                $("#also_bought_mix_title_"+strip_number+"_"+counter).text('('+label+')');
				                $("#also_bought_label_"+strip_number+"_"+counter).html('&nbsp;');
				            } else {
				            	// PHYSICAL AUDIO
				                $("#also_bought_label_"+strip_number+"_"+counter).text('('+label+')');
				            }
			            } else {
			            	// this shouldn't happen - everything should have a label
			            	$("#also_bought_mix_title_"+strip_number+"_"+counter).html('&nbsp;');
			                $("#also_bought_label_"+strip_number+"_"+counter).text('(no label)');
			            	
			            }
		                $("#also_bought_display_format_"+strip_number+"_"+counter).text('('+display_format+')');

						// DIGITAL PRICES
		              	var options = '';
		              	
		              	// loop over the prices and put all them in to the options string
		              	$(this).find('digital_price').each(function() {
		              		var option_name = $(this).parents('item').find('currency_symbol').text() + $(this).find('price').text() + ' - ' + $(this).find('name').text();
		              		var track_format_key = track_values + $(this).attr('format_key');
		              		var format_key = $(this).attr('format_key');
		              		var sale_format = $(this).parents('item').find('sale_format').text();
		              		var selected = '';
		              		if(format_key == sale_format) {
		              			selected = 'selected';
		              		} else {
		              			selected = '';
		              		}
					        options += '<option value="' + track_format_key + '" ' + selected + '>' + option_name + '</option>';
						});
						// set the drop down options to be the string 
						$("#also_bought_digital_prices_"+strip_number+"_"+counter).html(options);
					}
						
					// PHYSICAL PRICES
					$("#also_bought_physical_price_"+strip_number+"_"+counter).html(price_with_vat);

					// add the click handlers for the links
					var link_type_new_product = 'new_product';
					if(class_id == '10' || class_id == '24') {
						popup_url = url;
						link_type_new_product = '';
					}
					
					add_product_click_handler("#also_bought_label_"+strip_number+"_"+counter, 'labels/'+label_encoded, 'opener', strip_type);
					add_product_click_handler("#also_bought_artist_"+strip_number+"_"+counter, 'artists/'+artist_encoded, 'opener', strip_type);
					add_product_click_handler("#also_bought_title_"+strip_number+"_"+counter, popup_url, link_type_new_product, strip_type);
					add_product_click_handler("#also_bought_image_link_"+strip_number+"_"+counter, popup_url, link_type_new_product, strip_type);
					add_product_click_handler("#buy_link_"+strip_number+"_"+counter, 'cart/add/?popup=yes&titleid='+title_id+'&productid='+product_id+'&formatid='+class_id, 'adding_to_cart', strip_type);
	              	
	              	// Put the image in
              		$("#also_bought_image_"+strip_number+"_"+counter).attr("src", cover_image);
              		$("#also_bought_image_"+strip_number+"_"+counter).attr("alt", artist+" - "+title);	              		
	        
	              	// Update page numbering
	              	$("#also_bought_page_number_"+strip_number).text(page_number);
	              	
	              	// make sure it's shown
		           	$("#also_bought_"+strip_number+'_'+counter).show();
		           	
					if (counter < items_found_in_xml) {
			        	$("#separator_"+strip_number+'_'+counter).show();
					}

					counter++;
	            });

	            var items_found = counter-1;
	            
	            // put us back at page 1 if we've not got enough items for multiple pages
	            if (items_found<number_to_show && page_number ==1) {
	            	$("#also_bought_page_count_"+strip_number).text(1);
	           	}	            
	            
	            // remove the display for items that we don't have a product for
	            if (items_found < number_to_show) {
		            for ( var i = counter; i < (number_to_show+1); i++) {
		            	$("#also_bought_"+strip_number+'_'+i).hide();
		            }
				}
				
				// recalculate page count
				if (merch_feedback_recalculate) {
					var page_count = Math.ceil(counter/number_to_show);
					$("#also_bought_page_count_"+strip_number).text(page_count);	              	
				}
				
				
			
				
			} //ends success 
		}); 
	}

	/* process 'don't show me this' feedback */
	function merchandise_feedback(class_id, number_to_show, strip_number, generated_url) {
		$.ajax({
		   url: '/merchandise_feedback.php?class_id='+class_id,
		   processData: false,
		   success: function() { // now reload the list
				display_strip(1, number_to_show, strip_number, generated_url, true);
		    	return false;
	   		}, error: function() { // reload the list anyway
	   			display_strip(1, number_to_show, strip_number, generated_url, true);   		
		     	return false;
	   		}
		});
	}
	
	/**
		processes the add to cart drop-down for digital releases
	*/
	function digital_release_add_from_pwbt(url, strip_number, count) {
		$('#also_bought_digital_prices_'+strip_number+'_'+count+' option:selected').each(function() {
			var track = $(this).val();
			var full_url = url + '?popup=yes&track[]=' + track;
			addToCartWindow(full_url, 4);
		});
	}	

	/**
		Handles the clicking of links in the products in the strips
			id = the element id that has been clicked
			url = the url we will take the user to
			link_type = can be:
				 'adding_to_cart' - adds the product to the user's cart - loads the adding to cart popup
				 'opener' - the link loads in the non-pop-up window - ie the original
				 'new_product' - opens the product pop-up
			strip_type = 'physical', 'tracks' or 'merchandise'	 
	*/
	function add_product_click_handler(id, uri, link_type, strip_type) {
		$(id).attr('href', 'http://' + location.hostname+'/'+uri);
		$(id).click( function(event) {
			if (link_type == 'adding_to_cart') {
				if (strip_type=='merchandise') {
					media_id = 2
				} else {
					media_id = 1
				}
				addToCartWindow(uri, media_id);
			} else if (link_type == 'opener') {
				opener.document.location.href= 'http://' + location.hostname+'/'+uri;
				window.opener.focus();
			} else if (link_type == 'new_product'){
				NewWindow(uri);
			} else {
				// load in current window
				window.document.location.href= 'http://' + location.hostname+'/'+uri;								
			}
			event.preventDefault();
		});
	}
	
