/**
 * @author ahmet onol
 * @copyright 2009 - ICON Perception Managemet Co.
 * @company ICON Perception Managemet Co.
 * @email contact@icon-pm.com
 * @web http://www.icon-pm.com
 * @project Girsan
 * @file 
 * @version 
 * @created 2009.12.24 17:5
 * @modified 2009.12.24
 * @modifier Bilal Cinarli
**/

$(function(){
    
    resizeContent();
    
       $("#slider").ntTabsAndSlides({ pages : '.pages', navigation : '.slide-nav', transition: 'fade', autoSlide: true, transitionSpeed : '1000' }); 
       $("#news-box").ntTabsAndSlides({ pages : '.news-navigation', navigation : '#pagination', transition: 'fade', autoSlide: false,transitionSpeed : "fast" }); 
       
       $("h2.title").fadeIn();
    
      //Cufon
      Cufon("#nav li a", { 
        hover:true
      });     
      Cufon("#products-toggle a,#model-view #model-types li a span.text", { hover:true });      
      Cufon('h1.title, h2.title, .home-title, .home-title-ticker, #model-view #model-price', {textShadow: '1px 1px 2px #000000'});
      
      
      $("#q").focus(function(){
        if($.browser.msie)
            {
                $("#search label").hide();
            }
            else
            {
                $("#search label").fadeOut("fast");
            }
           $("#search label").fadeOut("fast");
           $(this).css('background-position','center bottom');         
      });
      
      $("#q").blur(function(){
        
        if ($(this).val() == "")
            {
                if($.browser.msie)
                    {
                        $("#search label").show();
                    }
                    else
                    {
                        $("#search label").fadeIn("fast");
                    }                

              $(this).css('background-position','center top');    
               
            }
        
      }); 
    $("a[rel=\'gallery\']").colorbox({ transition:"fade" });      
    
       /* $('#logo img').ifixpng();
        $('.top').ifixpng();
        $('.bottom').ifixpng();
        //$('a.round').ifixpng();
        //$('#products img').ifixpng();
        $('#model-view img').ifixpng();
        $('h2.title').ifixpng();
        $('#cboxPhoto').ifixpng();
        $('#cboxPrevious').ifixpng();
        $('#cboxNext').ifixpng();
        $('#cboxClose').ifixpng();  */

     
     // Open Model Box
     
     $("#togglelist a").click(function(){        

        $("#togglelist a").toggle("normal");                             
        $(".product-list").slideToggle("slow").queue(function(){
            $(".pbottom").slideToggle(25);           
            $(this).dequeue();
        });
     })                  
     
     jQuery('#mycarousel').jcarousel({

        
     }); 
     
     
     // Ajax Page Load
        
     $("#model-types a").live('click', function(){
        
        
            var url = $(this).attr("href");
            var themepath = $("#themepath").text();
            var title = $("h1.title").text();             
            $("#loading").show();
           if($.browser.msie){
             
             $("#model-image").hide();
             $("#model-price").hide();
             $("#model-name").hide();              
             
           }else{
              
             $("#model-image").fadeIn("fast");
             $("#model-price").fadeIn("fast");
             $("#model-name").fadeIn("fast"); 
           }      
           
           $("#fakediv").load(url + " #model-view", function(){
                Cufon.refresh(); 
                $("a[rel=\'gallery\']").colorbox({ transition:"fade" });                                                                   
           });        
          
         
           if($.browser.msie){
             
             $("#model-image").show();
             $("#model-price").show();
             $("#model-name").show();              
             
           }else{
              
             $("#model-image").fadeOut("fast");
             $("#model-price").fadeOut("fast");
             $("#model-name").fadeOut("fast");
           }             
                $("#loading").hide();
               document.title = title;
               //window.location = '#' + $(this).attr("id");                 
                                         
                   
         
     })
     
     
     // Contact Form
     
     if ($("input.inputbox").val() == "")
     {
        $(this).parent().parent().find("label.inputlabel").hide();
     }
     
     $("#contact .inputbox").focus(function(){
        
        $(this).parent().parent().find("label.inputlabel").hide();
        
     });
     
     $("#contact .inputlabel").click(function(){
        
        $(this).hide();
         $(this).parent().find("input.inputbox").focus();
        
     });     
     
     $("#contact .inputbox").blur(function(){
          if($(this).val() == ''){
            $(this).parent().parent().find("label.inputlabel").show(); 
          }
        
     })                
});

$(window).resize(function(){
    resizeContent();
})

function resizeContent(){
    var viewport = $(document).height();

    var cHeightHome = viewport - ($(".content-top").height() + 179  + 54 ) + 'px';
    var cHeightPage = viewport - ($(".content-top").height() + 179 + 69) + 'px';
    var cHeightModel = viewport - ($(".content-top").height() + 179 + 67) + 'px';
    $("#home #content").css('height', cHeightHome);
    $("#page #content").css('height', cHeightPage);
    $("#model #content").css('height', cHeightModel);

}

(function($){
    $.fn.ntTabsAndSlides = function(options){
        var opts = $.extend({}, $.fn.ntTabsAndSlides.defaults, options);
        
        return this.each(function(){
            var $container = $(this);
			var o = opts;
			
            var pages = o.pages;
            var $navigation = $container.find(o.navigation);            
            var $pageNavigations = $navigation.find('a');
            var currentClass = o.currentClass;
            var transition = o.transition;
            var transitionSpeed = o.transitionSpeed;
            var pagination = $pageNavigations.length;            
            var lastPage = pagination - 3; // if pagination has next & previous buttons
            
            var autoSlide = o.autoSlide;
            var slideShowDelay = o.slideShowDelay;
            
            var thePage, timer;
            
            $container.find(pages).each(function(){
               $(this).hide();
            });
            
            $container.find(pages + ':first').show().addClass(currentClass);
            
            // enables tab key navigation
            $pageNavigations.click(function(){
                // if previous button clicked
                if($(this).hasClass('pre') || $(this).parent().hasClass('pre')){
                    pre = $navigation.find('.'+ currentClass).prev();
                    
                    var p = $navigation.find('li').index(pre);
                    if(p == -1) { pre = $navigation.find('li:eq(' + lastPage + ')'); }                    
                    $navigation.find('.'+ currentClass).removeClass(currentClass);
                    pre.addClass(currentClass);
                    
                    thePage = $(pre.find('a').attr('href'));
                }
                
                // if next button clicked
                else if($(this).hasClass('next') || $(this).parent().hasClass('next')){
                    next = $navigation.find('.'+ currentClass).next();
                    var n = $navigation.find('li').index(next);
                    if(n == pagination - 1 || n == -1) { next = $navigation.find('li:eq(0)'); }
                    $navigation.find('.'+ currentClass).removeClass(currentClass);
                    next.addClass(currentClass);
                    
                    thePage = $(next.find('a').attr('href'));
                }
                
                // or other page buttons clicked
                else {
                    if(!$(this).parent().hasClass(currentClass)){
                        $navigation.find('.'+ currentClass).removeClass(currentClass);
                        $(this).parent().addClass(currentClass);
                    }
                    
                    thePage = $($(this).attr('href'));
                }
                    
                animatePage(thePage);
                
                return false;
            });
            
            if(autoSlide == true){
                startSlideShow();
                
                $(pages).hover(
                    function(){
                        stopSlideShow();
                    },
                    function(){
                        startSlideShow();
                    }
                );
            }
            
            function slideshow(){                
                var current = $navigation.find('.' + currentClass);
                var next = current.next();
                var n = $navigation.find('li').index(next);
                
                if(n == -1) { next = $navigation.find('li:eq(0)'); }
                
                if(next.hasClass('pre') || next.hasClass('next')){
                    next = $navigation.find('li:eq(1)');
                }                
                
                $(current).removeClass(currentClass);
                next.addClass(currentClass);
                    
                thePage = $(next.find('a').attr('href'));
                
                animatePage(thePage);                                            
            }
            
            function stopSlideShow(){                                    
                clearInterval(timer);
            } 
            
            function startSlideShow(){
                timer = setInterval(slideshow, slideShowDelay); 
            }             
            
            function animatePage(page){
                currentPage = $container.find(pages + '.' + '.'+ currentClass);
                nextPage = page;
                
                if(!nextPage.hasClass(currentClass)){
                    if(transition == 'fade'){
                        currentPage.fadeOut(transitionSpeed).queue(function(){
                            nextPage.fadeIn(transitionSpeed);
                            $(this).dequeue();
                        });
                        
                    }
                    
                    else if(transition == 'vslide'){
                        currentPage.slideUp(transitionSpeed);
                        nextPage.slideDown(transitionSpeed);
                    }
                    
                    // to work horizontal slide, pages should be wrapped with a overflow hidden layer having same width with the pages
                    else if(transition == 'hslide'){
                        var c = $(pages).index(currentPage);
                        var n = $(pages).index(nextPage);
                        
                        var containerW = currentPage.parent().width();
                        
                        var cM, nM;
                        
                        //alert(n + ', ' + c);
                        
                        // slide from right to left
                        if(n > c){
                            cM = -containerW;
                            nM = containerW;
                        }
                        
                        // slide from left to right
                        else{
                           cM = containerW;
                           nM = -containerW; 
                        }
                        
                        currentPage.animate({ left: cM }, transitionSpeed);
                        nextPage.css({ left: nM }).show().animate({ left: 0 }, transitionSpeed).queue(function(){
                            currentPage.hide().css({ left: 0 })
                            $(this).dequeue();
                        });
                    }
                    
                    else{
                        currentPage.hide();
                        nextPage.show();
                    }
                    
                    currentPage.removeClass(currentClass);
                    nextPage.addClass(currentClass);
                    
                    Cufon.refresh();
                }
            }                     
        });
    },
    
   	$.fn.ntTabsAndSlides.defaults = {
		pages: '.pages',
        navigation: '.tab-nav',
        currentClass: 'current',
        transition: 'fade', // fade|hslide|vslide|none
        transitionSpeed: 'slow', // slow|fast|1000|5000 etc.
        autoSlide: false,
        slideShowDelay: 8000
	};
})(jQuery);

