
$(document).ready(function(){

    //vars  
    var conveyor = $("#sliderContent .item-container"), item = $("#sliderContent .item"), mainCont = $("#sliderContent"), sld_track = $("#sld_track"), slideDrag, LoopTime = 100, hvAni, mathWitdh;
    
    
    if (!$("#sliderContent").is('.custom')) {
        mainCont.css({
            //overflow: 'hidden',
            height: item.outerHeight(true)
        });
    }
    conveyor.css({
        position: 'absolute'
    });
    //set length of conveyor  
    if (!$("#sliderContent").is('.custom')) {
        conveyor.css("width", item.length * parseInt(item.outerWidth(true)));
    }
    else {
        parseInt(conveyor.css("width")) - parseInt($("#sliderContent").width());
    }
    
    function maxSize(){
        if (!$("#sliderContent").is('.custom')) {
            mathWitdh = ((item.length * parseInt(item.outerWidth(true))) - parseInt($("#sliderContent").width()));
        }
        else {
            mathWitdh = parseInt(conveyor.css("width")) - parseInt($("#sliderContent").width());
        }
        return mathWitdh;
    }
    
    function checkType(event){
        slideDrag = $(".ui-slider-handle").hasClass("ui-state-focus");
        //slideDrag = $(event.target).parent().hasClass("ui-slider-handle");
        console.log(slideDrag);
        return slideDrag;
    }
    //config  
    function sldOpt(){
        var sliderOpts = {
            animate: true,
            max: maxSize(),
            start: function(event, ui){
                slideDrag = $(event.target).parent().hasClass("ui-slider-handle");
                //checkType(event);
            },
            slide: function(event, ui){
                
                //console.log(slideDrag);
                if (slideDrag == true) {
                    conveyor.css("left", "-" + (ui.value) + "px");
                }
                else {
                    conveyor.stop().animate({
                        left: "-" + (ui.value) + "px"
                    }, 'slow');
                }
                
            }
            
        };
        return sliderOpts;
    }
    
    function rotateHover(){
        $('.ui-slider-handle-hoverloop').stop().animate({
            opacity: 1
        }, 'slow').animate({
            opacity: 0
        }, 'slow');
        return false;
    }
    
    function callHandleHover(){
        if (sld_track.is(':visible')) {
        
            $(".ui-slider-handle").append("<span class='ui-slider-handle-hoverloop'></span>");
            
            $(".ui-slider-handle").hover(function(){
                clearInterval(hvAni)
                hvAni = 0;
                $('.ui-slider-handle-hoverloop').stop().animate({
                    opacity: 0
                }, 'slow');
            }, function(){
                rotateHover();
                hvAni = setInterval(rotateHover, 1200);
            });
        }
        else {
            $('.ui-slider-handle-hoverloop').remove();
        }
        
        return false;
        
    }
    
    function reloadSlider(){
        mainCont = $("#sliderContent");
        item = $("#sliderContent .item");
        conveyor.css("width", item.length * parseInt(item.outerWidth(true)));
        if (conveyor.width() > mainCont.width()) {
            $("#sld_slider").slider('destroy').slider(sldOpt());
            conveyor.css("left", "-" + $("#sld_slider").slider('value') + "px");
            sld_track.stop().animate({
                opacity: 1
            }, 'slow').css({
                visibility: 'visible'
            });
            callHandleHover();
        }
        else {
            conveyor.css({
                width: conveyor.css("width", item.length * parseInt(item.outerWidth(true))),
                margin: '0 auto',
                position: 'relative',
                left: 'auto'
            });
            sld_track.stop().animate({
                opacity: 0
            }, 'slow');
            $("#sld_slider").slider('destroy');
        }
        return false;
    }
    
    
    $(window).resize(function(){
        reloadSlider();
        //console.log(sldValMax);
    });
    
    if (conveyor.width() > mainCont.width()) {
        sld_track.stop().animate({
            opacity: 1
        }, 'slow').css({
            visibility: 'visible'
        });
        $("#sld_slider").slider(sldOpt());
    }
    else {
        conveyor.css({
            width: conveyor.css("width", item.length * parseInt(item.outerWidth(true))),
            margin: '0 auto',
            position: 'relative',
            left: 'auto'
        });
    }
    
    //call start functions
    callHandleHover();
    hvAni = setInterval(rotateHover, 1200);
    
    
    
    //fancybox types
    
    $("#openFormSend2Friend").fancybox({
        'width': 580,
        'height': 780,
        'autoScale': false,
        'scrolling': 'no',
        'type': 'iframe'
    });
    
    
    $(".open_sld_cont").fancybox({
        'width': '75%',
        'height': '75%',
        'autoScale': false
    });
    $("a.mag_cat").fancybox({
        'height': 600,
        'width': 780,
        'overlayShow': true,
        'autoScale': false,
        'titleShow': false,
        'scrolling': 'no',
        'type': 'iframe'
    });
    $("a.mag_cat_small").fancybox({
        'height': 500,
        'width': 480,
        'overlayShow': true,
        'autoScale': false,
        'titleShow': false,
        'scrolling': 'no',
        'type': 'iframe'
    });
    
    
    //Tooltips Gewinnspiel
    $(".toolTip").hide();
    $(".toolTip").hover(function(){
      $(this).addClass('active');
    },function(){
      $(this).removeClass('active');
      //console.log($("#preis2").hasClass('active'), $("#preis2").hasClass('active'));
      if($(this).hasClass('active') == false){
        $(this).fadeOut('fast');
      }
      
    });
    $("#preis2").hoverIntent(function(){
      $("#toolTKettcar").fadeIn('fast');
    },function(){
      if($("#toolTKettcar").hasClass('active') == false){
        $("#toolTKettcar").fadeOut('fast');}
      });
   $("#preis3").hoverIntent(function(){
      $("#toolTSchweizer").fadeIn('fast');
    },function(){
      if($("#toolTSchweizer").hasClass('active') == false){
      $("#toolTSchweizer").fadeOut('fast');}
    });
   
    
    
    
    //sorting
    
    var data = {};
    data = {
        'filter': '',
        'sorting': ''
    };
    
    
    $("#filter").delegate("a", "click", function(){
    
        $("#filter a").removeClass('active');
        $(this).addClass('active');
        
        var filter = $(this).attr('id');
        
        data.filter = filter;
        
        $.ajax({
            async: true,
            url: gettext("URL_AJAX_VOTEFILTER"),
            context: document.body,
            data: data,
            dataType: 'html',
            type: 'post',
            
            success: function(result){
                $('#result').empty();
                $('#result').append(result);
                reloadSlider();
            }
        });
        
        return false;
    });
    
    $("#sort").delegate("a", "click", function(){
    
        $("#sort a").removeClass('active');
        $(this).addClass('active');
        
        var sorting = $(this).attr('id');
        
        data.sorting = sorting;
        
        $.ajax({
            async: true,
            url: gettext("URL_AJAX_VOTEFILTER"),
            context: document.body,
            data: data,
            dataType: 'html',
            type: 'post',
            
            success: function(result){
                $('#result').empty();
                $('#result').append(result);
                reloadSlider();
            }
        });
        
        return false;
    });
    
    
    //voting 
    
    $('#detail-vote').click(function(){
    
        var href = $(this).attr('href');
        
        $.ajax({
        
            url: href,
            dataType: 'json',
            context: document.body,
            success: function(result){
            
                if (result.error == 'already voted') {
                    $('.comment').html(gettext("24HOURVOTING"));
  
                    
                    return false;
                }
                if (result.error == 'no cookies') {
					
                    $('.comment').html(gettext("24HOURVOTING_ERROR"));
                    return false;
                }
                
                var count = result.count;
                
                if (count > 1) {
                    $('.detail-pic-votes').html(count + gettext("Stimmen"));
                }
                else {
                    $('.detail-pic-votes').html(count + gettext("Stimme"));
                }
				
				 $('#detail-vote').html("<span>&nbsp;</span>" + gettext("Stimme gezählt")).addClass('voted');
                
                if (result.showform) {
                
                    $.fancybox({
                        'href': 'http://' + window.location.host + gettext("URL_POPUP_VOTING"),
                        'width': 570,
                        'height': 670,
                        'autoScale': 'true',
                        'scrolling': 'no',
                        'type': 'iframe'
                    });
                }
                
            }
        });
        return false;
    });
    
    
    
    
    
});

function setCookie(){
    var jetzt = new Date();
    
    parent.document.cookie = "SHOWFORM=N; expires=1209600" + jetzt.getTime() + ";path=/";
}


function disableForm(){
	
    if ($('#accept').attr('checked') == 'checked') {
		parent.setCookie();
	}
    parent.$.fancybox.close();
}


function gettext(string) {
  return LOCALE[string] ? LOCALE[string] : string;
}


