/*
 * jQuery Plugins:
 *
 * - Twitter Widget
 * - Testimonial Widget
 * - Toggle
 * - Accordion
 * - Back to top
 * - Contact Form
 * - Google Map
 *
 * Copyright 2011, MTD
 * http://themeforest.net/user/MTD
 */
(function($){
  $.fn.extend({
    // --- -- - GOOGLE MAP PLUGIN - -- --- //
    kotodama_GoogleMap	: function(){
			
      if( $(this).length ){
        $.kotodama_GoogleMap_Maps	= $(this);
        $.getScript("http://maps.google.com/maps/api/js?sensor=false&callback=jQuery.fn.kotodama_GoogleMap_CallBack&async=2");
      }
    },
    
    // --- -- - TOGGLE PLUGIN - -- --- //
		kotodama_Toggle	: function(){
			
			return this.each(function(){
			
				var obj			= {
						panel	: $(this),
						title	: $(this).find(".toggleTitle"),
						content	: $(this).find(".toggleContent"),
						isOpen	: $(this).data("open")
					}
				
				// Setting a fixed width prevent the jumpy bug
				obj.content.css({width	: obj.content.width()});
				
				// Close panel by default
				if( !obj.isOpen ){
					obj.content.hide(0, function() {
                      $(this).parent().hide(0, function() {$(this).show(0)});
                    });
					obj.panel.addClass("closed");
				}
                
				// Add click event
				obj.title.click(function(){
                  var $parent = $(this).parent('div');
                  var $content = $('.toggleContent', $parent);
                  if( $parent.hasClass('closed') ) {
                    $parent.removeClass("closed");
                    $parent.addClass("open");
                    $content.show(300, function() {
                      $(this).parent().hide(0, function() {$(this).show(0)});
                    });
                  }
                  else {
                    $parent.removeClass("open");
                    $parent.addClass("closed");
                    $content.hide(300, function() {
                      $(this).parent().hide(0, function() {$(this).show(0)});
                    });
                  }
				});
						
			});
	
		},
    kotodama_TestimonialWidget	: function(){
		
      function showNext(el, utils){
        utils.isReady		= false;
        el.nextTestimonial	= el.testimonialList.find('li').eq( utils.nextIndex );

        // Hide current Testimonial
        el.currentTestimonial.fadeOut( 200, function(){
          $(this).removeClass("current");
          // Show next Testimonial
          el.nextTestimonial.fadeIn( 200, function(){
            $(this).addClass("current");
            utils.currentIndex		= utils.nextIndex;
            el.currentTestimonial	= el.nextTestimonial;
            utils.callback();
            utils.isReady	= true;
          });
          el.testimonialAuthor.html( el.nextTestimonial.find("cite").html() );
        });
      }

      return this.each(function(){

        var el		= {
          widget				: $(this),
          testimonialList		: $(this).find(".testimonialList"),
          arrowLeft			: $(this).find(".arrowLeft"),
          arrowRight			: $(this).find(".arrowRight"),
          testimonialAuthor	: $(this).find(".testimonialAuthor")
        },
        utils	= {
          testimonialNum	: el.testimonialList.find('li').length,
          currentIndex	: 0,
          callback		: function(){},
          isReady			: false,
          width			: 0,
          height			: 0,
          outerHeight		: 0
        }
        el.currentTestimonial	= el.testimonialList.find('li').eq(0);				

        // Calculate the max height
        el.testimonialList.find("li").each(function(){
          $(this).css({
            opacity	: 0,
            display	: "block"
          });
          if( $(this).outerHeight(true) > utils.outerHeight ){
            utils.outerHeight = $(this).outerHeight(true);
            utils.height = $(this).height();
          }
          $(this).css({
            opacity	: 1,
            display	: "none"
          });
        });

        // Set the new height
        el.testimonialList.find("li").each(function(){
          $(this).css({
            height	: utils.height
          });
        });
        el.testimonialList.css({
          height	: utils.outerHeight
        });

        // Set the first Testimonial as current
        el.currentTestimonial.fadeIn(300, function(){
          $(this).addClass("current");
        });
        el.testimonialAuthor.html(
          el.currentTestimonial.find("cite").html()
          );

        // Check for inactive arrows
        el.arrowLeft.addClass("inactive");
        if( utils.testimonialNum === 1 ){
          el.arrowRight.addClass("inactive");
        }

        // Add click event on the LEFT arrow
        el.arrowLeft.click(function(){
          if( !$(this).hasClass("inactive") && utils.isReady ){
            utils.nextIndex	= utils.currentIndex - 1;
            if( utils.nextIndex === 0 ){
              utils.callback	= function(){
                el.arrowLeft.addClass("inactive");
              };
            }else{
              utils.callback	= function(){
                el.arrowRight.removeClass("inactive");
              };
            }
            showNext(el, utils);
          }
          return false;
        });

        // Add click event on the RIGHT arrow
        el.arrowRight.click(function(){
          if( !$(this).hasClass("inactive") && utils.isReady ){
            utils.nextIndex	= utils.currentIndex + 1;
            if( utils.nextIndex === utils.testimonialNum -1 ){
              utils.callback	= function(){
                el.arrowRight.addClass("inactive");
              };
            }else{
              utils.callback	= function(){
                el.arrowLeft.removeClass("inactive");
              };
            }
            showNext(el, utils);
          }
          return false;
        });

        utils.isReady = true;

      });

    },
kotodama_Accordion	: function(){
		
			function open(obj, index){
			
				if( index === obj.openIndex ){
					obj.panels.eq( index ).find(".accordionContent").slideUp(300).end().addClass("closed");
					obj.openIndex = false;
				}else if( obj.openIndex === false ){
					obj.panels.eq( index ).find(".accordionContent").slideDown(300).end().removeClass("closed");
					obj.openIndex = index;
				}else{
					obj.panels.eq( index ).find(".accordionContent").slideDown(300).end().removeClass("closed");
					obj.panels.eq( obj.openIndex ).find(".accordionContent").slideUp(300).end().addClass("closed");
					obj.openIndex = index;
				}
			
			}
			
			return this.each(function(){
			
				var obj			= {
						list		: $(this),
						panels		: $(this).children("li"),
						openIndex	: parseInt( $(this).data("open") )
					}
					obj.openIndex	= ( obj.openIndex ) ? obj.openIndex-1 : false;
				
				// Close all the panels and add Click events
				obj.panels.each(function(e){
					var panel	= $(this),
						content	= panel.find(".accordionContent");
					
					// Setting a fixed width prevent the jumpy bug
					content.css({width	: content.width()});
					
					if( e !== obj.openIndex ){
						content.hide(0, function() {
                       $(this).parent().hide(0, function() {$(this).show(0)});
                      });
						panel.addClass("closed");
					}
					// Add Click event
					panel.find(".accordionTitle").click(function(){
                      var $parent = $(this).parent('li');
                      var $content = $('.accordionContent', $parent);
                      if( $parent.hasClass('closed') ) {
                        $parent.removeClass("closed");
                        $parent.addClass("open");
                        $content.show(300, function() {
                          $(this).parent().hide(0, function() {$(this).show(0)});
                        });
                      }
                      else {
                        $parent.removeClass("open");
                        $parent.addClass("closed");
                        $content.hide(300, function() {
                          $(this).parent().hide(0, function() {$(this).show(0)});
                        });
                      }
						return false;
					});
				});
						
			});
	
		},    
    kotodama_GoogleMap_CallBack	: function(){
		
      $.kotodama_GoogleMap_Maps.each(function(){
		
        var map			= $(this),
        details		= $(this).find(".mapDetails").html(),
        latitude	= map.data("latitude"),
        longitude	= map.data("longitude"),
        coord		= new google.maps.LatLng( latitude, longitude ),
        param		= {
          zoom				: map.data("zoom") || 6,
          zoomControlOptions	: {
            style: google.maps.ZoomControlStyle.SMALL
          },
          panControl			: false,
          streetViewControl	: false,
          center				: coord,
          html				: map.data("title"),
          popup				: true,
          mapTypeId			: google.maps.MapTypeId.ROADMAP
        },
        gmap		= new google.maps.Map( map.get(0), param ),
        bubble		= new google.maps.InfoWindow({
          content		: details,
          maxWidth	: 400
        }),
        icon		= new google.maps.MarkerImage("/gfx/map-marker.png",
          new google.maps.Size(130,127),
          new google.maps.Point(0,0),
          new google.maps.Point(65,120)
          ),
        marker		= new google.maps.Marker({
          position	: coord,
          map			: gmap,
          icon		: icon,
          flat		: true,
          title		: map.data("title")
        });
        google.maps.event.addListener(marker, "click", function () {
          bubble.open(gmap, marker);
        });
			
      });
    }
	
  });
})(jQuery);



$(document).ready(function() {
  $(".googleMap").kotodama_GoogleMap();
  $(".testimonialWidget").kotodama_TestimonialWidget();
  // Initialize the accordion widgets
  $(".accordion").kotodama_Accordion();
  
  // Initialize the toggle widgets
  $(".toggle").kotodama_Toggle();
});

jQuery(document).ready(function($){

  mtdScript = {
		
    // Initialize functions
    initSite	: function(){
      this.topPanel();
      this.dropDownMenu();
    },
    
    // Main menu dropdown functionality
    dropDownMenu	: function(){
      /** ie bug
      $('div.leftSide ul li ul').each(function() {
        if( $(this).text().trim() == '' ) {
          $(this).remove();
        }
      });
       */
      
      $("div.menu ul").find('li').hover(function(){
        $(this).children("ul").stop(true,true).fadeIn(300);
      }, function(){
        $(this).children("ul").stop(true,true).fadeOut(200);
      });
    },    
    // Show and Hide the top panel
    topPanel		: function(){
		
      var topPanel	= $("#topPanel"),
      panelHandle	= $("#topPanelHandle"),
      height		= 0,
      wrapHeight	= 0,
      imgs		= topPanel.find("img"),
      imgsNum		= imgs.length,
      readyImg	= 0;
			
      // If there are images in the toppanel
      // load them before initializing the toggle effect
      if( imgsNum ){
        imgs.each(function(){
          $(this).load(function(){
            readyImg++;
            initTopPanel();
          });
        });
      }else{
        initTopPanel();
      }
			
      function initTopPanel(){
        if( imgsNum === readyImg ){
          height		= topPanel.height();
          var height2		= $("#topForm").height() + 14;
          topPanel.css({
            display : "block"
          });
          wrapHeight	= topPanel.find(".wrap").height();
					
          topPanel.css({
            marginTop	: -height,
            height		: height,
            display		: "block",
            overflow	: "visible"
          });
					
          topPanel.find(".wrap").css({
            height		: wrapHeight,
            display		: "block",
            overflow	: "visible"
          });
					
          // Open / Close Panel
          panelHandle.click(function(){
            if($("#topForm").css('marginTop') == '0px') {
              $("#topForm").animate({
                marginTop	: -height2
              }, 485, function(){
                $("#topFormHandle").removeClass("close");
                $('#topForm .line').hide();
              });          
            }
            if( !$(this).hasClass("close") ){
              topPanel.animate({
                marginTop	: 0
              }, 350, function(){
                panelHandle.addClass("close")
              });
            }else{
              topPanel.animate({
                marginTop	: -height
              }, 350, function(){
                panelHandle.removeClass("close")
              });
            }
          });
					
          // Open the panel by clicking on a link that point to #topPanel
          $('a[href="#topPanel"]').click(function(){
            $('html, body').animate({  
              scrollTop: 0  
            }, 300);
            panelHandle.trigger("click");
            return false;
          });
          
          
          $("#topForm").css({
            display : "block"
          });
          wrapHeight	= topPanel.find(".wrap").height();
          $("#topForm").css({
            marginTop	: -height2,
            height		: (height2 - 14),
            display		: "block",
            overflow	: "visible"
          });
          
          // Open / Close Panel
          $("#topFormHandle").click(function(){
            if($("#topPanel").css('marginTop') == '0px') {
              $("#topPanel").animate({
                marginTop	: -height
              }, 350, function(){
                $("#topPanelHandle").removeClass("close")
              });
            }
            if( !$(this).hasClass("close") ){
              $('#topForm .line').show();
              $("#topForm").animate({
                marginTop	: 0
              }, 485, function(){
                $("#topFormHandle").addClass("close");
              });
            }else{
              $("#topForm").animate({
                marginTop	: -height2
              }, 485, function(){
                $("#topFormHandle").removeClass("close");
                $('#topForm .line').hide();
              });
            }
          });
					
          // Open the panel by clicking on a link that point to #topPanel
          $('a[href="#topPanel"]').click(function(){
            $('html, body').animate({  
              scrollTop: 0  
            }, 300);
            panelHandle.trigger("click");
            return false;
          });
        }
      }
		
    }
  }
	
  $('#topPanel, #topForm').show();
    
  mtdScript.initSite();

});


$(document).ready(function() {
  
  $('div.leftSide ul li ul li:first-child').addClass('firstChild')
  
  
  $(window).bind('scroll',function() {
    var val  =$(window).scrollTop();
    $('.shareButtons').css('top', val + 'px' );
    
  });
  
  $('ul.products li:has(a)').each(function() {
    $(this).addClass('hoverable');
  });
  
  $('.new_content h2:first-child').addClass('nopadding');
  
  
  $('.thumbHolder img').hover(
    function () {
      $(this).animate({
        opacity: 0.5
      });
    },
    function () {
      $(this).animate({
        opacity: 1.0
      });
    }
    );
  
  // colorbox
  $('a[rel=colorbox]').colorbox();
  
  $('.images .one img.hover').hide();
  
  $('.images .one').hover(
    function (e) {
      if( $('img.hover', this).is(':visible') == false )
        $('img.hover', this).show("slide", {
          direction: "left"
        }, 150);
//      e.preventDefault();
    }, 
    function () {
      if( $('img.hover', this).is(':visible') == true )
        $('img.hover', this).hide("slide", {
          direction: "left"
        }, 150);
      if (($.browser.msie && $.browser.version.substr(0,1)>8 ) || $.browser.msie == false) {
        e.preventDefault();
      }
    }
    );
}
);


$(function(){
  $('#slides').slides({
    preload: true,
    preloadImage: '/gfx/slides/loading.gif',
    play: 5000,
    pause: 2500,
    hoverPause: true,
    animationStart: function(current){
      $('.caption').animate({
        bottom:-35
      },100);
      if (window.console && console.log) {
        // example return of current slide number
      };
    },
    animationComplete: function(current){
      $('.caption').animate({
        bottom:0
      },200);
      if (window.console && console.log) {
        // example return of current slide number
      };
    },
    slidesLoaded: function() {
      $('.caption').animate({
        bottom:0
      },200);
    }
  });
});





$(document).ready(function() {
  if ($.browser.msie && $.browser.version.substr(0,1)<=8) {
    $(function() {
      $('body').addClass('ie');
      
      var zIndexNumber = 2000;
      $('div').each(function() {
          $(this).css('zIndex', zIndexNumber);
          zIndexNumber -= 10;
      });
    });
  }

    $('#news_scrollbar').tinyscrollbar();

    var flashvars = {},
    params = {wmode:"transparent"},
    lang = $('.flash_content').attr('data-cms-lang');
    attributes = {};


    swfobject.embedSWF("/flash/kupeg_" + lang + ".swf", "flash_banner", "920", "300", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
});
