
$.fn.positionateSearchOptions = function (){

    var relatedToPosition = $('#siblingsNav').offset();
    
    if(typeof relatedToPosition !='undefined' && typeof relatedToPosition !='null' && relatedToPosition != null){

        $(this).css({
            'left' : relatedToPosition.left,
            'top' : relatedToPosition.top + $('#siblingsNav').outerHeight() + 10
        });

        $('#siblingsNav').css({
            'marginBottom':'80px'
        });
    }
}

$.fn.positionateCatAttachementBlock = function (){

    var relatedToPosition = $('#searchOptions').offset();

    if(typeof relatedToPosition !='undefined' && typeof relatedToPosition !='null' && relatedToPosition != null){

        $('#categoryAttachementsBlock').css({
            'left' : relatedToPosition.left,
            'top' : relatedToPosition.top + $('#searchOptions').outerHeight() + 10
        });

        $('#siblingsNav').css({
            'marginBottom':'100px'
        });
    } else {

        relatedToPosition = $('#siblingsNav').offset();

        if(typeof relatedToPosition !='undefined' && typeof relatedToPosition !='null' && relatedToPosition != null){

            $('#categoryAttachementsBlock').css({
                'left' : relatedToPosition.left - 20,
                'top' : relatedToPosition.top + $('#siblingsNav').outerHeight() + 10
            });

            $('#siblingsNav').css({
                'marginBottom':'80px'
            });
        } else {
            relatedToPosition = $('#red_label').offset();

            if(typeof relatedToPosition !='undefined' && typeof relatedToPosition !='null' && relatedToPosition != null){

                $('#categoryAttachementsBlock').css({
                    'left' : relatedToPosition.left,
                    'top' : relatedToPosition.top + $('#red_label').outerHeight() + 10
                });


            }
        }
    }
}

// jQuery mouseover stop scroll callback
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function getStates(current, destination, filter_request) {
    var country =  $('#' + current).val();
    if ( country ) {
        $('#' + destination).load("ajax_location_select.php",{
            'country': country,
            'filter_request':filter_request
        });
    } else {
        $('#' + destination).html("<" + "option value=''>Provincia</" + "option" + ">");
    }
}

function getCities(current,destination, filter_request) {
    var state =  $('#' + current).val();
    if ( state ) {
        $('#' + destination).load("ajax_location_select.php",{
            'state': state,
            'filter_request':filter_request
        });
    } else {
        $('#' + destination).html("<" + "option value=''>Ciudad<" + "/option" + ">");
    }
}



$(document).ready(function(){

    $('.groupListToggler').click(function(e){
        e.preventDefault();
        var target = $(this).attr('rel');
        $('#' + target).siblings().fadeOut(100);
        $('#' + target).fadeIn(500);
    });

    $('#searchOptions').positionateSearchOptions();

    $('#spareAskForInfo').positionateSearchOptions();
    
    $('#categoryAttachementsBlock').positionateCatAttachementBlock();



    $(":image").css({
        border:"none"
    });
    $(":checkbox").css({
        border:"none"
    });
    $(":radio").css({
        border:"none"
    });
    $(":submit").css({
        cursor:"pointer"
    });


    // DL slideToggle
    $('dl').find('dd').hide().end().find('dt').click(function() {
        $(this).next().toggle("fold",{
            horizFirst: true
        },400);
    });


    $('#ajax_search_reseller').ajaxForm({
        target: '#resellers_list'
    });


    $('#attachements_outer').hover(
        function() {
            $('#attachements').slideDown(300);
        },
        function() {
            $('#attachements').slideUp(300);
        }
        );

    $('.smoothButtons').prepend('<img src="immagini/layout_images/bg_smooth_buttons_left_corner.png" align="left" border="0"><img src="immagini/layout_images/bg_smooth_buttons_arrow.png"  border="0" class="center_img" style="margin-right:5px;">');
    
    $('input[type=text],input[type=password]').addClass('rounded');

    $('.contenidos_wide').css({
        width:'100%',
        'float':'none'
    });





    $('.tipCaller').click(function(e){
        e.preventDefault();
        var relTipContent = $(this).attr('rel');
        var tipCallerTitle =  $(this).attr('rev');

        TagToTip(relTipContent, TITLE, tipCallerTitle, CLICKCLOSE, true, CLOSEBTN, true, STICKY, true);

    })

    $('#familyListItemActive').effect("transfer", {
        to: $("#productContens")
        }, 1000);

    $('.spareRow').click(function(e){
        $(this).toggleClass('spareRowSelected');
        var me = $(this).find('.spareCode').attr('id');
        var that = $('.' + me);
        var myText = '<div class="'+me+'"><input type="hidden" name="comment[]" value="cod. '+ $(this).find('.spareCode').text()+' | '+$(this).find('.spareCode').next().text()+'">cod. '+ $(this).find('.spareCode').text()+'</div>';
        if(typeof that.html() !='""' && typeof that.html() !='undefined' && typeof that.html() !='null' && that.html() != null){
            $(that).remove();
        } else {
            $('#requestList').html().replace(myText,'');
            $('#requestList').append(myText);
        }
    });



    $('#spareTypeSwitch').change(function(){

        if($(this).val() == ''){
            $('#sparePartGroup').find('.spareTypeList').show();
        } else {
            $('#sparePartGroup').find('.spareTypeList:visible').toggle(300);
            $('#sparePartGroup').find('#spare_cat_'+$(this).val()).toggle(300);
        }

    });

    $('#spareAskForInfoButton').click(function(e){
        e.preventDefault();

        $('#spareRequestForm').submit();


    });

// $('textarea').wrap('<div class="box" >');

/*
    $('input.rounded').css({
        'border':'transparent'
    }).before('<img src="immagini/layout_images/bg_smooth_buttons_left_corner.png"  border="0" class="center_img" >');
*/
});


