var winCompare; // global variable

jQuery.fn.extend({
    fastSerialize: function() { //!!!! This will not submit name_x,name_y for image button
        var a = [];
        $('input,textarea,select,button', this).each(function() {
            var n = this.name;
            var t = this.type;
            if ( !n || this.disabled || t == 'reset' ||
                (t == 'checkbox' || t == 'radio') && !this.checked ||
                (t == 'submit' || t == 'image' || t == 'button') && this.form.clicked != this ||
                this.tagName.toLowerCase() == 'select' && this.selectedIndex == -1)
                return;
            if (t == 'select-multiple') {
                $('option:selected', this).each( function() {
                    a.push({name: n, value: this.value});
                });
                return;
            }
            a.push({name: n, value: this.value});
        });
        return a;
    }
});




$(function(){
    $('a.uc').click(function() {
        alert('In lucru'); 
        return false;
    });


    $('a.productTitle').cluetip({width:'600px'});
    $.fn.fancyzoom.defaultsOptions.imgDir= ssMVC_BASE + '/js/ressources/';   //must finish with a /

    // Select all links with tozoom class, set the open animation time to 1000
    $('a.tozoom').fancyzoom({Speed:500, overlay:0.8});


    var groups = new Array();
    $("[@class^=sameheight]").each(function() {
        var classes = this.className.split(' ');
        for(var i=0;i<classes.length;i++) {
            if (classes[i].substring(0,10) == 'sameheight') {
                if(groups[classes[i]]) {
                    groups[classes[i]] = Math.max($(this).height(),groups[classes[i]]);
		    
                } else {
                    groups[classes[i]] = $(this).height();
                }
            }
        }
        //alert(this.className);
    });
    for(var i in groups) {
        $("."+i).height(groups[i]);
    }


    var tabsCont = $('#charsProduct').eq(0);

    $('a.ajax').click(function(){

        var href   = this.href;
        var divCont = $('#other',tabsCont).eq(0);
        var div     = $('.tab-body',divCont).eq(0);
        var title  =  $('h2',divCont).eq(0);


        $('.tabs li',tabsCont).removeClass('active');
        $('#other-tab',tabsCont).removeClass('hide').addClass('active');
        $('#other-tab a').html(this.title || $(this).html());
        $('.tab',tabsCont).addClass('hide');
        divCont.removeClass('hide');


        var onsubmit = function() {
            $('form',div).each(function(){
                $(":submit,input:image,button",this).click(function(e) {this.form.clicked = this;});
                $(this).submit(function() {
                    var action = this.action || href;
                    var params = $(this).fastSerialize();
                    $(div).load(action,params,onsubmit);
                    return false;
                });
            });
        };

        $(title).html($(this).html());
        $(div).load(href,onsubmit);
        return false;
    });
    
    
    $('a.showtab').click(function() {
        $('.tabs li',tabsCont).removeClass('active');
        $('.tab',tabsCont).addClass('hide');

        $('a[@rel='+this.rel+']',tabsCont).parent().addClass('active');

        var divCont = $('#'+this.rel,tabsCont).eq(0);
        divCont.removeClass('hide');

        var div = $('.tab-body',divCont).eq(0);
        if(div.html() == 'Loading') {
            div.load(this.href);
        } else {
           // alert('already loaded');
        }

        //document.location.hash = this.rel;
        return false;
    });

    $('a[@rel=external]').click(function(){
        winCompare = window.open( $(this).attr('href'),'Comparare',"width=980,height=600,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
        return false;
    });

/*
    $('#banners').slideshow({
                timeout: 8000,
                type: 'sequence',
                pauselink: 'pause1',
                pausecallback: function(self){
                    self.html('Continua derularea promotiilor')
                },
                playcallback: function(self){
                    self.html('Opreste derularea promotiilor');
                }
    });
*/
    $('#banners').cycle({
        timeout: 5000,
        pager:  '#nav',
        pagerAnchorBuilder: function(idx, slide) {
            // return selector string for existing anchor 
            return '#nav a:eq(' + idx + ')';
        },
        pagerEvent: 'mouseover'
    });

    $('#stop').show().click(function(){
        $('#stop').hide();
        $('#start').show();
        $('#banners').cycle('pause');
        return false;
    });
    $('#start').hide().click(function(){
        $('#start').hide();
        $('#stop').show();
        $('#banners').cycle('resume',true);
        return false;
    });

});
