/*
    CompareME XCart Module Developed by New Level Softwares
*/


(function($){
    $.fn.compareMeSlide = function(callerSettings) {
        var settings = $.extend({
            tabHandle: '.handle',
            speed: 300, 
            action: 'click',
            tabLocation: 'left',
            topPos: '200px',
            handleTop: '170px',
            leftPos: '20px',
            fixedPosition: false,
            positioning: 'absolute',
            pathToTabImage: null,
            imageHeight: null,
            imageWidth: null,
            onLoadSlideOut: false                       
        }, callerSettings||{});

        settings.tabHandle = $(settings.tabHandle);
        var obj = this;
        if (settings.fixedPosition === true) {
            settings.positioning = 'fixed';
        } else {
            settings.positioning = 'absolute';
        }
        
        //ie6 doesn't do well with the fixed option
        if (document.all && !window.opera && !window.XMLHttpRequest) {
            settings.positioning = 'absolute';
        }
        

        
        //set initial tabHandle css
        
        if (settings.pathToTabImage != null) {
            settings.tabHandle.css({
            /*'background' : 'url('+settings.pathToTabImage+') no-repeat',*/
            'width' : settings.imageWidth,
            'height': settings.imageHeight
            });
        }
        
        settings.tabHandle.css({
            'display': 'block',
            'textIndent' : '-99999px',
            'outline' : 'none',
            'position' : 'absolute'
        });
        
        obj.css({
            'line-height' : '1',
            'position' : settings.positioning
        });

        
        var properties = {
                    containerWidth: parseInt(obj.outerWidth(), 10) + 'px',
                    /*containerHeight: parseInt(obj.outerHeight(), 10) + 'px',*/
                    containerHeight: '550px',
                    tabWidth: parseInt(settings.tabHandle.outerWidth(), 10) + 'px',
                    tabHeight: parseInt(settings.tabHandle.outerHeight(), 10) + 'px'
                };

        if(settings.tabLocation === 'top' || settings.tabLocation === 'bottom') {
            obj.css({'left' : settings.leftPos});
            settings.tabHandle.css({'right' : 0});
        }
        
        if(settings.tabLocation === 'top') {
            obj.css({'top' : '-' + properties.containerHeight});
            settings.tabHandle.css({'bottom' : '-' + properties.tabHeight});
        }

        if(settings.tabLocation === 'bottom') {
            obj.css({'bottom' : '-' + properties.containerHeight, 'position' : 'fixed'});
            settings.tabHandle.css({'top' : '-' + properties.tabHeight});
        }
        
        if(settings.tabLocation === 'left' || settings.tabLocation === 'right') {
            obj.css({
                'height' : properties.containerHeight,
                'top' : settings.topPos
            });
            
            settings.tabHandle.css({'top' : settings.handleTop});
        }
        
        if(settings.tabLocation === 'left') {
            obj.css({ 'left': '-' + properties.containerWidth});
            settings.tabHandle.css({'right' : '-' + properties.tabWidth});
        }

        if(settings.tabLocation === 'right') {
            obj.css({ 'right': '-' + properties.containerWidth});
            settings.tabHandle.css({'left' : '-' + settings.imageWidth});
            
            $('html').css('overflow-x', 'hidden');
        }
        
        var loadComparison = function(){
            jQuery("#productsComparison").html('');
            jQuery(".productsComparison-div .loading").show();
            $.ajax({
                   type: "POST",
                   url: "compare_process.php",
                   data: { action: "loadProducts", cat:currentCat},
                   success: function(data){
                        jQuery(".productsComparison-div .loading").hide();
                        jQuery("#productsComparison").html(data);
                   }
             });
        }
        
        settings.tabHandle.click(function(event){
            event.preventDefault();
        });
        
        var slideIn = function() {
            
            if (settings.tabLocation === 'top') {
                obj.animate({top:'-' + properties.containerHeight}, settings.speed).removeClass('open');
            } else if (settings.tabLocation === 'left') {
                obj.animate({left: '-' + properties.containerWidth}, settings.speed).removeClass('open');
            } else if (settings.tabLocation === 'right') {
                obj.animate({right: '-' + properties.containerWidth}, settings.speed).removeClass('open');
            } else if (settings.tabLocation === 'bottom') {
                obj.animate({bottom: '-' + properties.containerHeight}, settings.speed).removeClass('open');
            }    
            
        };
        
        var slideOut = function() {
            
            if (settings.tabLocation == 'top') {
                obj.animate({top:'-3px'},  settings.speed).addClass('open');
            } else if (settings.tabLocation == 'left') {
                obj.animate({left:'-3px'},  settings.speed).addClass('open');
            } else if (settings.tabLocation == 'right') {
                obj.animate({right:'-3px'},  settings.speed).addClass('open');
            } else if (settings.tabLocation == 'bottom') {
                obj.animate({bottom:'-3px'},  settings.speed).addClass('open');
            }
            loadComparison();
        };

        var clickScreenToClose = function() {
            obj.click(function(event){
                event.stopPropagation();
            });
            
            $(document).click(function(){
                slideIn();
            });
        };
        
        var clickAction = function(){
            settings.tabHandle.click(function(event){
                if (obj.hasClass('open')) {
                    slideIn();
                } else {
                    slideOut();
                }
            });
            
            clickScreenToClose();
        };
        
        var slideOutOnLoad = function(){
            slideIn();
            setTimeout(slideOut, 500);
        };
        
        //choose which type of action to bind
        if (settings.action === 'click') {
            clickAction();
        }
        
        if (settings.onLoadSlideOut) {
            slideOutOnLoad();
        };
        
    };
})(jQuery);

/*EOF Slide Code*/

jQuery(document).ready(function(){
    var winHeight=jQuery('html').height();
    var winWidth=jQuery('html').width();
    
    jQuery("#successMsg").css("width", winWidth);
    jQuery("#successMsg").css("left", "0px");
    jQuery("#successMsg").css("top", "0px");
    
    jQuery("#successMsg").click(function(){
        jQuery("#successMsgCont").hide();
    });
    
    hideMsg=function(){
        jQuery("#successMsgCont").fadeOut(1000);
    }
    
    jQuery('#handlearea').hover(
          function () {
            //jQuery('.productsComparison-div a.handle').fadeIn('800');
          }, 
          function () {
            //jQuery('.productsComparison-div a.handle').fadeOut('800');
          }
    );
    
    jQuery('a.handle').hover(
          function () {
            $(this).animate({
                opacity: 1
              }, 100 );
          }, 
          function () {
            $(this).animate({
                opacity: 0.1
              }, 1000 );
          }
    );
    
    showSuccessMsg=function(){
        
            jQuery("#successMsgCont").fadeIn('500',function() {
                jQuery(this).fadeOut('1500', function() {             
                });
            })
            
            jQuery('.productsComparison-div a.handle').fadeIn('300',function() {
                jQuery(this).animate({  opacity: 1 }, 1000 );
                jQuery(this).fadeOut('300', function() {
                    jQuery(this).fadeIn('800',function() {
                        jQuery(this).fadeOut('800', function() {
                            jQuery(this).fadeIn('800',function() {
                                jQuery(this).fadeOut('800', function() {
                                    jQuery(this).fadeIn('800',function() {
                                        jQuery(this).animate({  opacity: 0.1  }, 1000 );
                                        jQuery("#successMsgCont").fadeOut("slow");
                                    });
                                });
                            });
                        });
                    });
                });
            });
    }
    
    addToCompareList = function(prodId){
        jQuery.ajax({
               type: "POST",
               url: "compare_process.php",
               data: { action: "addtolist", prodId : prodId },
               success: function(msg){
                    showSuccessMsg();
               }
         });
    }
    
    if(document.getElementById("removeCompareId")){
        jQuery('#removeCompareId').click(function(event){
            var selectedok=false;
            
            jQuery("input[rel=prod2remove]").each(function(){
                
                if(jQuery(this).is(':checked')){
//                    alert(this.value);
                    jQuery.ajax({
                           type: "POST",
                           url: "compare_process.php",
                           data: { action: "removefromlist", prodId : this.value },
                           success: function(msg){

                                jQuery("#productsComparison").html('');
                                jQuery(".productsComparison-div .loading").show();
                                $.ajax({
                                       type: "POST",
                                       url: "compare_process.php",
                                       data: { action: "loadProducts", cat : currentCat},
                                       success: function(data){
                                            jQuery(".productsComparison-div .loading").hide();
                                            jQuery("#productsComparison").html(data);
                                       }
                                 });
                           }
                     });
                    selectedok=true;
                }
                    
            });
            
           if(!selectedok){
                alert("Please select a product to remove.");
            }

        });
    }
    
    
});