// JavaScript Document

$(document).ready(function() {
	var myScrollable;
	var myCycle;
        var isScrollable = true;
	 
	initAll();
});

function initAll(){
	
	
	//thumbNails
	getCurrentPage();
        
        if(isScrollable){
            setUpScrollable();
        }
        
	//main images
	setUpCycleOptions();
	setUpClickEvents();
	//setUpSubCakes();
	setUpHoverEvents();
	//setUpStartCake();
	
       
        
}

function setDashboardMarker(id) {
    if(id) {
        var cid = parseInt(id);
        myScrollable.click(cid);
        myCycle.cycle({startingSlide: cid});
        myCycle.cycle('pause');
        setUpCakeInfo();
        displayMainImages();
    }
}

function setUpScrollable(){
	
	myScrollable = $("div.scrollable").scrollable({easing: 'jswing', speed: 700, size: 7, clickable: true, api: true});

        // register scrollable seek event function
        myScrollable.onSeek(function() {
            $('#thumbNailInfo').html('showing ' + (this.getIndex()+1) + '-' + (this.getIndex() + this.getVisibleItems().size()) + ' of ' + this.getSize());
	});
	
}

function setUpCycleOptions(){
	myCycle = $('#mainImageContainer').cycle({
        fx:   'fade', 
    	delay: -1000,
        pager:  '.items',
        pagerAnchorBuilder: function(idx, slide) {
        	return '.items img:eq(' + idx + ')'; 
		}
    });
	
	myCycle.cycle('pause');
}

function setUpClickEvents(){
      $(".items img").click(function () {
			if(!$('#mainImageContainer').is(':visible')){
		  		displayMainImages();
		  	}
		  
		  	if(!$(this).hasClass('itemMarkers')){
				/*$("#cakeInfoDiv h1").html($(this).attr('title'));
				$("#cakeInfoDiv p").html($(this).attr('caption'));
                                $("#bettyBubble").html($('.items img.active').attr('quote'));
				$(".items img").removeClass('selectedImg');
				$(this).addClass('selectedImg');*/
                                var gotoHref = $(this).attr('rel');
                                window.location = gotoHref;
			}else{
                            // THIS CODE RUNS ON ITEM MARKERS!!!!
				/*$("#cakeInfoDiv h1").html($(this).next().attr('title'));
				$("#cakeInfoDiv p").html($(this).next().attr('caption'));
                                $("#bettyBubble").html($('.items img.active').attr('quote'));
				$(".items img").removeClass('selectedImg');
				
				var num =  $('.items img').index(this);
                                
				//window.location = '/cakes/category/id/' + (num+1);
				//myScrollable.click(num+1); SHOW APPROPIATE THUMB AND ADD CLASSES
				myCycle.cycle({startingSlide: (num+1)});
				myCycle.cycle('pause');
				
				$(this).next().addClass('selectedImg');
				//$(this).next().addClass('selectedImg');*/
                                var gotoHref = $(this).next().attr('rel');
                                window.location = gotoHref; 
			}
        	
      }

    );
        
       
      //Solves About Page Click issue in IE
      $('#flowerDiv').mouseup(function(){
          var gotoHref = $(this).children('a').attr('href');
          window.location = gotoHref;
          //return false;
      });
	
	/*$('.prevPage').click(function(){
            $('.items').animate({left: ''}, 'slow')
        });
        
        $('.nextPage').click(function(){
            
        });*/

	$(".tabBG").click(function(){
		if($(this)[0] != $('#cakesTab')[0]){
			//trigger anchor within div not cakes tab
			var newLoc = $(this).children("div:not(.tabSelected)").children(".tabText").children("a").attr('href');
		
			if(typeof(newLoc) != 'undefined'){
				window.location = newLoc;
			}
		}else{
			var newLoc = $('#cakesTab .tabTextBG .tabText a').attr('href');
			window.location = newLoc;
		}
	});
	
	$("#cakeInfoDiv").click(function(){
									
		$("#cakeInfoDiv p").slideToggle("slow", function(){
                        var text = $(this).parent().html();
			//text = $(this).parent().html();
			if(text.indexOf("detail_box_plus") != -1){
				$(this).parent().html(text.replace("detail_box_plus", "detail_box_minus"));
			}else{
				$(this).parent().html(text.replace("detail_box_minus", "detail_box_plus"));
			}
		});
	});
	
	
	
	//home page cake thumb clicks
	$("div.scrollable").click(function(){
		setHomePgTn();
	});
	
	$('.cakeTypeBtns').click(function(){
		cType = $(this).children('img').attr('alt');
		getMarkerItemIndex(cType);
	});
	
}

function setUpHoverEvents(){
        $(".items img").hover(
        function () {
            $(this).addClass('hoverImg');
        },
        function () {
            $(this).removeClass('hoverImg');
        }
    );

    
	
	$(".tabBG").hover(
      function () {
        $(this).children("div:not(.tabSelected)").css("background-position","0 -26px");
      }, 
      function () {
        $(this).children("div:not(.tabSelected)").css("background-position","0 0");
      }
    );

    $("#bettyDiv").hover(
      function () {
          $(this).next().css('display','block');
      },
      function () {
          $(this).next().css('display','none');
      }
    );

}

//home page cake thumbs
function setHomePgTn(){
	var currLocation = window.location.href;
	//alert(currLocation);
	if(currLocation.indexOf('/cakes/') == -1){
		var clickedTnIndex = myScrollable.getClickIndex();
		//window.location = '/cakes/category/id/' + clickedTnIndex; MOVE TO THE APPROPIATE PAGE NOW USES A TAG
	}
}

/*function setUpStartCake() {
	
}*/

function setUpCakeInfo(){
	$("#cakeInfoDiv h1").html($('.items img.active').attr('title'));
	$("#cakeInfoDiv p").html($('.items img.active').attr('caption'));
        //$("#bettyBubble #bettyText #innerText").html($('.items img.active').attr('quote'));
        $("#bettyBubble").html($('.items img.active').attr('quote'));
}

function displayMainImages(){
	$('.cakeTypeBtns').slideUp("slow");
	$('#cakeTypeDiv').fadeOut("slow", function(){
		$('#mainImageContainer').fadeIn("slow");
		//$('#cakeInfoDiv').corner();
		$('#cakeInfoDiv').show("slow");	
	});
}

//Instant cake page setup no transisition
function displayMainImagesFromHomePg(){
        
	$('.cakeTypeBtns').css("display", "none");
	$('#cakeTypeDiv').css("display", "none");
	$('#mainImageContainer').css("display", "block");
	$('#cakeInfoDiv').css("display", "block");
	//$('#cakeInfoDiv').corner();
	
}

function getMarkerItemIndex(cType){
        $('.items img').each(function(){
		if($(this).attr('alt') == cType){
			var num =  $('.items img').index(this);
			//window.location = 'index.php?cid=' + (num+1);
			//myScrollable.click(window.location.hash.substr(1));  MOVE TO THE APPROPIATE PAGE NOW USES A TAG
			myCycle.cycle({startingSlide: window.location.hash.substr(1)});
			myCycle.cycle('pause');
			setUpCakeInfo();
			displayMainImages();
		}
	});
}

function getCurrentPage(){
    
    var curPage = window.location.href;
    
    if (curPage.indexOf('/cakes/') != -1){
        $('.tabTextBG').each(function(){
            if($(this).children('div').children('a').html() == 'cake shop'){
              //$('.tabTextBG').removeClass('tabSelected');
              $(this).addClass('tabSelected');
            }
        });
        isScrollable = true;
        return 'cakes';
    }else if (curPage.indexOf('/trade/') != -1){
        $('.tabTextBG').each(function(){
            if($(this).children('div').children('a').html() == 'trade'){
              //$('.tabTextBG').removeClass('tabSelected');
              $(this).addClass('tabSelected');
            }
        });
        isScrollable = false;
        return 'trade';
    }else if (curPage.indexOf('/contact/') != -1){
        $('.tabTextBG').each(function(){
            if($(this).children('div').children('a').html() == 'contact'){
              //$('.tabTextBG').removeClass('tabSelected');
              $(this).addClass('tabSelected');
            }
        });
        isScrollable = false;
        return 'contact';
    }else{
        $('.tabTextBG').each(function(){
            if($(this).children('div').html() == 'home'){
              //$('.tabTextBG').removeClass('tabSelected');
              $(this).addClass('tabSelected');
            }
        });
        isScrollable = true;
        return 'home';
    }
    
}