/* Drop Down Menu */
jQuery.fn.dropDownMenu = function(){
    var $headers=$('ul ul',this).parent();
    $headers.each(function(i){
        var $curobj=$(this);
        var $subul=$(this).find('ul:eq(0)');
        this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()};
        this.istopheader=$curobj.parents("ul").length==1? true : false;
        $subul.css({top:this.istopheader? this._dimensions.h+"px" : 0});
        $curobj.hover(
            function(e){
                var $targetul=$(this).children("ul:eq(0)");
                this._offsets={left:$(this).offset().left, top:$(this).offset().top};
                var menuleft=this.istopheader? 0 : this._dimensions.w;
                menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width()) ? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft;
                if ($targetul.queue().length<=1) {
                    $targetul.css({left:menuleft+"px", width:this._dimensions.subulw+'px'}).slideDown(200)
                }
            },
            function(e){
                var $targetul=$(this).children("ul:eq(0)")
                $targetul.slideUp(200)
            }
        ); //end hover
    }); //end $headers.each()
    $headers.find("ul").css({display:'none', visibility:'visible'});
}
jQuery.fn.slideShows = function(options){
	var slides = $(this);
	/* default variables */
	var defaults = {
		delaytime : 5000,
		animation : true
	};
	/* assign options */
	var options = $.extend(defaults, options);	
	/* assign variables */
	var delaytime = options.delaytime;
	var animation = options.animation;
	/* class indexing */
	$('.slide-content',this).each(function(index){
		$(this).addClass('slide-'+index);
		$(this).hover(function(){
    		clearTimeout(timeoutslide);
		},function(){
    		if(animation==true){timeoutslide = setTimeout(simpleslide,delaytime);};
		});
	});
	/* how many slide contents? */
	var totalslides = $('.slide-content',this).length;
	/* Begin The Show */
	    var i=0; //Index of slide
	    /* slide animation */
	    var simpleslide = function simpleslide() {	        
	        clearTimeout(timeoutslide);
	        /* end of slides */
	        if(i==totalslides-1){
	            i = 0;
	        } else {
	            i = ++i;
	        }
	        /* animate and assign classes */
	        $('.slide-nav li a', slides).removeClass('selected').eq(i).addClass('selected');
	        if($.browser.msie){
    	        $('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+i).show().removeClass('hidden');
	        }else {
    	        $('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+i).fadeIn().removeClass('hidden');
	        }	        
	        timeoutslide = setTimeout(simpleslide,delaytime);
	    };
	/* animate or not? */   							       
	if(animation==true){
		timeoutslide = setTimeout(simpleslide,delaytime);
	};
	/* Navigation */
	$('.slide-nav li a', this).each(function(index){													
		$(this).addClass('slide-'+index);
		$(this).hover(function(){ 
    		clearTimeout(timeoutslide);           
			$(this).addClass('selected').parent().siblings().children().removeClass('selected');
			if($.browser.msie){ // Damn you, IE6
    	        $('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+index).show().removeClass('hidden');
	        }else {
    	        $('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+index).fadeIn().removeClass('hidden');
	        }
		},function(){
    		if(animation==true){timeoutslide = setTimeout(simpleslide,delaytime);};
		});					
	}); // end of navigation
};
/* Gallery Slide */
jQuery.fn.gallerySlide = function(options){
	var slides = $(this);
	/* default variables */
	var defaults = {
		delaytime : 3000,
		animation : true,
		showthumb : 0
	};
	/* assign options */
	var options = $.extend(defaults, options);	
	/* assign variables */
	var delaytime = options.delaytime;
	var animation = options.animation;
	var showthumb = options.showthumb;
	/* class indexing */
	$('.slide-content',this).each(function(index){
		$(this).addClass('slide-'+(index+1));
	});
	/* how many slide contents? */
	var totalslides = $('.slide-content',this).length;

	if(showthumb==0){
    	/* Begin The Show */
    	    var i=1; //Index of slide
    	    /* slide animation */
    	    var galleryslide = function galleryslide() {
    	        clearTimeout(gallerytimeout);
    	        /* end of slides */
    	        if(i==totalslides){
    	            i = 1;
    	        } else {
    	            i = ++i;
    	        }
    	        /* animate and assign classes */
    	        $('.slide-nav li a', slides).removeClass('selected').eq(i).addClass('selected');
    	        $('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+i).fadeIn().removeClass('hidden');
    	        gallerytimeout = setTimeout(galleryslide,delaytime);
    	    };
    	/* animate or not? */   							       
    	if(animation==true){
    		gallerytimeout = setTimeout(galleryslide,delaytime);
    	}
    	/* Navigation */
    	$('.slide-nav li a', this).each(function(index){
    		/* exclude previous and next button from content index */
    		if($(this).hasClass('prev') || $(this).hasClass('next')) {
    			/* in case it is previous or next: */											
    			switch($(this).attr('class')){
    				case 'prev':
    					$(this).click(function(){
    						/* cancel out the show */
    						clearTimeout(gallerytimeout);
    						/* find which content is active, grab their index*/
    						var theclass = $('.slide-content:not(.hidden)', slides).attr('class').split(' ');
    						var y = parseInt(theclass[1].slice('6'));
    						if(y==1){
    							$('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+totalslides).fadeIn().removeClass('hidden');
    							$('.slide-nav li a', slides).removeClass('selected').eq(totalslides).addClass('selected');
    						} else {
    							$('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+(y-1)).fadeIn().removeClass('hidden');
    							$('.slide-nav li a', slides).removeClass('selected').eq(y-1).addClass('selected');
    						}					
    						return false;
    					});
    				break;
    				case 'next':
    					$(this).click(function(){
    						/* cancel out the show */
    						clearTimeout(gallerytimeout);
    						/* find which content is active, grab their index*/
    						var theclass = $('.slide-content:not(.hidden)', slides).attr('class').split(' ');
    						var x = parseInt(theclass[1].slice('6'));
    						if(x==totalslides){
    							$('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+1).fadeIn().removeClass('hidden');
    							$('.slide-nav li a', slides).removeClass('selected').eq(1).addClass('selected');
    						} else {
    							$('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+(x+1)).fadeIn().removeClass('hidden');
    							$('.slide-nav li a', slides).removeClass('selected').eq(x+1).addClass('selected');
    						}						
    						return false;
    					});
    					
    				break;
    			}; // end of cases
    				
    		} else { // if it is slide index, do the following:								
    			$(this).addClass('slide-'+index);
    			$(this).click(function(){
//                                 alert("masuk");	
    				clearTimeout(gallerytimeout);
    				$(this).addClass('selected').parent().siblings().children().removeClass('selected');
    				$('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+index).fadeIn().removeClass('hidden');				
    				return false;
    			});
    		};																		
    	}); // end of navigation
	} else { 
    	$('.slide-nav li a:gt('+showthumb+'):not(.next)', slides).hide();
    	/* Navigation */
    	var z = 0;
    	var floor;
    	var ceiling;
    	$('.slide-na li a', this).each(function(index){
        	$(this).parent().addClass('index-'+index);
    		/* exclude previous and next button from content index */
    		if($(this).hasClass('prev') || $(this).hasClass('next')) {
    			/* in case it is previous or next: */											
    			switch($(this).attr('class')){
    				case 'prev':
                                        
    					$(this).click(function(){
    						if(z==0){
        						z = Math.floor(totalslides / showthumb);       						
    						} else {
        						z = --z;
        						
    						}			    						    						
    						floor = z*showthumb;
    						ceiling = floor+showthumb+1;
    						$('.slide-nav li a:not(.next, .prev)', slides).hide();
    						$('.slide-nav li a', slides).filter(':lt('+ceiling+'):gt('+floor+')').show();
    						return false;
    					});
    				break;
    				case 'next':
                                        
    					$(this).click(function(){
    						if(z==Math.floor(totalslides / showthumb)){
        						z = 0;       						
    						} else {
        						z = ++z;
        						
    						}			    						    						
    						floor = z*showthumb;
    						ceiling = floor+showthumb+1;
    						$('.slide-nav li a:not(.next, .prev)', slides).hide();
    						$('.slide-nav li a', slides).filter(':lt('+ceiling+'):gt('+floor+')').show();
    						return false;
    					});
                                       
    				break;
    			}; // end of cases
    				
    		} else { // if it is slide index, do the following:									
    			$(this).addClass('slide-'+index);
    			$(this).click(function(){
                                //alert(index);	//ini dia aksi click
    				clearTimeout(gallerytimeout);
    				$(this).addClass('selected').parent().siblings().children().removeClass('selected');
    				$('.slide-content', slides).hide().addClass('hidden').filter('.slide-'+(index+1)).fadeIn().removeClass('hidden');	
    				return false;
    			});
    		};
																		
    	}); // end of navigation

	} // end showthumb 

};
/* Video Tab */
jQuery.fn.videoTab = function(){
	
	var defaults = {  
	   divWrapper: '.slide-thumb ul li', 
	   divContent: '.slide-content' 
	};
	var options = $.extend(defaults, options);	
	
    var div = $(options.divWrapper, this);
    var content = $(options.divContent, this);        	
	    
    /* Clicking */
    div.each(function(index){
        $(this).click(function(){
            console.log('Slide ke '+index);
    		$('a', div).removeClass('selected').eq(index).addClass('selected');
    		content.hide().addClass('hidden').eq(index).removeClass('hidden').fadeIn();
            return false;
        });
    });  
};

/* Initiate */
jQuery(document).ready(function(){	
	$('#menu').dropDownMenu();
	$('.highlightslides').slideShows();
	$('.indexgalleryslides').slideShows();
	$('.foodblogger').slideShows();
	$('.contentgalleryslides').gallerySlide({showthumb:30}); 
	$('.galleryslide').gallerySlide();	
	$('.tvslide').videoTab();
	$('.roundabout').roundabout({
    	tilt: -2,
    	btnNext: '.roundaboutnext',
    	btnPrev: '.roundaboutprevious',
    	debug: false,
    	clickToFocus: false 
	});
	/* Equal Column Height */
	function equal_article(){
    	var max_height = 0;	
    	if ($('#article').height() > max_height) { max_height = $('#article').height(); }
        if ($('#sidebar').height() > max_height) { max_height = $('#sidebar').height(); }    	
    	if($.support.cssFloat) {
        	$("#article").css({'min-height':max_height});
        	$("#sidebar").css({'min-height':max_height});
    	} else {
        	$("#article").height(max_height);
        	$("#sidebar").height(max_height);
    	};
	};
	equal_article();
	/* Equal Column Height */	
	function equal_recommended(){
    	var max_height = 0;
    	$(".recommended .content").each(function(){
        	   if ($(this).height() > max_height) { max_height = $(this).height(); }
        	});    	
        	if($.support.cssFloat) {
            	$(".recommended .content").css({'min-height':max_height});
        	} else {
            	$(".recommended .content").height(max_height);
        	}
    	};
	equal_recommended();
	/* Equal Column Height */	
	function equal_restorecipe(){
    	var max_height = 0;
    	$(".restorecipesearchresult .databaselist").each(function(){
        	   if ($(this).height() > max_height) { max_height = $(this).height(); }
        	});    	
        	if($.support.cssFloat) {
            	$(".restorecipesearchresult .databaselist").css({'min-height':max_height});
        	} else {
            	$(".restorecipesearchresult .databaselist").height(max_height);
        	}        	
    	};
	equal_restorecipe();
	/* Equal Column Height */	
	function equal_indexboxes(){
    	var max_height = 0;
    	if ($('.c310 .restorecipebox').height() > max_height) { max_height = $('.c310 .restorecipebox').height(); }
        if ($('.c310 .halalbox').height() > max_height) { max_height = $('.c310 .halalbox').height(); }
    	if($.support.cssFloat) {
            $(".c310 .restorecipebox").css({'min-height':max_height});
        	$(".c310 .halalbox").css({'min-height':max_height});
    	} else {
        	$(".c310 .restorecipebox").height(max_height);
        	$(".c310 .halalbox").height(max_height);
    	};    	    	
	};
	equal_indexboxes();
	/* Equal Column Height */	
	function equal_browserboxes(){
    	var max_height = 0;
    	$(".articlebrowser li").each(function(){
        	   if ($(this).height() > max_height) { max_height = $(this).height(); }
    	});    	
    	if($.support.cssFloat) {
        	$(".articlebrowser li").css({'min-height':max_height});
    	} else {
        	$(".articlebrowser li").height(max_height);
    	}        	
	};
	equal_browserboxes();
});