var J = jQuery.noConflict();
	 
J(document).ready(function() {
		 
	//image preloder
	jQuery.preloadImages = function() {
		var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
		for(var i = a.length -1; i > 0; i--) {
			jQuery("<img>").attr("src", a[i]).load(function() {
			  J('body').append(J(this) );												
		    });
		}
	}
	J.preloadImages('/images/spinner.gif');
		 
	// for info dialog
	J("a").mouseover(function() { 
		var rel, bollonText, img, off, parent;
		rel = (J(this).attr('rel'));
		
		if(rel.match(/^balloon(\d+)$/i) !== null)
		{
		 parent = J(this).parent('li.info');   
		 off = J(parent).offset();
		 J("#" + rel).css({
		   position: "absolute",
		   visibility: 'visible',
		   top: (off.top + 25) + "px",
		   left: (off.left - 204) + "px"
		 }).show();
		}
	});
		 
	J("a").mouseout(function() {
		var rel, bollonText, anc;
		rel = (J(this).attr('rel'));
		if(rel.match(/^balloon(\d+)$/i) !== null)
		{
			J('body').mouseover(function(e) {
				var cond = J(e.target).is(".balloonstyle") || J(e.target).is(".balloondummy") || J(e.target).is('a[rel=' + rel + ']') || J(e.target).is("li.info");
				if (cond)
				{
					//J("#" + rel).show();
				}
				else
				{
					J("#" + rel).hide();
				}
			});
		}
	});
	
	//price update script
	J('#priceupdate-btn').click(function(){
		var action = 'priceupdate';
		var id = J('#prodid').val();
		var dataString = "action="+action+"&id="+id;
		var email = J('#youremail').val(); 
		var price = J('#price').val();
		price = price.replace(/£/,'');
		if(!(/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/).test(email))
		{
			growlupdate('Price Update Error!','This is an invalid email address.');
			return false;
		} else {
			dataString = dataString + "&email="+email;
		}
		if(price.search(/\S/) === -1) {
			growlupdate('Price Update Error!','Price field is empty.');
			return false;
		}else{
			dataString = dataString + "&price="+price;  
		}
		//ajax call
		J.ajax({
			type: "POST",
			url: "/priceupdate.php",
			data: dataString,
			success: function(msg){
				growlupdate('Thank You!',msg);
			},
			faliure: function () {
			  growlupdate('Try Later!','Failed to save your information.');
			}
		});
	});
	
	J('#ebayfrm').submit(function() {
  		var action = 'ebayauction';
		var id = J('#prodid').val();
		var dataString = "action="+action+"&id="+id;
		var email = J('#ebayemail').val(); 
		if(!(/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/).test(email))
		{
			alert("This is an invalid email!");
			return false;
		} 
		else
		{
			dataString = dataString + "&email="+email;
		}
		//ajax call
		J.ajax({
			type: "POST",
			url: "/priceupdate.php",
			data: dataString,
			success: function(msg){
				growlupdate('Thank You!',msg);
			},
			faliure: function () {
			  growlupdate('Try Later!','Failed to save your information.');
			}
		});
 		 return false;
	});
});

		
//short list script
function updShortlist(prodId,subcat){
	J(document).ready(function() {
		J.ajax({
			type: 'GET',
			url: '/addtomyshortlist.php?id='+prodId+'&subcat='+subcat,
			success: function(msg){
				
				var resArr = msg.split('#');
				var innerHTML = 'My Shortlist(0)';
				if(parseInt(resArr[0])>0){
				  	innerHTML = '<a href="/myshortlist.html">My Shortlist('+resArr[0]+')</a>';
				}
				J('#shortlist').html(innerHTML);
				growlupdate('Shortlist',resArr[1]);
			},
			faliure: function () {
			  growlupdate('Sorry Try Later!','Failed to add this product to your shortlist.');
			}
		});
	});	
	
}
function growlupdate(titl, msg){	
	// First of all I create a Notimoo instance with default configuration
	if (!this.ManagerOne)
		this.managerOne = new Notimoo();
		  
	// Showing a simple notification
	this.managerOne.show({
		title: titl,
		message: msg
	});	
}



	
function rateProduct(prodId, rating)
{
	J(document).ready(function() {
		J.ajax({
			type: 'GET',
			url: '/saveproductrating.php?id='+prodId+'&rating='+rating,
			success: function(msg){
				var resArr = msg.split('#');
				if(parseInt(resArr[0])>0)
				{
					J('#rating-img').attr('src', '/images/'+rating+'starrating.gif').load(function(){
						growlupdate('Thank You!',resArr[1]);
					});
					//J('#rating-text').html('('+resArr[0] + ' user ratings)');
				}
			},
			faliure: function () {
			  growlupdate('Try Later!','Failed to save your information.');
			}
		});
	});	
}

function merchant_rating(j, q, t, c){
	J(document).ready(function() {
		J.ajax({
			type: 'GET',
			url: '/ajax_merchant_rating.php?j='+j+'&q='+q+'&t='+t+'&c='+c,
			success: function(msg){
				growlupdate('Thank You!',msg);
			},
			faliure: function () {
			  growlupdate('Try Later!','Sorry, failed to save your information.');
			}
		});
	});	
}

		   



function showBalloon(balloonNum, el) {
	J(document).ready(function() {
		off = J(el).offset();					   
		J("#balloon" + balloonNum).css({
			position: "absolute",
			visibility: 'visible',
			top: (off.top + 85) + "px",
			left: (off.left - 75) + "px"
		}).show();
	});
}

function hideBalloon(balloonNum) {
	J("#balloon" + balloonNum).hide();
	/*J(document).ready(function() {
		J('body').mouseover(function(e) {
			var cond = J(e.target).is(".balloonstyle") || J(e.target).is(".balloondummy");
			if (cond)
			{
				//J("#" + rel).show();
			}
			else
			{
				J("#balloon" + balloonNum).css({visibility: 'hidden'});
			}
		});
	});*/
}

/* ----------------------------------------------------------------------------- */

	function showcats(dept){
            hidecats();
            document.getElementById("dept"+dept).style.height = "15px";
            var catnum=1;
            while(document.getElementById("cat"+dept+"-"+catnum) != null){
                document.getElementById("cat"+dept+"-"+catnum).style.display = "block";            
                catnum++;
            }
            document.getElementById("cat"+dept+"-btm").style.display = "block";            
    }

	function oldshowcats(dept){
            //hidecats();
            document.getElementById("dept"+dept).style.height = "15px";
            var catnum=1;
            while(document.getElementById("cat"+dept+"-"+catnum) != null){
                document.getElementById("cat"+dept+"-"+catnum).style.display = "block";            
                catnum++;
            }
            document.getElementById("cat"+dept+"-btm").style.display = "block";            
    }

	
    function hidecat(dept){
            document.getElementById("dept"+dept).style.height = "23px";
            var catnum=1;
            while(document.getElementById("cat"+dept+"-"+catnum) != null){
                document.getElementById("cat"+dept+"-"+catnum).style.display = "none";            
                catnum++;
            }
            document.getElementById("cat"+dept+"-btm").style.display = "none";            
    }
	
    function hidecats(){
        var deptnum=1;
        while(document.getElementById("dept"+deptnum) != null){
            var catnum=1;
            while(document.getElementById("cat"+deptnum+"-"+catnum) != null){
                document.getElementById("cat"+deptnum+"-"+catnum).style.display = "none";
                catnum++;    
            }
            document.getElementById("dept"+deptnum).style.height = "23px";
            document.getElementById("cat"+deptnum+"-btm").style.display = "none";
            deptnum++;
        }
    }
    function chg_tab(tab){
        var tabnum=1;
        while(document.getElementById("tab-"+tabnum) != null){
            document.getElementById("tab-"+tabnum).style.color = "#04405c";
            document.getElementById("content-tab-"+tabnum).style.display = "none";            
            tabnum++;
        }
        document.getElementById("tab-"+tab).style.color = "#d5162d";
        document.getElementById("content-tab-"+tab).style.display = "block";                        
    }

/* ----------------------------------------------------------------------------- */

	/*var countries=new ddtabcontent("countrytabstfg");
	countries.setpersist(true)
	countries.setselectedClassTarget("link") 
	countries.init()*/
	
	function facebook_init(){
		FB.init({appId: '118579034828907', status: true,
			   cookie: true, xfbml: true});
		FB.Event.subscribe('auth.login', function(response) {
		window.location.reload();
		});
	}
	
	function refresh_logout()
	{
	 window.location.href = 'profile.php?action=logout';
	}
	
	
			
	function hideShortlist() {
		$('shortlistPopOut').setStyle('visibility','hidden');
	}
	
	
	function addBookmark(){
		if((navigator.appName.indexOf("Microsoft",0)>=0)&&(parseInt(navigator.appVersion)>=4)){
			window.external.AddFavorite('http://www.dealclick.co.uk/','DealClick.co.uk - UK price comparison online shopping directory');
		}
		else{
			alert("Click 'OK' to close this window, then hit Ctrl+D to bookmark DealClick.co.uk");
		}
	}
	
	function searchProduct(searchText){
		if(searchText.search(/\S/) !== -1)
		{
			document.getElementById('kw').value = searchText;
			window.location = document.location.protocol + "//" + document.location.hostname + "/search/index.html?kw=" + searchText;
		}
	}
	
	
	google_ad_client = "pub-5051751857729182";
    /* 728x15, created 2/14/08 */
    google_ad_slot = "2613051489";
    google_ad_width = 720;
    google_ad_height = 15;


function hideTableColumn(table, columnNumber)
{
    var table = document.getElementById(table);
    if (table)
    {
        var rows = table.getElementsByTagName('tr');

        for (var row=0; row<rows.length;row++) 
        {
            var cells = rows[row].getElementsByTagName('td');
            cells[columnNumber].style.display='none';
        }
    }
}

function showTableColumn(table, columnNumber)
{
    var table = document.getElementById(table);
    if (table)
    {
        var rows = table.getElementsByTagName('tr');

        for (var row=0; row<rows.length;row++) 
        {
            var cells = rows[row].getElementsByTagName('td');
            cells[columnNumber].style.display='';
        }
    }
}

var currentFirstColumn = 1;
var hiddenColumns = 0;

function scrollTableRight(table, MaximumColumnsToDisplay, TotalTableColumns)
{
    var columnToShow = currentFirstColumn + MaximumColumnsToDisplay;
    hideTableColumn(table, currentFirstColumn);
    showTableColumn(table, columnToShow);
    currentFirstColumn++;
    
    if (columnToShow == TotalTableColumns)
    {
        //this is the last column, hide the scroll button.
        var scrollButton = document.getElementById('scrollTableRight');
        if (scrollButton != null)
            scrollButton.style.display = 'none';
    }
    
    var scrollButton = document.getElementById('scrollTableLeft');
    if (scrollButton != null)
        scrollButton.style.display = 'block';
        
    UpdateStatusText(MaximumColumnsToDisplay, TotalTableColumns);
}

function scrollTableLeft(table, MaximumColumnsToDisplay, TotalTableColumns)
{
    var columnToShow = currentFirstColumn-1;
    var columnToHide = currentFirstColumn + MaximumColumnsToDisplay - 1;
    
    if (columnToShow < 1)
        return;
    
    hideTableColumn(table, columnToHide);
    showTableColumn(table, columnToShow);
    currentFirstColumn--;
    
    if (currentFirstColumn > 1)   
    {
        //show the scroll button.
        var scrollButton = document.getElementById('scrollTableLeft');
        if (scrollButton != null)
            scrollButton.style.display = 'block';
    }
    else
    {
        //hide the scroll button.
        var scrollButton = document.getElementById('scrollTableLeft');
        if (scrollButton != null)
            scrollButton.style.display = 'none';
    }
    
    var scrollButton = document.getElementById('scrollTableRight');
    if (scrollButton != null)
        scrollButton.style.display = 'block';
      
    UpdateStatusText(MaximumColumnsToDisplay, TotalTableColumns);
}

function HideColumn(table, ColumnToHide, MaximumColumnsToDisplay, TotalTableColumns)
{
    hideTableColumn(table, ColumnToHide);
    hiddenColumns++;
    
    //display the next column available
    var nextHiddenColumn = currentFirstColumn + MaximumColumnsToDisplay;
    if (nextHiddenColumn < TotalTableColumns)
        showTableColumn(table, nextHiddenColumn);
        
    if ((TotalTableColumns - hiddenColumns) > MaximumColumnsToDisplay)
    {
        //show the scroll button.
        var scrollButton = document.getElementById('scrollTableRight');
        if (scrollButton != null)
            scrollButton.style.display = 'block';
    }
    else
    {
        //hide the scroll button.
        var scrollButton = document.getElementById('scrollTableRight');
        if (scrollButton != null)
            scrollButton.style.display = 'none';
    }
    
    UpdateStatusText(MaximumColumnsToDisplay, TotalTableColumns);
}

function hidefilter(num){
    J(".dept_expand_middle").each( function() {
        if(J(this).attr("id")==num){
            J(this).hide();
        }
    })
    J(".dept_expand_btm").each( function() {
        if(J(this).attr("id")==num){
            J(this).hide();
        }
    })
    J(".dept_expand_btmshow").each( function() {
        if(J(this).attr("id")==num){
            J(this).show();
        }
    })
    
}
function showfilter(num){
    J(".dept_expand_middle").each( function() {
        if(J(this).attr("id")==num){
            J(this).show();
        }
    })
    J(".dept_expand_btm").each( function() {
        if(J(this).attr("id")==num){
            J(this).show();
        }
    })
    J(".dept_expand_btmshow").each( function() {
        if(J(this).attr("id")==num){
            J(this).hide();
        }
    })
}



