$(document).ready(function() {
	
	"use strict";
	
	
/*------------corner and draggable stuff -------------*/
	
	//Anything with class .draggable is "draggable"
		//$( ".draggable" ).draggable();
	// Anything draggable gets a rounded corner of 5px
		$( "#page div.draggable" ).corner("5px");
		$( "#copyright" ).corner("5px");
		$( ".cornerthis" ).corner("8px");
		//alert("corners work");
					



/*------------define function variables-------------*/

		var showText='Overview';
		var hideText='List All';
		var clicked=false;
		var windowHeight= Number($(window).height());
		var windowWidth= Number($(window).width());
		
		
		
		
/*- - - - - - - - - - - - - - Change image size on window resize - - - - - - - - - - - */
//on resize screens larger than 1600 wide on resize fire bg check	(works!)

		function imageResize(){
			windowWidth = Number($(window).width());
			windowHeight= Number($(window).height());
			var currentNum = $.cookie('randomPick');
			//alert("fire");

			if(windowWidth > 1590){
				
				
	            $('body').removeClass('img'+ currentNum).addClass('img'+ currentNum +'xl');
				//alert("wide");
				
			}else{
				
	            $('body').removeClass('img'+ currentNum + 'xl').addClass('img'+ currentNum);
				//alert("normal");
				
			}
			
			if (windowHeight <= 680) {
				
				/*****check and hide copyright unless there's enough room on initial load*****/
				//*****if window is resized window size check will fire again and display or hide footer******/
               
				$('#footer').hide();
				
				
		   }else{
				
				$('#footer').show();
				
			}
				return windowHeight;

		}
		
		
		
/*------------toggle hide/show stuff-------------*/
		
		//$('div.toggle').hide();

		// append show/hide links to the element directly preceding the element with a class of "toggle"
		//var target = $(this).parent().attr("id");
		
		$('.target').prepend('<a href="#" class="toggleLink">'+hideText+'</a>');
			

		$('a.toggleLink').click(function() {
            //alert("clicked");
			var targetdiv = $(this).parent().attr("id"); 
 
			var currentText = $("#" + targetdiv + " .toggleLink").text();

			if(currentText === hideText){
			//alert("flip to show");
			$("#" +targetdiv + " .toggleLink").text(showText);
			$("#toggle" + targetdiv +"2").slideDown('fast'); 
			$("#toggle" + targetdiv).slideUp('fast'); 
			
				if (windowHeight <= 740){
	                $('#footer').hide();
	                //alert(windowHeight + " check 1");
	            }
	
			clicked = true;
			//alert(clicked);
			
				

			} else {
			//alert("flip to hide");
				
				if (clicked === true) {
					if (windowHeight >= 650){
		                $('#footer').show();
		                //alert(windowHeight + " check 2");
		            }else{
						$('#footer').hide();
			            clicked = false;
			            //alert(windowHeight + " check 3");
			        }		
			      }else { 
					    $('#footer').hide(); 
		         }
		    
		
		    $("#" + targetdiv+ " .toggleLink").text(hideText);
			$("#toggle" + targetdiv + "2").slideUp('fast');
			$("#toggle" + targetdiv).slideDown('fast');
			 
			//clicked = false;
			//alert(clicked);
            
         }
			
			
			//clicked = true;
			return false; 
			//this link really doesn't go anywhere so kill any further action, returns no result other than above actions
				});
	
	
	
	
	
	
	
/*------------ Load images ------------*/
	
	 if (document.images)
	    {
		//alert("loading images");
	      var preload_image_object = [];
	      // set image url
	      var image_url = [];
	      image_url[0] = "/images/big/cab1_2700.jpg";
	      image_url[1] = "/images/big/cab2_2700.jpg";
	      image_url[2] = "/images/big/cab3_2700.jpg";
	      image_url[3] = "/images/big/cab4_2700.jpg";
	      image_url[4] = "/images/big/cab5_2700.jpg";
          image_url[5] = "/images/big/cab0_2700.jpg";
	      
	      var i = 0;
              for(i=0; i<=3; i++) {preload_image_object.src = image_url[i];
              }
	    }
	
		
		
		
		function pickNum(){

		//*------------define function variables-------------*//


		var maxCount = 6;
		// set the max of the counter, in my tests "4" = (0,1,2,3) I adjusted below (+1) to get a "real" 4 (0,1,2,3,4) this is in reality 5 keys to humans, you can adjust script to eliminate "0", but my script makes use of the "0"


		var randomNum = (Math.floor(Math.random() * maxCount)) + 1;

		//give me a random number limited by the max, adding "1" because computers start counting at "0" (eliminating "0" position)

		var cookieContents;

		if ($.cookie('randomPick')=== null) {

		        // check if random number cookie is not set
		        //alert( "not set");
                //set the cookie for the first time
		        $.cookie('randomPick', randomNum, {
		            expires: 7,
		            path: '/'
		        });


		        cookieContents = $.cookie('randomPick');

		        //-------------hold the last number if it was set before

		} else {

			    cookieContents = $.cookie('randomPick');

			    //-------------hold the last number if it was set before
                //alert("cookie contents: " + cookieContents);
                //alert("random number: " + randomNum);
                
				
				//------------- Check if numbers match? If so add 1

				if (Number(cookieContents) === Number(randomNum)){
					
                //alert( "(" + cookieContents + ", " + randomNum + ", max: " + maxCount +"), they match");
				randomNum = ++randomNum;
				//alert(randomNum + " increment");

				}
				
		        //------------- let's make sure we didn't got over the maxCount, force it to be 1 less than maxCount
		        if (randomNum > maxCount){
			    randomNum = --maxCount;
		        }
		
		        //------------- All done with checks? reset cookie with results of check
		        $.cookie('randomPick', randomNum);
		        
				}
                
                //-------------store checked or processed cookie contents in a variable-------//
		        var currentNum = $.cookie('randomPick');
		        //alert("is set:" + currentNum);
		        return currentNum;
         
		}
		
		
		
		$("body").addClass("img" + pickNum());
        

		//imageResize();

		
		$(window).bind("resize",function(){
			imageResize();
		});
			
});
