/* fx.js
 * Created by Dawid Zarrach
 
  fx:     'fade',
    speed:   400,
    timeout: 4000,
    pause:   1

 
 ****************************************/

var $jq = jQuery.noConflict();

function log(t)
{
        console.log(t);
}

$jq(document).ready(function($){
        
        if($('.slides ul').length > 0) {
                $(".slides ul").cycle({
                        fx: "fade",
                        speed:   400,
    					timeout: 4000,
   						pause:   1


                });                
        }
        
        if($('.line-edit').length > 0) {
                $(".line-edit").toggleVal({
                        focusClass: "text-focus",
                        changedClass: "text-changed"
                });
        }
        
        $('#top #aw .sidebar ul li img').each(function(){
                $(this).css({
                        'opacity': '0.8'
                });
        });
        
        $('#top #aw .sidebar ul a').each(function(){
                $(this).hover(function(){
                        $(this).parent().find('img').fadeTo(300, 1);
                }, function(){
                        $(this).parent().find('img').fadeTo(300, 0.8);
                });
        });
        
        $("#top .accordion").accordion({
                active: 'true',
                header: 'a.head',
                animated: false,
                selectedClass: 'current'
        });
        $("#top .accordion").accordion("activate" , ".current") ;
        $("#top .accordion").accordion({animated: true});
        
        function setPhoto(href) {
                var preloader = new Image() ;
                var img = $("#slideshow .slides img");
                var container = $("#slideshow .slides");
                preloader.onload = function() {
                        preloader.onload = null;
                        var newImg = $('<img />');
                        $(newImg).hide();
                        $(newImg).css({
                                "width": "680px",
                                "height": "440px"
                        });
                        $(newImg).attr("src", href);
                        $(newImg).appendTo(container);
                        $(newImg).fadeIn("slow", function() {
                                if($(img).length > 0) {
                                        $(img).remove();
                                }
                        });
                }
                preloader.src = href;
        }
        
        var currentImage = 0;
        var pause = false;
        
        function slideshow() {
                if($("#jcarousel li").length >= 2) {
                        setTimeout(function(){
                                if(!pause) {
                                        if(currentImage++ >= $("#jcarousel li").length-1) {
                                                currentImage = 0;
                                        }
                                        var items = $("#jcarousel li");
                                        var a = $(items[currentImage]).find("a");
                                        $(".jcarousel-skin-tango .jcarousel-item a").removeClass("current").css("opacity", "0.7");
                                        a.addClass("current").css("opacity", "1");
                                        setPhoto(a.attr("href"));
                                        slideshow();
                                }
                        }, 5000);
                }                
        }
        
        function pauseSlideshow() {
                if(pause) return;
                pause = true;
                setTimeout(function(){
                        pause = false;
                        slideshow();
                }, 10000);
        }
        
        $("#jcarousel img").each(function(){
                $(this).parent().parent().width($(this).width());
        });
        
        if($("#simple-jcarousel").length > 0) {
                setPhoto($("#simple-jcarousel li:first-child a").attr("href"));
        }
        
        $("#jcarousel").jcarousel({
                initCallback: function(carousel, state) {
                        $(".jcarousel-skin-tango .jcarousel-item a").each(function(){
                                $(this).css("opacity", "0.7");
                                $(this).hover(function() {
                                        if(!$(this).hasClass("current")) {
                                                $(this).css("opacity", "1");
                                        }
                                }, function(){
                                        if(!$(this).hasClass("current")) {
                                                $(this).css("opacity", "0.7");
                                        }                                
                                });
                                $(this).click(function(){
                                        $(".jcarousel-skin-tango .jcarousel-item a").removeClass("current").css("opacity", "0.7");
                                        $(this).addClass("current").css("opacity", "1");
                                        pauseSlideshow();
                                        currentImage = $(this).parent().index();                                        
                                        setPhoto(this.href);
                                        return false;
                                });
                        });
                        setPhoto($("#jcarousel li:first-child a").attr("href"));
                        $("#jcarousel li:first-child a").addClass("current").css("opacity", "1");
                        slideshow();
                }
        });
});
