	var rps = jQuery.noConflict();
	
	rps(document).ready( function() {
	    // var url = 'http://localhost/123shoot/carry-ontrailer/';
	    // var url = 'http://www.123shootdev.com/carry-ontrailer/';        
        
        rps(".fi_pane, .hide_on_js, .dynamic_message, .hide_no_cat, .hide_no_group, .optgroups, #image_portal img, #image_portal").hide();
				
		rps(".current_pane").show();
		
		rps("a.thumb2feat").click(function(e){
			e.preventDefault();
			var new_pane = "#" + rps(this).attr('title');
			var new_thumb = "#" + rps(this).attr('id');
			rps(".current_pane").hide().removeClass("current_pane");
			rps(new_pane).show().addClass("current_pane");
		});
		
		rps(".plus_one_img").click(function(e){
			e.preventDefault();
			var plus = "#" + rps(".img_array li:last").attr('title');
			plusnum = rps(".img_array li:last").attr('title');
			parseInt(plusnum);
			++plusnum;
			if(plusnum >= 5) {
				alert("You can only add 4 images.");
			} else {
				rps(".img_array").append('<li title="' + plusnum + '"><input type="file" name="prodimgarray[]" /></li>');
			}
		});
		
		rps(".remove_this_image").click(function(e) {
			e.preventDefault();
			var answer = confirm("Are you sure?");
			if(answer) {
				todelete = "#" + rps(this).attr('title');
				rps(todelete).fadeOut( function () {
					rps(this).remove();
					rps('.first_dm').append('<div class="update">You must update to delete the images.<br /><br /> <input type="submit" value="Update Now" name="submit" /></div>').slideDown().removeClass('first_dm');
				});
			}
		});


		//Adds another tire VIN field to the product registration form
		rps(".plus_one_tire").click(function(e){
			e.preventDefault();
			var plus = "#" + rps(".tire_array li:last").attr('title');
			plusnum = rps(".tire_array li:last").attr('title');
			parseInt(plusnum);
			++plusnum;
			rps(".tire_array li:last").clone().children().attr('value', '').appendTo(".tire_array").attr('title', plusnum);
		});
		
		rps("#sortable").sortable().disableSelection();
		
		var fixHelper = function(e, ui) {
		    ui.children().each(function() {
		        rps(this).width(rps(this).width());
		    });
		    return ui;
		};
		 
		rps("#sortTable tbody").sortable({
		    helper: fixHelper
		}).disableSelection();

		// Sitemap toggle
			rps(".toggle a").click(function(e) {
				var shown = rps(this).parent().hasClass('shown');
				e.preventDefault();
				if(shown) {
					var hide = '#' + rps(this).attr('id').replace('show_', '');
					rps(hide).hide();
					rps(this).parent().removeClass('shown');
				} else {
					var show = '#' + rps(this).attr('id').replace('show_', '');
					rps(show).show();
					rps(this).parent().addClass('shown');
				}
				
			});
		
		
		// Image Portal Actions
		
		      (function() {
                var httpRequest;            
                
                rps(".drill_menu").change(function() {
                        var id = rps(this).attr('value');
                        var type = rps(this).attr('id');
                        sendString = 'type=' + type + '&id=' + id;
                        var drill = rps(this).attr('name');
                        // var sendString = 'name=' + name + '&type=' + type;
                        //makeRequest('/123shoot/carry-ontrailer/ajax/get-dropmenu', sendString, drill); 
                        makeRequest(rpsajax.ajax_url + '/get-dropmenu', sendString, drill); 
                });
                
                rps(".ajax_prod_imgs").click(function(e) {
                	e.preventDefault();
                    rps('#image_portal #featured_image, #image_portal .thumb_window, #image_portal #image_portal_title').hide();
                    rps('#image_portal').addClass('empty').show();
                	var cotnum = rps(this).attr('title');
                	sendString = 'cotnum=' + cotnum;
                	// makeRequest('/123shoot/carry-ontrailer/ajax/get-image', sendString, 'image');
                	makeRequest(rpsajax.ajax_url + '/get-image', sendString, 'image');
                });
                
                function makeRequest(url, sendString, drill) {
                    if(window.XMLHttpRequest) {
                        httpRequest = new XMLHttpRequest();
                        
                    } else if (window.ActiveXObject) {
                        try {
                            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                        }
                        catch(e) {
                            try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            catch(e) { }            
                        }
                    }
                    
                    if(!httpRequest) {
                        alert('Giving up: (Cannot create an XMLHTTP instance');
                        return false;
                    }
                    
                    if(drill == 'drill_group') {
                        httpRequest.onreadystatechange = alertContentsGroup;
                    } else if(drill == 'drill_product') {
                        httpRequest.onreadystatechange = alertContentsProduct;
                    } else if(drill == 'image') {
                        httpRequest.onreadystatechange = alertImage;
                    }
    
    
    
                    httpRequest.open('POST', url);
    
                    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    httpRequest.send(sendString);
                    
    
                }
                function alertImage() {
                    if(httpRequest.readyState === 4) {
                        if(httpRequest.status === 200) {
                        	var string = httpRequest.responseText;
                        	// alert(string);
                        	rps('#image_portal img').hide();
                        	if(string.indexOf("|") != -1) {
                        	
	                        	var array = explodeArray(string, '|');
	                        	for(i in array) {
	                        		var product = explodeArray(array[i], ',');
	                        		var t = i;
	                        		t++;
	                        		var thumbnail = '#thumb_' + t + ' img';
	                        		var mainimg	  = '#feat_' + t + ' img';
	                        		var widthspan = '#download_width_' + t;
	                        		var heightspan = '#download_height_' + t;
	                        		product[1] = rpsajax.site_url + product[1];
	                        		var large = product[1];
	                        		product[1] = product[1].replace('large', 'medium');
												
	                        		rps(thumbnail)
	                        		.attr('src', '').attr('width', product[2]).attr('height', product[3]).attr('src', product[1])
	                        		.show();
	                        		rps(mainimg)
	                        		.attr('src', '').attr('width', product[4]).attr('height', product[5]).attr('src', product[1])
	                        		.show()
	                        		.parent().attr('href', large);
	                        		rps(widthspan).empty().append(product[6]);
	                        		rps(heightspan).empty().append(product[7]);
	                        		rps("#image_portal_title").empty().append(product[0]);
	                        		
	                        	}
	                        } else {
	                        
	                        		var mainimg	  = '#feat_1 img';
	                        		var widthspan = '#download_width_1';
	                        		var heightspan = '#download_height_1';
	                        		var product = explodeArray(string, ',');
	                        		// alert(product[1]);
	                        		var large = product[1];
	                        		product[1] = product[1].replace('large', 'medium');
	                        		rps("#thumb_slider").hide();
	                        		rps(mainimg)
	                        		.attr('src', '').attr('width', product[2]).attr('height', product[3]).attr('src', product[1])
	                        		.show()
	                        		.parent().attr('href', large);
	                        		rps("#image_portal_title").empty().append(product[0]);
	                        		rps(widthspan).empty().append(product[2]);
	                        		rps(heightspan).empty().append(product[3]);
	                        }
                        	rps('#image_portal #featured_image, #image_portal .thumb_window, #image_portal #image_portal_title').show();
                        	rps('#image_portal').removeClass('empty');
                        	rps('#image_portal img').show();
	                        // alert(httpRequest.responseText)
                        } 
                    }                
                }
                
                function alertContentsGroup() {
                    if(httpRequest.readyState === 4) {
                        if(httpRequest.status === 200) {
                        
                            rps('#group').empty().show().append(httpRequest.responseText);
    
                        } else {
                            alert('There was a problem with the request.');
                        } 
                    }
                }
                
                function alertContentsProduct() {
                    if(httpRequest.readyState === 4) {
                        if(httpRequest.status === 200) {
                            
                            rps('#product').empty().show().append(httpRequest.responseText);
    
                        } else {
                            alert('There was a problem with the request.');
                        } 
                    }
                }
                
                
            }) ();
            
            function explodeArray(item,delimiter) {
				tempArray=new Array(1);
				var Count=0;
				var tempString=new String(item);
				while (tempString.indexOf(delimiter)>0) {
					tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
					tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
					Count=Count+1
				}
				tempArray[Count]=tempString;
				return tempArray;
			}
                 
		  // If a category is selected
    		rps("#category").change(function() {
    		        
    		        rps("#product").empty().hide();
    		    
    		    if(rps(this).attr('value') != 'default') {
    		        rps('.slide').hide();
                    rps(".slider_container").animate({left: '0'});
                    rps('#sbps').attr('value', 0);
                    
                    var category = '.category_' + rps(this).attr('value');
                    rps(category).fadeIn();
                    var n = rps(category).length - 4;
                    rps('#drilled_max').attr('value', n);
                    rps('#drilled_to').attr('value', 'true');
                }
                
                if(rps(this).attr('value') == 'default') {
                    rps(".slide").show();
                    rps("#group").hide(function() {
                        rps(this).attr('value', 'default').empty();
                    }).removeClass(".hide_no_cat");
                    rps('#drilled_to').attr('value', '');
                
                }
    		});
    		
    		rps("#group").change(function() {
        		  if(rps(this).attr('value') != 'default') {
        		        rps('.slide').hide();
                        rps(".slider_container").animate({left: '0'});
                        var group = '.group_' + rps(this).attr('value');
                        var n = rps(group).length - 4;
                        rps(group).fadeIn();
                    }
        		  
        		  if(rps(this).attr('value') == 'default') {
                    var category = '.category_' + rps('#category').attr('value');
                    rps(category).fadeIn();
                  } 
    		});

            var maxWidth = rps('.tot_width').attr('title');
            parseInt(maxWidth);    
            
            // If the slidepane changer arrows are clicked...
            rps('.arrow').click(function(e) {
                /*
                    This click function finds the id of the arrow you just clicked on and cycles through the appropriate
                    actions depending on the id.
                    
                    PARAMETERS
                    var maxslide        - Gives us the most amount of slides we can 'flip' to.
                    var leftVal         - Gets how far the left positioning currently is.
                    var currentslides   - The amount of slides we've cycled through so far.
                    var newVal          - Sets up the new value for the hidden input that holds our currentslides value.
                
                */
                
                e.preventDefault();
                
                // The #category & #group change functions set this for us.                 
                if(rps('#drilled_to').attr('value') == 'true') {
                    var maxslide = parseInt(rps('#drilled_max').attr('value'));
                } else {
                    var maxslide = parseInt(rps('#max_slide').attr('value'));
                }
                
                var leftVal = parseInt(rps('.slider_container').css('left'));
                
                var currentslides = parseInt(rps('#sbps').attr('value'));
                
                var count = maxslide - currentslides;

                if(rps(this).attr('id') == 'scroll_forward_pane_slider') {

                    if(currentslides < maxslide) {
                        if(count > 0) {
                            if(count < 5) {
                                var dist = count * 146;
                                rps('.slider_container').animate({left: '-=' + dist});
                                var newVal = parseInt(rps("#sbps, #sbis").attr('value')) + count;
                            } else {
                                rps(".slider_container").animate({left: '-=584'});
                                var newVal = parseInt(rps("#sbps, #sbis").attr('value')) + 4;
                            }
                            
                        }
                    }
                }
                
                if(rps(this).attr('id') == 'scroll_forward_image_slider') {
                    if(currentslides < maxslide) {
                        if(count > 0) {
                            rps(".slider_container").animate({left: '-=146'});
                            var newVal = parseInt(rps("#sbps, #sbis").attr('value')) + 1;
                        }
                    }
                }
                
                if(rps(this).attr('id') == 'scroll_back_pane_slider') {
                    if(currentslides > 0) {

                        // Checks if we're on the second slide pane. If we're not, it goes through this, the else sets any second pane 'flip back a pane' back to 0.
                        if(leftVal < -584) {
                            rps(".slider_container").animate({left: '+=584'});
                            var newVal = parseInt(rps("#sbps").attr('value')) - 4;
                        } else {
                            rps(".slider_container").animate({left: '0'});
                            var newVal = 0;
                        }
                    }
                }
                
                if(rps(this).attr('id') == 'scroll_back_image_slider') {
                    if(currentslides > 0) {
                        rps(".slider_container").animate({left: '+=146'});
                        var newVal = parseInt(rps("#sbps, #sbis").attr('value')) - 1;
                    }
                }
                                
                rps("#sbps").attr('value', newVal);

            });       
    
            
            rps(".slide a").click(function(e) {
                e.preventDefault();
                var trailer = '#featured_' + rps(this).attr('id');
                var num = rps(this).attr('id').replace('trailer_', '');
                var feat_img = '#feat_img_' + num + '_1'; 
                if(rps(feat_img).attr('class') != 'current_feat_img') {
	                rps('.current_feat_img').removeClass('current_feat_img').addClass('save_to_hide');
	                rps(feat_img).addClass('current_feat_img').removeClass('save_to_hide').show();
	            }
                
                rps('.shown_trailer').hide().removeClass('shown_trailer');
                rps(trailer).show( function() {
                	rps('.save_to_hide').hide();
                }).addClass('shown_trailer');
            });
            
            rps("a.feature_img_changer").click(function(e) {
            	e.preventDefault();
            	var featured = '#' + rps(this).attr('id').replace('_show', '');
                if(rps(featured).attr('class') != 'current_feat_img') {
	            	rps('.current_feat_img').hide().removeClass('current_feat_img');
	            	rps(featured).show().addClass('current_feat_img');
	            }
            });
                    

        
	});
	
		
	

