/****************************************
** Rotator plugin for VOV Woon website **
** Version: 0.1S                       **
****************************** heap.nl */

(function($){  
    $.fn.extend({
        rotator: function(options) {       
            var defaults = {
                speed: 1000
            } 
            var elements = new Array();
            var options = $.extend(defaults, options); 
            var objects = new Array();
            var curMenu = 0; var curTarget;
            return this.each(function() { 
                // Initializing objects
                var obj = $(this);
                obj.attr("rotatorSerial", objects.length);
				obj.attr("target", "#text_container" + (objects.length + 1));
                objects.push(obj);                
                var target = obj.attr("target");
                if (objects.length != 1) {
                   $(target).hide();
				   //console.log(target);
				}
                else
                   curTarget = target;
                // Set event handler
                $(obj).bind("click", function() {
											  $('#stippels').css('background-image','url(images/ani_stop.gif)');
					$("#bg").fadeOut(500);					 
					$("#accordion2").accordion('activate',false);
					$("#accordion3").accordion('activate',false);
					$("#accordion4").accordion('activate',false);
					$("#accordion5").accordion('activate',false);
                    curMenu = parseInt( $(this).attr("rotatorSerial") );
                    oldTarget = curTarget;
                    curTarget = $(this).attr("target");
					
                    jQuery.each(objects, function(key, value) {
                        // Animate content
                        var target = value.attr("target");
						
                        if (target == oldTarget) {
								
								$(curTarget).css('height','36px');
								$(curTarget).css('width','204px');
								$(curTarget).hide();
								$(target).animate({height:"36px"},500).animate({width:"204px"},500, function(){
												$(target).hide();	
												$('#stippels').css('background-image','url(images/ani.gif)').animate({height: '600px'}, 2100, function(){
														jQuery.each(objects, function(k, v) {		
															var rotatorPos = parseInt( (this).attr("rotatorSerial") - curMenu + 1);
                        									if (rotatorPos < 1)
                        										rotatorPos = rotatorPos + 5;
                        									v.addClass("rotator_pos" + rotatorPos);
														});
													if (curTarget=='#text_container1') 	$("#bg").html('<img src=\"images/background/achtergrond.gif\" width=\"100%\" height=\"100%\" />').fadeIn(1500);	
													if (curTarget=='#text_container2') 	$("#bg").html('<img src=\"images/background/beweging.jpg\" width=\"100%\" height=\"100%\" />').fadeIn(1500);
													if (curTarget=='#text_container3') 	$("#bg").html('<img src=\"images/background/nieuwbouw.jpg\" width=\"100%\" height=\"100%\" />').fadeIn(1500);
													if (curTarget=='#text_container4') 	$("#bg").html('<img src=\"images/background/blokkies.jpg\" width=\"100%\" height=\"100%\" />').fadeIn(1500);
													if (curTarget=='#text_container5') 	$("#bg").html('<img src=\"images/background/stap.jpg\" width=\"100%\" height=\"100%\" />').fadeIn(1500);
													
													$(curTarget).show().animate({opacity: 1.0}, 1000).animate({width:"636px"},500).animate({height:"540px"},2000);
													$('#stippels').css('background-image','url(images/ani_stop.gif)')
																																					
												});
								});
                                //$(target).slideUp(options.speed, function(){ $(curTarget).animate({opacity: 1.0}, 3000).slideDown(options.speed); });
								//$(target).slideUp(options.speed, function(){ 
											 
								//			 $(curTarget).animate({height:"36px"},500).animate({opacity: 1.0}, 1000).animate({height:"540px"},2000); 
								// });

                        } else if (target != curTarget) {
                                $(target).hide();
								$(target).css('height','1px');
                        }
						
                        // Jump menu items
                        if (value.hasClass("rotator_pos1") ) {
                            value.removeClass("rotator_pos1");
                        } else if (value.hasClass("rotator_pos2") ) {
                            value.removeClass("rotator_pos2");
                        } else if (value.hasClass("rotator_pos3") ) {
                            value.removeClass("rotator_pos3");
                        } else if (value.hasClass("rotator_pos4") ) {
                            value.removeClass("rotator_pos4");
                        } else if (value.hasClass("rotator_pos5") ) {
                            value.removeClass("rotator_pos5");
                        }
						
                        //var rotatorPos = parseInt( (this).attr("rotatorSerial") - curMenu + 1);
                        //if (rotatorPos < 1)
                        //    rotatorPos = rotatorPos + 5;
                        //value.addClass("rotator_pos" + rotatorPos);
						
                    });
                });
            });
        }
        ,
        rotatorTo: function() {
            return this.each(function() { 
                $(this).trigger("click");
            });
        }

    });
})(jQuery);
