$(function(){
    // scroller
    if($(".scroller").length)
        $(".scroller").simplyScroll({
            speed: 10,
            buttons: null
        });

    // Drop Down Menu
    $('.sf-menu').superfish({
        dropShadows : false,
        speed : 'fast',
        pathClass:  'current'
    });


    if($("#homepage-top .slideBanner").length)
     $('#homepage-top .slideBanner').advListRotator({
        effect: 'slide',
        effectOptions: {slideBy: 1000},
        helper: '#homepage-top .slideBannerHelper',
        helperActiveItemClass: 'slideBannerHelperItemActive',
        helperInteraction: 'click'
    });

    if($("#homepage-left .slideBanner").length)
     $('#homepage-left .slideBanner').advListRotator({
        effect: 'slide',
        effectOptions: {slideBy: 592},
        helper: '#homepage-left .slideBannerHelper',
        helperActiveItemClass: 'slideBannerHelperItemActive',
        helperInteraction: 'click'
    });



    // search form
    var default_search_value = 'SEARCH FOR';
    var search_field = $('#search-form #simple-search');
    if(search_field.val() == '') search_field.val(default_search_value);
    search_field.bind('click focus', function(){
        if($(this).val() == default_search_value) $(this).val('');
    });
    search_field.focusout(function(){
        if($(this).val() == '') $(this).val(default_search_value);
    });

    // Checkbox, Radiobutton replacement
    if($(".checkbox").length) $(".checkbox").dgStyle();
    if($(".checkbox-small").length) $(".checkbox-small").dgStyle();
    

    // Selectbox replacement
//    if($("select").length) $('select').not('.not-replace').selectbox();

    // DatePicker
    if($(".datepicker").length) $('.datepicker').datepick({dateFormat: 'yyyy-mm-dd', yearRange:'c-90:c-12'});

    // make active menu item
    var document_href = document.location.href.replace('http://'+document.domain,'');
    // fix slash at the end of the url
    if(document_href[document_href.length-1] == '/') document_href = document_href.substring(0, document_href.length-1);
    
    $('A').each(function(){
        $(this).removeClass('active');
        if($(this).attr('href') == document_href) $(this).addClass('active');
    });
    
    $('#pages-shortcut-bar').find('li').each(function(){
        
        $(this).css("display", "block");
        
        if($(this).hasClass(document_href.split("/")[2])){
            $(this).css("display", "none");
        }
                        
    });
    
    $(':input[value=Clear]').click(function(){$(this).clear_form_elements()});

    // modal for product details
    //$('#ex2').jqm({ajax:'@href', trigger: 'A:contains("Play Video")', overlay: 1});
    $('A:contains("Play Video")').click(function(){
        var sthis = $(this);
        sthis.nextAll('.play-video-div:first').remove();
        var url = $(this).attr('href');
        sthis.after($('<br/><div class="play-video-div"></div>'));
            $.ajax({url:url, 
                    type: 'GET',
                    success:function(data) {
                        sthis.nextAll('.play-video-div:first').html(data);
                        sthis.fadeOut('fast');
                    },
                    beforeSend: function(){}
            });
        return false;
    });

    
    /*//////////////////////   MODAL    ///////////////////////*/
    flashMessage();

    // load pages content
    var myLoad= function(hash){
        $('#content .loader').remove();
        $(hash.w).prepend($('<a class="jqmClose">x</a><div class="loader"></div>'));
        $('.jqmClose', $(hash.w)).click(function(){ 
            //$(hash.w).jqmHide(); $('.jqmOverlay').remove()
            hash.w.hide(); 
            //$('.jqmOverlay').remove()
        });  
        hash.w.show('slow', function(){ $('.loader', $(hash.w)).fadeOut() }); 
    }; 
    var myClose = function(hash){
        $('#jqmWindow').html('');
        $('#jqmWindow').hide();
        $('.jqmOverlay').remove()
        $('.jqmWindow').hide();
    }
    $('#jqmWindow').jqm({onLoad:myLoad, onHide:myClose, ajax:'@href', trigger: 'a.jqModal', overlay: 18});

    // load single big images
    //$('#ex2zoom').jqm({overlay: 18}); 
/*
    $('.zoom').click(function(){
        $('#ex2zoom').jqmShow(); 
        $('#ex2zoom').html('<br/><img src="'+$(this).attr('href')+'"/>').prepend($('<a class="jqmClose">x</a>'));
        $('#ex2zoom .jqmClose').click(function(){ $('#ex2zoom').jqmHide(); });  
        return false;
    });
*/
    $("a[rel^='zoom']").prettyPhoto({opacity: 0.20, social_tools: false, gallery_markup:'', allow_resize: false, show_title: false});



    if(document.location.hash == '#search-advanced') show_advanced_search();
    $('#search-advanced-link').click(function(e){
            e.stopPropagation(); 
            if($('#search-advanced-block').css('display') == 'none'){
                show_advanced_search();
            }
            else{
                hide_advanced_search();
            }
        }
    )
    $('#search-advanced-block .jqmClose').click(function(e){
        e.stopPropagation(); 
        hide_advanced_search();
    });


    /******************* Share buttons ********************/
    $('.share-button').click(function(){
        newwindow = window.open($(this).attr('href'),'sharer','height=300,width=450');
        if (window.focus) { newwindow.focus() }
        return false;
        
    });

    $('#latest-news-crawl').crawlLine({
        speed:2,
        crawElement:'.move', // для примера div.move
        textElement:'p',
        hoverClass:'viewText'
    });

});

function flashMessage(message){
    if(message && !$('#flashMessage').get(0)) $('BODY').append('<div id="flashMessage"></div>');
    else if($('#flashMessage').get(0)) $('BODY').append($('#flashMessage'));
    else return false;

    if(message){
        $('#flashMessage').text('');
        $('#flashMessage').prepend(message);
    }

    var myLoad = function(hash){ hash.w.show(); setTimeout( "$('#flashMessage').jqmHide();", 4600);}; 
    $('#flashMessage').prepend('<a class="jqmClose">x</a>');
    $('#flashMessage').jqm({onShow:myLoad, overlay: 18});
    //$('#flashMessage').jqm({overlay: 18});
    $('#flashMessage').jqmShow();
}

function update_cart_button(content){
    $('.cart-button').remove();
    $('#sidebar-banner').before(content);
}

function show_advanced_search()
{
    var default_search_value = 'Search';
    $('#search-advanced-block FORM').hide();
    if($('#search-advanced-block FORM :text:first').val() == '')
        $('#search-advanced-block FORM :text:first').val(default_search_value);
    $('#search-advanced-block').slideDown('fast', function(){
        $('#search-advanced-block FORM').fadeIn();
        document.location.hash = 'search-advanced';
        $('#search-form INPUT').attr('disabled', 'disabled');
    });
    $('#search-advanced-block FORM :text:first').bind('click focus', function(){
        $(this).val('');
    });
    $('#search-advanced-block FORM :text:first').bind('focusout', function(){
        if($(this).val() == '') $(this).val(default_search_value);
    })
}
function hide_advanced_search()
{
    $('#search-advanced-block').fadeOut();
    document.location.hash = '';
    $('#search-form INPUT').removeAttr('disabled');
}

function ajaxSetupLoadStart(){
    var loadElement = $('#content');
    loadElement.append('<div class="loader"></div>')
}
function ajaxSetupLoadComplete(){
    var loadElement = $('#content');
    $('#content .loader').remove();
}
$.ajaxSetup({
        beforeSend: ajaxSetupLoadStart,
        complete: ajaxSetupLoadComplete
})
  
$.fn.clear_form_elements= function(){

    //$(this).click(function(){
    $(this).parents('FORM').find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
    //});

}

$.fn.clear_fields= function(){

    //$(this).click(function(){
    $(this).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
    //});

}

$.fn.paging= function(loadElement){
    var loadElement = loadElement || $('#content');
    var sthis = $(this);
    sthis.each(function(){
        $(this).click(function(){
            $.ajax({type:'GET',
                url:$(this).attr('href'), 
                beforeSend: function(){ajaxSetupLoadStart(loadElement)},
                complete: function(){ajaxSetupLoadComplete(loadElement)},
                success: function(data) {
                    loadElement.html(data);
                    // Checkbox, Radiobutton replacement
                    $(".checkbox", loadElement).dgStyle();
                    $(".checkbox-small", loadElement).dgStyle();
                    // Selectbox replacement
                    $('select', loadElement).not('.not-replace').selectbox();
                }
            });
            return false;
        })
    });
}
$.fn.sorting= function(){
    var sthis = $(this);
    sthis.each(function(){
        $(this).click(function(){
            $('#content').load($(this).attr('href'));
            return false;
        })
    });
}

function set_form_input_focus(el, default_value){

    if(default_value && el.val() == '') el.val(default_value);
    el.bind('click focus', function(){
        if(default_value == 'password' && el.attr('type') == 'text'){
            var elclone = $('<input type="password"/>').attr('id', el.attr('id')).attr('name', el.attr('name')).attr('tabindex', el.attr('tabindex'));
            elclone.replaceAll('#'+el.attr('id'));
            elclone.focus();// doesn't word in IE8
            set_form_input_focus(elclone);
            
            return true;
        }
        if(el.val() == default_value) el.val('');
    });
    
    el.focusout(function(){
        if(el.val() == '') el.val(default_value);
        //alert(default_value);
        if(el.val() == '' && el.attr('type') == 'password'){
              var elclone1 = $('<input type="text"/>').attr('id', el.attr('id')).attr('name', el.attr('name')).attr('tabindex', el.attr('tabindex')).val('password');
              elclone1.replaceAll('#'+el.attr('id'))
              set_form_input_focus(elclone1, 'password');
            return true;
        }
    })
    
    return true;
}
