// JavaScript Document
(function ($) {
    

     $.fn.extend({
        oneFBLike: function(options) {  
        
            //Set the default values, use comma to separate the settings 
            var defaults = {  
            	appID: '220687584610079',
            	siteTitle: '',
            	siteName: '',
            	siteImage: '',
                url: '',
            	buttonWidth: 450,
            	buttonHeight: 80,
            	showfaces: false,
            	font: '',
            	layout: 'normal',
            	action: 'recommend',
            	colorscheme: 'light'
            	
            }  
            
            var options =  $.extend(defaults, options);  
                        
            return this.each(function() {  
                var o = options;  
                var obj = $(this);
                
                // Add Meta Tags for additional data - options
               // jQuery('head').append('<meta property="og:title" content="'+o.siteTitle+'"/>');
                //jQuery('head').append('<meta property="og:site_name" content="'+o.siteName+'"/>');
               // jQuery('head').append('<meta property="og:image" content="'+o.siteImage+'"/>');
                
                // Add #fb-root div - mandatory - do not remove
                
                if ($("#fb-root").length==0){
                $('body').append('<div id="fb-root"></div>');
                
                }
                

                function sizeChangeCallback() {
                FB.Canvas.setSize();
               }
                // setup FB Developers App Link - do not touch
                window.fbAsyncInit = function() {
				FB.init({appId: o.appID, status: true, cookie: true, xfbml: true});
             //   $(window).onload(sizeChangeCallback());
                               
				};
				(function() {
				var e = document.createElement('script'); 
                e.async = true;
				e.src = document.location.protocol +
				'//connect.facebook.net/en_US/all.js';
				document.getElementById('fb-root').appendChild(e);
				}());
                

               //  $('iframe').attr({'border':'none', 'allowTransparency':'true','scrolling':'0'});
               
               //  $('iframe').attr('style','border:none; overflow:hidden');

                // Grab the URL and assign it to a variable
               
                var fbDIV = obj;
                var $url  = $(this).attr('id');
                                
                              
                // Apply the like button to an element on the page and include all available options
                // If no options are passed in from the page, the defaults will be applied            
              //  jQuery(fbDIV).html('<fb:like href="'+$url+'" width="'+o.buttonWidth+'" height="'+o.buttonHeight+'" show_faces="'+o.showfaces+'" font="'+o.font+'" layout="'+o.layout+'" action="'+o.action+'" colorscheme="'+o.colorscheme+'"/>')

                              
            });  
        } 
    });
 
	    	jQuery.fn.eGallery = function(){
    return this.each(function() {
	  
	         $(this).next().fadeIn(400).show();
					
							  });
};

jQuery.fn.Gallery = function(){
	 
 	   return this.each(function() {
	   var $gallery = $(this);
       
       var $images  = $(this).find('img');
       var currentPosition = $('.show').index();     
       var numberOfImages = $images.length;
       
    
       
  	  manageControls(currentPosition);
   
    $('.control').click(function(){
        
        var $id = $(this).attr('id');
        
        currentPosition = ($id=='next') ? currentPosition+1 : currentPosition-1;
        
        

     var current = ($('#gallery img.show') ?  $('#gallery img.show') : $('#gallery img:first'));

    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    if($id =='next'){
    var next = ((current.next().length) ? ((current.next().hasClass('show'))? $('#gallery img:first') :current.next()) : $('#gallery img:first'));  
    next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
    //Hide the current image
    }else{
    var prev = ((current.next().length) ? ((current.prev().hasClass('show'))? $('#gallery img:last') :current.prev()) : $('#gallery img:last'));  
    prev.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
    }
    manageControls(currentPosition);
    current.animate({opacity: 0.0}, 1000).removeClass('show');
    
         })  
    function manageControls(position){

    // Hide left arrow if position is first slide

	if(position==0){ $('#prev').hide() } else{ $('#prev').show() }

	// Hide right arrow if position is last slide

    if(position==numberOfImages-1){ $('#next').hide() } else{ $('#next').show() }

  }         
        });                     
    
    
 
            
	};
    	


jQuery.fn.Review = function(){
	
	$(this).each(function() {
    var $select = $(this); // Cache a refernce to the current select
    var sliderDiv = $("<div></div>"); // create a div for the slider
    var displayLabel = $('<span></span>').css({'margin-top':'5px', 'color':'#666', 'font-weight':'bold'}); // create a span to display current selection

    if ($select[0].selectedIndex < 0) // Make sure that an item is selected
        $select[0].selectedIndex = 0;

    $select.hide() // hide the select
        .before( // Insert display label before the select
            displayLabel
               .text($select.find("option:selected").text()) // and set it's default text
        )
        .after( 
           sliderDiv // Insert the silder div after the select
               .data("select", $select) // store a reference to the select
               .data("label", displayLabel) // store a reference to the display label
               .slider({
					range: "min",  
					value: 1,
                    max: $select.find("option").length - 1, // set to number of items in select
                    slide: function(event, ui) {
                        var $select = $(this).data("select"); 
                        $select[0].selectedIndex = ui.value; // update the select
                        $(this).data("label").text( // Update the display label
                            $select.find("option:selected").text()
                        );
                    }
               }).css({'width':'200px','float':'left','margin-right':'15px','margin-top':'4px'})
        );
});
	
}
    
    	jQuery.fn.eDelay = function(time){
     return this.each(function() {
		var i = $(this);
		i.queue(function(){
			setTimeout(function(){
				i.dequeue();
			}, time);
		});
	});
};

       jQuery.fn.jEvent = function(opt, event_id, event_title, event_start, event) {
        
        
        
        if(opt=='renderEvent' || opt=='updateEvent'){
            
            
        }else{
      
     
        }
   				var originalEventObject = $(this).data('eventObject');
				
				// we need to copy it, so that multiple events don't have a reference to the same object
				var copiedEventObject = $.extend({}, originalEventObject);
                copiedEventObject.title = event_title;
                copiedEventObject.start = event_start;
     
                 return this.each(function() {
					$('#calendar').fullCalendar(opt, copiedEventObject, true);
                                       
				
	//	$(this).fullCalendar('unselect');
                });
                };

$.fn.vtxt = function () {

return this.each(function() {
    var default_value = this.value;
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            this.value = default_value;
        }
    });
});
};


 $.fn.limitChars = function (infodiv,limit){
    
   
  $(this).keyup(function(){
   
  var text =$(this).val()
  var textlength = text.length;
  if(textlength > limit)
  {
  $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
  $(this).val(text.substr(0,limit));
  return false;
  }
  else
 {
 $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
  return true;
  }
  });

  }

$.fn.dbox = function (msg, bgc) {     
  return this.each(function() {
							
				       //Get the screen height and width   
        var maskHeight = $(document).height();   
        var maskWidth  = $(window).width();   
        //Set height and width to mask to fill up the whole screen   
       $('#mask').css({'height':maskHeight+'px'});   
       $('#mask').css({'display': 'block'}); 
	   if(bgc==true){
       $('#mask').css({'background':'#000','opacity': '0.2'});
	   }
       //transition effect 
       $('#mask').show();   
       $(this).css({'background':'transparent url(images/b.png)'});
   
       $(this).css({'min-width':'400px','max-height':'1000px'});		 
        

        $(this).html(msg); 
        $(this).centerInClient({ container: window, forceAbsolute: true });
		$(this).show(); 
        
   
       
    //if close button is clicked   
    $('.window .close').click(function () {   
        //Cancel the link behavior      
                  $('#mask').fadeOut(500,function(){
            $('.window').fadeOut(400).hide();
            
        });      
    });        
       	
    //if mask is clicked   
    $('#mask').click(function () {  
              $('#mask').fadeOut(500,function(){
            $('.window').fadeOut(400).hide();
            
        });      
    }); 
			
       
});
};

 $.fn.jRedirect = function(location, time) {  
	 return this.each(function() { 
		window.setTimeout ( function() {
	   window.location = location; 
	},time*1000);
	 });  
 } 
 
 	     function countChecked() {
	var n = $("input:checked").not('#paradigm_all').length;

	if(n < 1){
		$(".submit").hide();
	
        $('#paradigm_all').removeAttr("checked");
		
		}else{
		$(".submit").show();
	  
		
		}
		


	};
 $.fn.jCheck = 	 function(){
     
	 return this.each(function() { 
	 $(this).click(function(){

	var company_id = $(this).val();
	var company_name = $(this).attr("alt");						
	 var wrap  = '<span class="c'+company_id+'">'+company_name+'<a class="del" id="'+company_id+'">X</a>';
	     wrap += '<input type="hidden"  name="quote[]" value="'+company_id+'" /></span>';
	if($(".c"+company_id).length > 0){$(".c"+company_id).fadeOut(400).remove(); countChecked();}else{
	$("#companies").append(wrap); countChecked();}

	$('.del').click(function(){  										  
	var cid= $(this).attr('id'); 
	$("#q"+cid).removeAttr("checked");                   
	$(this).parent().fadeOut(400).remove();
	countChecked();
	});
	});
});
 }
 


$.fn.centerInClient = function(options) {

    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
     // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.appendTo("body");
            else
                el.appendTo(jWin.get(0));
        }
        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}

 
 
 $.fn.CheckAll = function(input){

	return this.each(function() { 
							  
	        $(this).click(function() { 
						   
	             var checked_status = this.checked;
				$(input).each(function(){
					this.checked = checked_status;
					 var company_id = $(this).val();
	                  var company_name = $(this).attr("alt");
					  var isChecked   = $(this).is(':checked');	
	            var wrap  = '<span class="c'+company_id+'">'+company_name+'<a class="del" id="'+company_id+'">X</a>';
	                wrap += '<input type="hidden"  name="quote[]" value="'+company_id+'" /></span>';
                	if($(".c"+company_id).length > 0){ if(!isChecked){ $(".c"+company_id).fadeOut(400).remove(); countChecked();}}else{
	                if(isChecked){ $("#companies").append(wrap); countChecked();}}
					
				$('.del').click(function(){  										  
             	var cid= $(this).attr('id'); 
				$("#q"+cid).removeAttr("checked");                   
				$(this).parent().fadeOut(400).remove();
				countChecked();
	});
		     
				});

            }); 
			
			});
			
 }
 
 $.fn.jBid = function(){
    
    var $min = parseFloat(1.99); 

      
        $(this).click(function(){
            var $bid = $(this).parent().find('span');
             var $amount = $bid.text();
             var $price  = parseFloat($amount);
        if($(this).hasClass('plus')){
           
            
                $price +=parseFloat(0.01);
            var $final = $price.toFixed(2);
            $bid.text($final);  
          
     }else{
 
                $price -= parseFloat(0.01);
            var $final  = $price.toFixed(2);
            if($final<$min){
                alert('Minimum bid is 1.99')
            }else{
                 $bid.text($final);
                 }
     }
     

        })
    

    };


})(jQuery)
