$(function() {

$(".showDescription").click(function(event){
				event.preventDefault();
				var isShowing = $(this).parent().parent().next(".promoDesc").css("display");
				
				if(isShowing == 'none')
				{
					//change the icon from down to up
					$(this).children("img").attr("src","/images/up.png");
					//show the content
					$(this).parent().parent().next(".promoDesc").css("display","block");
				}
				else
				{
					//change the icon from down to up
					$(this).children("img").attr("src","/development/new_igain/images/down.png");
					$(this).parent().parent().next(".promoDesc").css("display","none");
				}
			});
			
			$(".wishListReward").click(function(event){
				event.preventDefault();
				var currentWLStatus = $(this).children().attr("title");
				//fade this out	
				
				if(currentWLStatus == "add")
				{
					$(this).animate({
						opacity: 0
					},500);
					$(this).children().attr({
							src: "images/remove_wishlist.png",
							title:"remove"
					});
					$(this).animate({
							opacity: 1
					},1000);
					
					//put a message in the message div
					var headerVal = $(this).parents(".promoHolder").children(".promoTitle").find("h3").text();
					$(".infobox .content").append("<span class='fadeMessage'>Added " + headerVal + " to Wishlist</span><br />");
				}
				else
				{
					$(this).animate({
					opacity: 0
					},1000).children().attr({
						src: "/images/add_to_wishlist_small.png",
						title:"add"
						});
					$(this).animate({
					opacity: 1
					},1000);
					
					var headerVal = $(this).parents(".promoHolder").children(".promoTitle").find("h3").text();
					$(".infobox .content").append("<span class='fadeMessage'>Removed " + headerVal + " from Wishlist</span><br />");
				}	
			});
			
			
			$("#messageHolder").live("append", function(event){
				console.log('change fired -' +event.toSource());
				fadeOut('slow');
			});
			
			$(".infobox").bind("onload", function(){
				console.log('loaded');
			});
			
			$(".newNavigation li").hover(
				function(event){
						if($.browser.version.msie)
						{
							$(this).children("dl").css("display","block");
							$(this).children("dl").css("zIndex","1000000");
							$("#searchBox input").css("border", "1px solid #f00");
						}else{
							$(this).children("dl").css("display","block");
							
						}
				},
				function(event){
					$(this).children("dl").css("display","none");	
			});
			
			$(".dropDown").hover(
				function(event){
						$(this).children("dl").css("display","block");
						if($.browser.version.msie)
						{
							$(this).children("dl").css("border","1px solid #def");
						}
				},
				function(event){
					$(this).children("dl").css("display","none");	
			});


	for (i=0;i<=10;i++)
		{
			$("#form"+i).hide();
		}
        
               $("#wishlist").sortable({
                handle : '.handle',
                update : function () {
                  var order = $('#wishlist').sortable('serialize');
                  $(".infobox .content").load("/scripts/process-sortable.php?"+order);
                }
              });

		$("#addressform").hide();
                $("#address").hide();
		$("ul [name=subcats]").hide();
		$("#hidecats").hide();
		$("#loading").hide();
		
		$("#selectallcats").each(function(){
					this.checked = 1;
		});
		
		$("li [class=category]").each(function()
				{
					this.checked = 1;
				});
 
	function removeAllOptions(selectbox)
	{
		var i;
		for(i=selectbox.options.length-1;i>=0;i--)
		{
		selectbox.remove(i);
		}
	}
	

		
		$(function() {
			$("#opencats").click(function(){
				animate(this,'catform','show', 'vertical');
				animate(this,'opencats','hide', 'vertical');
				animate(this,'hidecats','show', 'vertical');
			});
			
			$("#hidecats").click(function(){
				animate(this,'catform','hide', 'vertical');	
				animate(this,'hidecats','hide', 'vertical');
				animate(this,'opencats','show', 'vertical');
				
			});
            
			
			$("#selectallcats").click(function()				
			{
				var checked_status = this.checked;
				$("li [id=category]").each(function()
				{
					this.checked = checked_status;
				});
			});
			
			$("li [id=category]").click(function()
				{
				  $("#selectallcats").each(function(){
					  this.checked = 0;
				  });
			});
	});
 });

function animate(self,id,effect, optins){
	
	$(function() {
		if(effect == 'show'){
			$("#"+id).show('slide',optins,500);
		}
		if(effect == 'hide'){
			$("#"+id).hide('slide',optins,500);
		}

	});
}

function openallqs(self,optins){
	for (id=0;id<=10;id++)
		{openqs(self,'_'+id,'show','show');	}	
		SelectAll();
}

function SelectAll()
{
	var offerslist = document.getElementById('offers').value.split(",");
	for(i = 0; i < offerslist.length; i++){
        document.getElementById('tb_' + offerslist[i]).src = "http://static.igain.co.uk/devlayout/tickon.gif";
        document.getElementById('tickbox_' + offerslist[i]).value = "1";
        $("div [id*=questions]").show();
        $("div [id*=form]").show();
	}
}

function selectqs(self, id, cid){
    if(self.src == "http://static.igain.co.uk/devlayout/tickoff.gif"){
        openqs(self,id);
        document.getElementById('tickbox' + cid).value = "1";
        self.src = "http://static.igain.co.uk/devlayout/tickon.gif";
    }else{
        closeqs(self,id);
        document.getElementById('tickbox' + cid).value = "0";
        self.src = "http://static.igain.co.uk/devlayout/tickoff.gif";
    }
}

function openqs(self,id){
	$(function() {
			$("#questions"+id).show('slide','',100);
			$("#form"+id).show('slide','',500);

	});
}

function closeqs(self,id){
	$(function() {
			$("#form"+id).hide('slide','',500);
			$("#questions"+id).hide('slide','',100);
	});
}

function GetXmlHttpObject()
{
                var xmlHttp=null;
                try
                {
                                xmlHttp=new XMLHttpRequest();
                }
                catch (e)
                {
                                try
                                {
                                                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                                }
                                catch (e)
                                {
                                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                                }
                }
                return xmlHttp;
}
