  $(document).ready(function(){
    var tabs = new Array('2', '4', '5', '7', '8');
 
    for (i=0; i<tabs.length; i++) {
      //alert(tabs[i]);
      if (tabs[i]!='2') {
        $('#main_description_tab_'+tabs[i]).addClass("gray_background");
      }
      $('#main_description_tab_'+tabs[i]).click(function() {
        for (j=0; j<tabs.length; j++) {
          if (this.id == 'main_description_tab_'+tabs[j]) {
            $('#main_description_tab_'+tabs[j]).removeClass("gray_background").addClass("white_background");
            $('#main_description_'+tabs[j]).show();
          } else {
            $('#main_description_tab_'+tabs[j]).removeClass("white_background").addClass("gray_background");
            $('#main_description_'+tabs[j]).hide();
          }
        }
      });
    }
		
		$("a.fancyBox").fancybox();
		
    $("#tellAFriendDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 90,
        width:380,
        modal: true,
        title:'Tell a friend'
    });
		$('a.tellAFriendLink').click(function(event) {
			event.preventDefault();
			$('#tellAFriendDialog').dialog('open');
		});
		$('a#BestPriceGuaranteed').click(function(event) {
			event.preventDefault();
			$('.BestPriceGuaranteedDialog').toggle();
		});
		$('.BestPriceGuaranteedDialog').click(function(event) {
			event.preventDefault();
			$('.BestPriceGuaranteedDialog').toggle();
		});
        
		$('a#ADA').click(function(event) {
			event.preventDefault();
			$('.ADADialog').toggle();
		});
		$('.ADADialog').click(function(event) {
			event.preventDefault();
			$('.ADADialog').toggle();
		});
        
		$('a#thirty_day_back_guarantee').click(function(event) {
			event.preventDefault();
			$('.thirty_day_back_guarantee_dialog').toggle();
		});
		$('#thirty_day_back_guarantee_dialog_close').click(function(event) {
			event.preventDefault();
			$('.thirty_day_back_guarantee_dialog').toggle();
		});
		$('#thirty_day_back_guarantee_dialog_link').click(function(event) {
			$('.thirty_day_back_guarantee_dialog').toggle();
		});        
        
		
		///jsbookmark
		// add a "rel" attrib for Opera 7+
		if(window.opera) {
			if ($("a.bookmark").attr("rel") != ""){
				$("a.bookmark").attr("rel","sidebar");
			} 
		}
	
		$("a.bookmark").click(function(event){
			event.preventDefault();
			var url = this.href;
			var title = this.title;
			
			if (window.sidebar) { // Mozilla Firefox Bookmark
				window.sidebar.addPanel(title, url,"");
			} else if( window.external ) { // IE Favorite
				window.external.AddFavorite( url, title);
			} else if(window.opera) { // Opera 7+
				return false; // do nothing
			} else { 
				 alert('Unfortunately, this browser does not support the requested action, please bookmark this page manually.');
			}
		
		});
		///eojsbookmark
        
    //Dialog
    $("#nameyourpricedialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 250,
        width:450,
        modal: true,
        title:'Name your Price',
        buttons: {
            'Send': function() {
                var bValid = true;
                //allFields.removeClass('ui-state-error');
                var Error = "";
                
                name = $("#fullname");
                if(name.val()=="") {
                    Error ="Name is requred field.<br />";
                }
                
                email = $("#email");
                if(email.val()=="") {
                    Error ="Email is requred field.<br />";
                }
                
                phone = $("#phone");
                if(phone.val()=="") {
                    Error ="Phone is requred field.<br />";
                }
                
                if(Error!="") {
                    updateTips(Error);
                    return false;
                }
                
                bValid = bValid && checkRegexp(name,/^[a-z ]+$/i,"Username may consist of a-z.");
                bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. user@domain.com");
                //bValid = bValid && checkRegexp(phone,/^([0-9\+ \(\)])+$/,"Phone number allow only : 0-9 ( ) +");


                if (bValid) {
                    $.post("<?php echo FILENAME_DEFAULT; ?>?action=post_desiredprice", {
                                    fullname:$("#fullname").val(),
                                    email:$("#email").val(),
                                    phone:$("#phone").val(),
                                    desiredprice:$("#desiredprice").val(),
                                    products_id:$("#nameprice_products_id").val()
                                    },function(data){
                                     return true;
                                    });
                    $(this).dialog('close');
                }
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        }
    });
    $('#nameyourpricebutton').click(function() {
        $('#nameyourpricedialog').dialog('open');
    });
    
    ////////////////////////// Paginator START //////////////////////////
    
		var numPerPage = 3;
		var currentPage = 0;
		
		var repaginate = function() { 
			var start=currentPage * numPerPage;
			var end=(currentPage + 1) * numPerPage;
			$('table.paginated').find('tr')
			.slice(start, end).fadeIn().end()
			.slice(0, start).hide().end()
			.slice(end).hide().end();
		}
		
		var numRows = $('table.paginated').find('tr').length;
		var numPages = Math.ceil(numRows / numPerPage);
		var pager = $('<div class="paging"><span class="title">Pages: </span></div>');
		for (var page = 0; page < numPages; page++) {
			$('<span class="page-number pagingLink border" id="page-' + (page + 1) + '">&nbsp;' + (page + 1) + '&nbsp;</span>')
			.bind('click', {'newPage': page}, function(event) {
				currentPage = event.data['newPage'];
				repaginate();
				$('span.page-number').addClass('pagingLink');
				$('span#page-' + (currentPage+1)).removeClass('pagingLink');
			}).appendTo(pager);
		}
		pager.find('span.page-number:first').removeClass('pagingLink');
		pager.insertAfter($('table.paginated'));
		repaginate();
		
		////////////////////////// Paginator END //////////////////////////
		
  });
  
    function checkRegexp(o,regexp,n) {
        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass('ui-state-error');
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }
    function checkLength(o,n,min,max) {
        if ( o.val().length > max || o.val().length < min ) {
            o.addClass('ui-state-error');
            updateTips("Length of " + n + " must be between "+min+" and "+max+".");
            return false;
        } else {
            return true;
        }
    }
  
    function updateTips(m) {
        $("#validateErrors").html(m);
        $("#validateErrors").css("display", "block");
    }
    
//-------------------------------
function updateInformations(id, imgsrc, price, o) {
    updateLinks(o);
    selectImage(id, imgsrc);

    updatePrice(price);
    
}

function updateLinks(o) {
    link = $(o).attr('link_key');
    if($(o).attr('checked')) {
        $("input[link_key='"+link+"']").map(function() {
            if($(o).attr('id') != $(this).attr('id')) {
                $(this).click();
            }
        });
    }
}

function selectImage(id, imgsrc) {
    $('#bundle_id_' + id).html('<img src="/images/'+ imgsrc + '">');
}

function updatePrice(price) {
    total = 0;
    
    $("input[attr='bundle_option']:checked").map(function() {
    
        qty_id = $(this).attr("id");
        var srchText = /id_/g;
        var replacement = 'bundle_id_qty_';
        qty_id = qty_id.replace(srchText, replacement);
        if($("#"+ qty_id).val()) {
            qty = $("#"+ qty_id).val();
        }
        else {
            qty = $(this).attr('amount');
        }
        qty_int = parseInt(qty);
        prc = parseFloat($(this).attr('price'));
        total = total + (prc * qty_int);
    });
    total = toMoney(total);
    $(".old_price_02").html(total);
}


//rounds number to d decimal places, defaults to 2
round=function(number,decimal) {
var d=(decimal?decimal:2);
return Math.round(number*Math.pow(10,d))/Math.pow(10,d);
}

//return the amount in the money format .99 (/^\d+\.\d{2}$/)
//used round() function
toMoney=function(amount) {
var a=round(amount);
return(a==Math.floor(a))?a+'.00':((a*10==Math.floor(a*10))?a+'0':a);
}