// LinITX specific stuff

(function($) {

	var methods = {

	   init		:
			function () {
			
				$("ul.nav li.nest a.indicator").unbind('click');
				$("ul.nav li.nest a.indicator").click(function(){	
					var root = this;

					$(this).next("li.nest > ul").slideToggle(250, function () {
						if ($(this).is(':hidden')) 
							$(root).html("+");
						else
							$(root).html("-");
					});
		
					return false;
				});

				$("ul.nav li.nest a.indicator").each(function() {
                                        var root = this;
                                        
                                        if ($(this).next("li.nest > ul").is(':hidden'))  
                                                        $(root).html("+");
                                                else
                                                        $(root).html("-");
				});


				if ($(location).attr('href').indexOf('shoppingcart.php') !=-1) {

					$("div .routes a").each(function() {
	                                        headerArray = ['#header','#header'];
        	                                bodyArray = ['#body', '#body'];
                	                        mainArray = [ headerArray, bodyArray];

                        	                $(this).linitx('linkify',mainArray,'Switching step...',false,false,'','Switch Step');
                                	});
			

					$("form").each(function() {
						frmname=$(this).attr('id');
						if (((frmname.indexOf('remove') ==0) || (frmname.indexOf('add') ==0 ) || (frmname.indexOf('del') ==0 ) || (frmname.indexOf('set') ==0 ) ) && (frmname.substring(0,10) != 'addtocart_')) { 
							bodyArray = ['#body','#body'];
							basketArray=['#cartbits','#cartbits'];

                        	                        mainArray=[basketArray, bodyArray];

                                                	$('#'+frmname).linitx('ajaxify',mainArray, 'Updating cart...',false,false);
						} else if (frmname.indexOf('emptycart') ==0) {
                                                        bodyArray=['#body', '#body'];
							basketArray=['#cartbits','#cartbits'];
                                                        mainArray=[basketArray, bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Emptying cart...',false);
                                                } else if (frmname.indexOf('currencypound') ==0 ||frmname.indexOf('currencyeuro') ==0 || frmname.indexOf('currencydollar') ==0 ) {
							bodyArray=['#body', '#body'];
                                                        basketArray=['#cartbits','#cartbits'];

                                                        mainArray=[basketArray, bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Emptying cart...',false);

						} else if (frmname == 'nextstepfrm') {
							bodyArray = ['#body', '#body'];
                                                        basketArray=['#cartbits','#cartbits'];
							mainArray = [basketArray, bodyArray];
        	                                        $(this).linitx('ajaxify',mainArray,'Checking input and moving to next step...',false);
						} else if (frmname=='userdetailsform') {
                                                        basketArray=['#account','#account'];
                                                        bodyArray=['#body', '#body'];
                                                        mainArray=[basketArray, bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Registering details...',false);
						} else if (frmname=='updateuserdetails') {
                                                        bodyArray=['#body', '#body'];
                                                        mainArray=[bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Saving your details...',false);

                                                        $('#submitSetDetails').hide();
                                                        $('#'+frmname).change(function() {
                                                                                        $('#nextStep').hide();
                                                                                        $('#submitSetDetails').attr('style','border-color: #FF0000; border-width:2px;');
                                                                                        $('#submitSetDetails').show();

                                                                                 } );
							

                                                } else if (frmname=='loginfrm') {
                                                        bodyArray=['#body', '#body'];
                                                        basketArray=['#cartbits','#cartbits'];
                                                        accountArray=['#account','#account'];
                                                        mainArray=[accountArray, basketArray, bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Logging in...',false);

						} else if (frmname == 'customerinfovat') {
                                                        bodyArray = ['#body', '#body'];
							mainArray=[bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Setting/Clearing VAT Number and updating cart details.',false);
                                                        $('#'+frmname).change(function() {
                                                                                        $('#nextStep').hide();
											$('#submitCustInfo').hide();
                                                                                        $('#submitValidateVAT').attr('style','border-color: #FF0000; border-width:2px;');

                                                                                 } );
						} else if (frmname == 'shippingcalccountry') {
                                                        bodyArray=['#body', '#body'];
                                                        mainArray=[bodyArray];
	                                                $('#countryid').linitx('onchange',mainArray,'#shippingcalccountry','Shipping Calculator','Recalculating shipping...');
                                                } else if (frmname == 'shippingcalcmethod') {
                                                        bodyArray=['#body', '#body'];
                                                        mainArray=[bodyArray];
                                                        $('#shippingid').linitx('onchange',mainArray,'#shippingcalcmethod','Shipping Calculator','Recalculating shipping...');
						} else if (frmname =='cartaddressselect') {
							bodyArray=['#body', '#body'];
                                                        mainArray=[bodyArray];
                                                        $('#'+frmname).linitx('onchange',mainArray,'#'+frmname,'Address Picker','Selecting address...');
							$('#sethint').css('display','none');
						} else if (frmname == 'frmpaymentaddressselect') {
                                                        bodyArray=['#body', '#body'];
                                                        mainArray=[bodyArray];
                                                        $('#'+frmname).linitx('onchange',mainArray,'#'+frmname,'Address Picker','Selecting address...');
                                                        $('#sethint').css('display','none');
						} else if (frmname == 'frmpaymentmethod') {
                                                        bodyArray=['#body', '#body'];
                                                        mainArray=[bodyArray];
                                                        $('#'+frmname).linitx('onchange',mainArray,'#'+frmname,'Payment Type','Changing payment method...');
                                                        $('#paymentsethint').css('display','none');
						} else if (frmname == 'customerinfo') {
							bodyArray =['#body','#body'];
                                                        mainArray=[bodyArray];
						 	$('#'+frmname).linitx('ajaxify',mainArray, 'Setting customer information.',false);

                                                        $('#'+frmname).change(function() {
                                                                                        $('#nextStep').hide();
                                                                                        $('#submitValidateVAT').hide();
                                                                                        $('#submitCustInfo').attr('style','border-color: #FF0000; border-width:2px;');

                                                                                 } );
						} else if (frmname == 'customerinfonote') {
                                                        bodyArray =['#body','#body'];
                                                        mainArray=[bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Setting comments.',false);
                                                } else if (frmname == 'cartshippingselection') {
							
                                                        var thisfrm = frmname;
		                                        $('#'+frmname+' [type=submit]').css('display','none');

							l = $('#'+frmname+' [type=radio]');
                                                        l.each(function() {
								var idn = $(this).attr('id');
								var ddn = idn.replace('shippingidi','shippingidt');

                                                                $('#'+idn).css('cursor','pointer');

								var bodyArray = ['#body', '#body'];
	                                                        var mainArray=[bodyArray];

								$('#'+idn).linitx('optionify',mainArray,'#'+thisfrm,'Shipping Selection','Selecting shipping method...');
								$('#'+ddn).css('cursor','pointer');
                                                                $('#'+ddn).unbind('click');

                                                                $('#'+ddn).bind('click',function() {
									$('#'+idn).attr('checked',true);
									$('#'+idn).trigger('click');

                                                                });
								
                                                        });


						} else if (frmname == 'frmdeliveryaddress') {
							bodyArray = ['#body', '#body'];
                                                        mainArray=[bodyArray];
														
	
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Setting delivery address.',false);

							$('#submitSetAddress').hide();
							$('#'+frmname).change(function() {
											$('#nextStep').hide();
											$('#submitSetAddress').attr('style','border-color: #FF0000; border-width:2px;');
				                                                        $('#submitSetAddress').show();

										 } );
						} else if (frmname == 'frmpaymentaddress') {
							bodyArray=['#body', '#body'];
                                                        mainArray=[bodyArray];

                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Setting payment address.',false);

                                                        $('#submitSetAddress').hide();
                                                        $('#'+frmname).change(function() {
                                                                                        $('#nextStep').hide();
                                                                                        $('#submitSetAddress').attr('style','border-color: #FF0000; border-width:2px;');
                                                                                        $('#submitSetAddress').show();

                                                                                 } );



						} else if (frmname == 'frmpaymentmethod') {
                                                        bodyArray=['#body', '#body'];
                                                        mainArray=[bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Setting payment method.',false);
						} else if (frmname == 'frmpaymentdetails') {
	                                                bodyArray=['#body', '#body'];
                                                        mainArray=[bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Setting card details.',false);
						} else if (frmname == 'cartaddressselect' || frmname == 'frmpaymentaddresssselect') {
                                                        bodyArray = ['#body', '#body'];
                                                        mainArray=[bodyArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Setting address details.',false);
						} else if (frmname == 'cartcopydelivery') {
                                                        shippingArray=['#shippingdetails', '#shippingdetails'];
                                                        paymentArray=['#paymentdetails', '#paymentdetails'];
                                                        cobbuttontop = ['#topcheckoutbutton','#topcheckoutbutton'];
                                                        cobbuttonbot = ['#bottomcheckoutbutton','#bottomcheckoutbutton'];
                                                        otArray = ['#ordertable', '#ordertable'];
                                                        mainArray=[shippingArray,paymentArray,otArray,cobbuttontop,cobbuttonbot];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Copying address details.',false);
						} else if (frmname == 'userdetailsform') {
                                                        shippingArray=['#shippingdetails', '#shippingdetails'];
                                                        paymentArray=['#paymentdetails', '#paymentdetails'];
                                                        cobbuttontop = ['#topcheckoutbutton','#topcheckoutbutton'];
                                                        cobbuttonbot = ['#bottomcheckoutbutton','#bottomcheckoutbutton'];
                                                        otArray = ['#ordertable', '#ordertable'];
							cdetailArray = ['#customerlogindetail','#customerlogindetail'];
                                                        detailArray =['#extrainfo','#extrainfo'];
                                                        mainArray=[cdetailArray, shippingArray,paymentArray,otArray,cobbuttontop,cobbuttonbot,detailArray];
                                                        $('#'+frmname).linitx('ajaxify',mainArray, 'Setting customer details.',false);
						}
					});
				}

				if ( ($(location).attr('href').indexOf('viewproduct.php') !=-1) || ($(location).attr('href').indexOf('/product/') !=-1)) {
					$("div .photo a").each(function(i) {
						$(this).bind('mouseover', function() {
								href = $(this).attr('href');
								title = $(this).attr('title');
								productImage = href.replace('_large.jpg','_product.jpg');
								relname = $(this).attr('rel');							
								
								$("div .product-image a img").attr('src',productImage);
								$("div .product-image a").attr('href',href);
								$("div .product-image a").attr('title',title);
								$("div .product-image a").unbind('click');
								$("div .product-image a").bind('click',function() {
										$('a[rel='+relname+']').eq(i).click();
										return false;
								 });
							}
						);
					});


					$("div .product-image a").bind('click',function() {
							relname =  $("div .photo a ").eq(0).attr('rel');
                                        		$('a[rel='+relname+']').eq(0).click();
                                                        return false;
                                               });
				}	

				$("div .zpagination a").each(function() {
                                        headerArray = ['#header','#header'];
					bodyArray = ['#body', '#body'];					
                                        pagArray = ['#footpag', '#footpag'];
                                        mainArray = [ headerArray, bodyArray, pagArray];

					$(this).linitx('linkify',mainArray,'Switching page...',false,false,'','Paging');
				});

				$("form").each(function() {
                                frmid = $(this).attr('id');
                        
                                if (frmid != undefined) {
                                        if (frmid.substring(0,10) == 'addtocart_') {
                                                var tpid=$('#'+frmid+' input[name="prodid"]').attr('value');

                                                if (tpid != undefined) {
                                                        $('#addtocart_'+tpid).submit(function() {
                                                                frm = $(this).attr('id'); 
                                                                ipid=$('#'+frm+' input[name=prodid]').attr('value');
                                                                $('#addtocart_'+ipid).linitx('addtocart');
                                                                return false;
                                                        });

                                                        $('#addtocart_'+tpid+' .buylink').click(function() {
                                                                $('#addtocart_'+tpid).linitx('addtocart');
                                                                return false;
                                                        });
                                                }
                                        } else if (frmid.substring(0,14) == 'productnotify_') {

						// change this when we implement the product notify
						// or Opera doesn't work [see above].
	
						tpid=$('#'+frmid+' input[name=prodid]').attr('value');
						if (tpid != undefined) {
							 $('#productnotify_'+tpid).submit(function() {
                                                                frm = $(this).attr('id');
                                                                ipid=$('#'+frm+' input[name=prodid]').attr('value');
                                                                $('#productnotify_'+ipid).linitx('addnotify');
								return false;
							});
						}
					} else if (frmid == 'zzzzzquickfind') {
						headerArray = ['#header','#header'];
                                                bodyArray = ['#body', '#body'];
                                                pagArray = ['#footpag', '#footpag'];
                                                mainArray = [ headerArray, bodyArray, pagArray];

                                                $('#quickfind').linitx('ajaxify',mainArray,'Searching for matches...',false,false,'','Searching',false,"/images/site/indicator.gif");
					
                                        } else if (frmid.substring(0,8) == 'currency') {
                                                bodyArray = ['#body', '#body'];
                                                headerArray=['#basket', '#basket'];
                                                finderArray=['#superquickfinder', '#superquickfinder'];
                                                mainArray=[headerArray, bodyArray];

                                                $('#'+frmid).linitx('ajaxify',mainArray, 'Updating prices...',false,false,'',"Changing Currency");
                                        } else if (frmid == 'showvatinc') {
                                                bodyArray = ['#body', '#body'];
                                                headerArray=['#basket', '#basket'];
                                                finderArray=['#superquickfinder', '#superquickfinder'];
                                                mainArray=[headerArray, bodyArray,finderArray];

                                                $('#showvatinc').linitx('ajaxify',mainArray, 'Including VAT in prices...',false,false,'',"VAT Inclusive Pricing",true);
                                        } else if (frmid  == 'showvatexc') {
                                                bodyArray = ['#body', '#body'];
                                                headerArray=['#basket', '#basket'];
                                                finderArray=['#superquickfinder', '#superquickfinder'];
                                                mainArray=[headerArray, bodyArray,finderArray];

                                                $('#showvatexc').linitx('ajaxify',mainArray, 'Excluding VAT in prices...',false,false,'',"VAT Exclusive Pricing",true);
                                        } else if (frmid  == 'quickfinder') {

						$('#brand').linitx('changesubmit',  '#'+frmid,"Brand", "Selecting brand...");
                                                $('#category').linitx('changesubmit', '#'+frmid,"Category", "Selecting category...");
                                                $('#price').linitx('changesubmit',  '#'+frmid, "Price", "Selecting price...");

					} else if (frmid == 'selectitemsppu') {
                                                $('#selectitemsppu').linitx('changesubmit',  '#'+frmid,"Page Size", "Selecting page size...");

                                        } else if (frmid == 'selectitemsppl') {
                                                $('#selectitemsppl').linitx('changesubmit',  '#'+frmid,"Page Size", "Selecting page size...");

                                        } else if (frmid == 'showlist') {
                                                bodyArray = ['#body', '#body'];
                                                mainArray=[ bodyArray];
						$('#showlist').linitx('ajaxify',mainArray, 'Switching to grid view...',false,false,'',"Display switch",true);
					} else if (frmid == 'showgrid') {
                                                bodyArray = ['#body', '#body'];
                                                mainArray=[bodyArray];
                                                $('#showgrid').linitx('ajaxify',mainArray, 'Switching to list view...',false,false,'',"Display switch",true);
					} else if (frmid == 'advancedSearchCat') {
						bodyArray = ['#body', '#body'];
						mainArray=[ bodyArray];
						 
						$('#searchCategory').linitx('onchange',mainArray,'#'+frmid,'Category Selection','Selecting category parameters...');
					} /*else if (frmid == 'advancedsearch') {
                                                bodyArray = ['#body', '#body'];
						mainArray=[ bodyArray];

                                                $('#advancedsearch :input').linitx('onchange',mainArray,'#'+frmid,'Filter Criteria','Selecting using criteria...');
*/

//					}
                                }

				el = document.getElementById('ltwitterbutton');
				if (el) {
					$('#ltwitterbutton').attr('class','twitter-share-button');
					$.getScript('http://platform.twitter.com/widgets.js');
				}

                        });

                        $("div.product-tabbed div.product-tabs span").unbind('click');
                        $("div.product-tabbed div.product-tabs span").click(function(){
				
				fid = $(this).attr('id');

				if (fid == 'description') {
					$(this).attr('class','active');
					$("div.product-tabbed div.product-tabs span#specification").attr('class','inactive');
                                        $("div.product-tabbed div.product-info div#specification_").attr('class','inactive');
                                        $("div.product-tabbed div.product-info div#description_").attr('class','active');

				} else {
                                        $(this).attr('class','active');
                                        $("div.product-tabbed div.product-tabs span#description").attr('class','inactive');
                                        $("div.product-tabbed div.product-info div#description_").attr('class','inactive');
                                        $("div.product-tabbed div.product-info div#specification_").attr('class','active');
				}

				return false;
                         });

                        //<?  if (basename($_SERVER['SCRIPT_NAME'],".php")=='viewproduct') {?>
                        $("a[rel='productimages1']").colorbox({transition:"none", preloading: "false", opacity :"0.6" });
                        $("a[rel='productimages2']").colorbox({transition:"none", preloading: "false", opacity :"0.6" }); 
                        //<? } ?>

			},
	   addnotify	: function () {
				var unique_id='';
                                fid = this.attr('id');
                                hfid = '#'+fid;

                                oldClass = $(hfid+' input[type=submit]').attr('class');


                                $(hfid+' input[name=action]').removeClass(oldClass);
                                $(hfid+' input[name=action]').addClass('disabledbutton');
                                $(hfid+' input[name=action]').attr('disabled','disabled');

				unique_id = $.gritter.add({title: "Adding notification", text:  "I'm adding a notification for you.", image: "/images/site/info.png", sticky: false, time: '' });

                                var values = $(hfid).serialize();

                                $.ajax({
                                        type: "POST",
                                        url: "/ajax_action.php",
                                        data: 'action=Add Notification&'+values,
                                        dataType: 'json',
                                        success: function(msg){

                                                $.gritter.remove(unique_id, { fade: true });

                                                $(hfid+' input[name=action]').removeClass('disabledbutton');
                                                $(hfid+' input[name=action]').addClass(oldClass);
                                                $(hfid+' input[name=action]').removeAttr('disabled');

                                                $.gritter.add({
                                                        title: msg.title,
                                                        text:  msg.text,
                                                        image: msg.icon,
                                                        sticky: false,
                                                        time: ''
                                                });
                                        },
                                        error: function(XMLHttpRequest, textStatus, errorThrown){
                                                $.gritter.remove(unique_id, { fade: true });

                                                $(hfid+' input[name=action]').removeClass('disabledbutton');
                                                $(hfid+' input[name=action]').addClass(oldClass);
                                                $(hfid+' input[name=action]').removeAttr('disabled');

                                                $.gritter.add({
                                                        title: 'Error adding item',
                                                        text:  "Could not add a notification to your account for this item. The reason returned was '"+XMLHttpRequest.status+': '+textStatus+"'. <br><br>To remove this message place your mouse over it and click the X in the top left corner.",
                                                        image: '/images/site/warning.png',
                                                        sticky: true,
                                                        time: ''
                                                });
                                        }
                                });

			},
	   addtocart	: 
			function () {
			       	var unique_id ='';

				fid = this.attr('id');
				hfid = '#'+fid;

			        if (isNaN($(hfid+' input[name=quantity]').val())) {
		        	        $.gritter.add({title: "User error", text:  "Only use numbers in the quantity input box", image: "/images/site/info.png", sticky: false, time: '' });
                			return false;
			        }

	        		if (parseInt($(hfid+' input[name=quantity]').val()) >=0) {

	        	        	var stock  = parseInt($(hfid+' input[name=stock]').val());
		                	var rstock = parseInt($(hfid+' input[name=quantity]').val());

			                unique_id = $.gritter.add({title: "Adding item", text:  "I'm adding this item to your shopping cart", image: "/images/site/info.png", sticky: false, time: '' }); 

        	        		if (stock < rstock) {
		                	        $.gritter.add({title: "Stock Warning", text:  "I don't have that many of this item in stock", image: "/images/site/warning.png", sticky: false, time: '8000' }); 
	                		}
			        } else {
        			        unique_id = $.gritter.add({title: "Removing item", text:  "I'm removing items from your shopping cart", image: "/images/site/info.png", sticky: false, time: '' }); 
			        }

			        $(hfid+' input[name=action]').removeClass('updatebutton');
		        	$(hfid+' input[name=action]').addClass('disabledbutton');
			        $(hfid+' input[name=action]').attr('disabled','disabled');

		        	var values = $(hfid).serialize();
        
			        $.ajax({
        			        type: "POST",
		                	url: "/ajax_action.php",
	        		        data: 'action=Add to Cart&'+values,
		        	        dataType: 'json',
                			success: function(msg,textStatus,XMLHttpRequest){

	                			$.gritter.remove(unique_id, { fade: true });
	
						rdb = (XMLHttpRequest.getResponseHeader('AJAX-Redirect') == '');
                                                rdn = (XMLHttpRequest.getResponseHeader('AJAX-Redirect') == null);

                                                if (!rdn && !rdb) {
	                                                $.gritter.remove(unique_id, { fade: true });
                                                        why = XMLHttpRequest.getResponseHeader('AJAX-Redirect-Reason');
                                                        $.gritter.add({title: "Redirecting", text:  why, image: "/images/site/redirect.png", sticky: true, time: '' });
                                                        window.location.href=XMLHttpRequest.getResponseHeader('AJAX-Redirect');
                                               } else {

			        		        $(msg.span).replaceWith(msg.update);
        				        	$(hfid+' input[name=action]').removeClass('disabledbutton');
	        		        		$(hfid+' input[name=action]').addClass('updatebutton');
			        	        	$(hfid+' input[name=action]').removeAttr('disabled');

				                	$.gritter.add({
        		        		        	title: msg.title,
	        			        	        text:  msg.text,
		        		        	        image: msg.icon,
	        			        	        sticky: false, 
        		                			time: ''
			                	        });
						}
	                		},
		        	        error: function(XMLHttpRequest, textStatus, errorThrown){
                		        	$.gritter.remove(unique_id, { fade: true });

			                        $(hfid+' input[name=action]').removeClass('disabledbutton');
        			                $(hfid+' input[name=action]').addClass('updatebutton');
                	        		$(hfid+' input[name=action]').removeAttr('disabled');

		                        	$.gritter.add({
                		                	title: 'Error adding item',
	                        		        text:  "Could not add the item to your shopping cart. The reason returned was '"+XMLHttpRequest.status+': '+textStatus+"'. <br><br>To remove this message place your mouse over it and click the X in the top left corner.",
		        	                        image: '/images/site/warning.png',
                			                sticky: true, 
                        			        time: ''
		                                });
		                	}	
               			});
			},
	   linkify      :		// for links
                        function (as,doing, dnc, radios, blinkEl,msgT) {

                                var sourceArr=as;
                                var dowhat=doing;

                                if (dnc == undefined) dnc = true;

                                if (radios == undefined) radios = true;

                                if (msgT == undefined || msgT == '') msgT = 'Processing';

				$(this).unbind('click');
 
				$(this).bind('click',function() {
                                        var unique_id = $.gritter.add({title: msgT, text:  dowhat , image: "/images/site/info.png", sticky: true, time: '' });
					var href = $(this).attr('href');

					$.ajax({type: 'GET',
                                                url: href,
                                                dataType: "html",
						success: function(data, textStatus, XMLHttpRequest) {
		                                        $.gritter.remove(unique_id, { fade: true });

							for (var i=0; i<sourceArr.length; i++) {

                                                        	source = sourceArr[i][0];
                                                                target = sourceArr[i][1];

                                                                if (target == '') {
                                                                	$(source).fadeOut(500, function() { $(source).remove(); });
                                                                } else {
									var $response=$(data);
                                                                        var ourdata = $response.find(source);
									//$(target).fadeOut(500, function() { $(target).replaceWith(ourdata); });
                                                                        $(target).replaceWith(ourdata);
                                                                }
							}

                                                        $(document).linitx('init');


						},
						error: function(XMLHttpRequest, textStatus, errorThrown) {
		                                        $.gritter.remove(unique_id, { fade: true });
							
							$.gritter.add({
                                                                        title: 'Error performing update',
                                                                        text:  "Could not update the page. The reason returned was '"+XMLHttpRequest.status+': '+textStatus+"'. <br><br>To remove this message place your mouse over it and click the X in the top left corner.",
                                                                        image: '/images/site/warning.png',
                                                                        sticky: true,
                                                                        time: ''
                                                        });
						}
					});

					return false;
				 });
			},
	   ajaxify	: 
			function (as,doing, dnc, radios, blinkEl,msgT,useLabel, alticon) {

	                        var sourceArr=as;
				var dowhat=doing;

				if (dnc == undefined) dnc = true;

				if (radios == undefined) radios = true;

				if (msgT == undefined || msgT == '') msgT = 'Processing';

				if (useLabel == undefined) useLabel=false;

				if (alticon == undefined || alticon =='') alticon='/images/site/info.png';

	                        var id = this.attr('id');
			
				if (useLabel) {
					l = $('#'+id+' label'); 
				} else
					l = $('#'+id+' [type=submit]');

				l.each(function() {

					var value ='';
					var bindVal = '';

					if (!useLabel) {
						value='"'+(this.value)+'"';
						bindVal = '[value='+value+']';
					} else { 
						//value='"'+(this.innerHTML)+'"';
                                                bindVal = 'label[for='+$(this).attr('for')+']';

					}

					$('#'+id+' '+bindVal).unbind('click');
                	                $('#'+id+' '+bindVal).bind('click',function() {

						$('#carterrortd1').removeClass('carterror');
                                                $('#carterrortd2').removeClass('carterror');
                                                $('#carterrorp1').empty().remove();
                                                $('#carterrorp2').empty().remove();

						$('#'+id).unbind('click');
						$('#'+id).bind('click', function() {

		                                action = $('#'+id).attr('action');
						// returns nodelist if not found

						if (action == undefined || action.length == undefined || action.length ==0 || {}.toString.call(action) == '[object NodeList]' || action.substr(0,1) == '#') {
							action='/ajax_action.php';
						}
	
        	       		                values = $(this).serialize();
						values = values +'&ajax=true';

						var unique_id='';
	
        	                       		cmd = $('#'+id+' [value='+value+']').val();
	        	                        nme = $('#'+id+' [value='+value+']').attr('name');
						ajax= '&ajax=true';

						if (dnc) {
                        	                	$('#'+id+' input[type=submit], #'+id+'[value='+value+']').attr('disabled','disabled');

							oldClass = $('#'+id+' input[type=submit], #'+id+'[value='+value+']').attr('class');

		                                        $('#'+id+' input[type=submit], #'+id+'[value='+value+']').removeClass(oldClass);
        		                                $('#'+id+' input[type=submit], #'+id+'[value='+value+']').addClass('disabledbutton');
						}

						meth= $('#'+id).attr('method');

						if (meth == '')
							meth='POST';

						sendmethod = meth.toUpperCase();
						
	                                        unique_id = $.gritter.add({title: msgT, text:  dowhat , image: alticon, sticky: true, time: '' });

		                                $.ajax({
               			                        type: sendmethod,
                        	       		        url: action,
							dataType: "html",
	                                	        data: nme+'='+cmd+'&'+values,
               		                        	success: function(data, textStatus, XMLHttpRequest) {
								rdb = (XMLHttpRequest.getResponseHeader('AJAX-Redirect') == '');
								rdn = (XMLHttpRequest.getResponseHeader('AJAX-Redirect') == null);

								if (!rdn && !rdb) {
									$.gritter.remove(unique_id, { fade: true });
									why = XMLHttpRequest.getResponseHeader('AJAX-Redirect-Reason');
									$.gritter.add({title: "Redirecting", text:  why, image: "/images/site/redirect.png", sticky: true, time: '' });
									window.location.href=XMLHttpRequest.getResponseHeader('AJAX-Redirect');

								} else {

									for (var i=0; i<sourceArr.length; i++) {
							
										source = sourceArr[i][0];
        	                                	                        var target = sourceArr[i][1];

										if (target == '') {
											 $(source).fadeOut(500, function() { $(source).remove(); });
										} else {
	
	        	                                	                	var $response=$(data);
	        	                	                       			var ourdata = $response.find(source);

											$(target).fadeTo(5,1); 
											$(target).replaceWith(ourdata);
											//$(target).fadeTo(5,1); 


										}
									}

                                	        	                $.gritter.remove(unique_id, { fade: true });

									if (dnc) {
			        	                                        $('#'+id+' input[type=submit]').removeAttr('disabled');
					                                        $('#'+id+' input[type=submit]').removeClass('disabledbutton');
                					                        $('#'+id+' input[type=submit]').addClass(oldClass);
									}

									$(document).linitx('init');
								}
                                	       		},
							error: function(XMLHttpRequest, textStatus, errorThrown) {
	
	                                        	        $.gritter.remove(unique_id, { fade: true });

								$.gritter.add({
                                                        		title: 'Error performing update',
		                                                        text:  "Could not update the page. The reason returned was '"+XMLHttpRequest.status+': '+textStatus+"'. <br><br>To remove this message place your mouse over it and click the X in the top left corner.",
                		                                        image: '/images/site/warning.png',
                                		                        sticky: true,
                                                		        time: ''
                                                		});
			
						
								if (dnc) {
									$('#'+id+' input[type=submit]').removeAttr('disabled');
        			                                        $('#'+id+' input[type=submit]').removeClass('disabledbutton');
	                	                                        $('#'+id+' input[type=submit]').addClass(oldClass);
								}
							}
							
		                               	});
	                                	return false;
        	               			});
					});
				});

				if (radios && blinkEl != undefined)  {
				r = $('#'+id+' [type=radio]');

                	               r.each(function() {
        	                               var value='"'+(this.value)+'"';
	                                       $('#'+id+' [value='+value+']').bind('click', function() {
							$('#'+id+' [value="'+blinkEl+'"]').animate({ border: "2px solid #000000"},400).animate({ border: "2px solid #FF0000"},400).animate({ border: "2px solid #000000"},400).animate({ border: "2px solid #FF0000"},400).animate({ border: "2px solid #000000"},400).animate({ border: "2px solid #FF0000"},400).animate({ border: "2px solid #000000"},400).animate({ border: "2px solid #FF0000"},400).animate({ border: "2px solid #000000"},400).animate({ border: "2px solid #FF0000"},400).animate({ border: "2px solid #000000"},400).animate({ border: "2px solid #FF0000"},400) ;


						});
                                	});
				}
			  },

	   quickfind	: function () {

			},
	   optionify    : function(qfArray,frm,title,message,hide) {

				var sourceArr=qfArray;
				var form = frm;

                                if (hide == undefined)
                                        hide=true;

                                if ((this).attr('hook') != undefined)
                                        return false;

/*                                if (hide) {
                                        $(form+' [type=submit]').css('display','none');

                                        l = $(form+' label');
                                        l.each(function() {
                                                btn = $(form+' #'+$(this).attr('for'));
                                                if ($(btn).attr('type') == 'submit') {
                                                        $(this).css('display','none');
                                                }
                                        });
                                }

*/
                                this.attr('hook','attached');

                                $(this).bind('click',function() {

                                        value = '"'+$(form+' [type=submit]').attr('value')+'"';

                                        if (value == undefined) {

                                                l = $(form+' label');

                                                l.each(function() {
                                                        subm = $(form+' #'+$(this).attr('for'));

                                                        if ($(subm).attr('type') == 'submit') {
                                                                value = '"'+$(subm).attr('value')+'"';

                                                        }

                                                });
                                        }

                                        cmd = $(form+' [value='+value+']').val();
                                        nme = $(form+' [value='+value+']').attr('name');
                                        values = $(form).serialize();

                                        action = $(form).attr('action');
                                        
					if (action.length == undefined || action.length ==0 ||  {}.toString.call(action) == '[object NodeList]'  || action.substr(0,1) == '#') {
                                               action='/ajax_action.php';
                                        }

                                        meth= $(form).attr('method');

                                        if (meth == '')
                                                meth='POST';

                                        sendmethod = meth.toUpperCase();
                                        unique_id = $.gritter.add({title: title,  text:  message, image: "/images/site/indicator.gif", sticky: false, time: '' });

                                        $.ajax({
                                                type: sendmethod,
                                                url: action,
                                                dataType: "html",
                                                data: nme+'='+cmd+'&'+values,
                                                success: function(data, textStatus, XMLHttpRequest) {
                                                        for (var i=0; i<sourceArr.length; i++) {
                                                                source = sourceArr[i][0];
                                                                target = sourceArr[i][1];

                                                                if (target == '') {
                                                                        $(source).fadeOut(500, function() { $(source).remove(); });
                                                                } else {
                                                                        var $response=$(data);
                                                                        var ourdata = $response.find(source);
                                                                        $(target).replaceWith(ourdata);
                                                                }
                                                         }

                                                         $.gritter.remove(unique_id, { fade: true });
                                                         $(document).linitx('init');
                                                },
                                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                                        $.gritter.remove(unique_id, { fade: true });
                                                        $.gritter.add({ title: 'Error performing update',
                                                                        text:  "Could not update the page. The reason returned was '"+XMLHttpRequest.status+': '+textStatus+"'. <br><br>To remove this message place your mouse over it and click the X in the top left corner.",
                                                                        image: '/images/site/warning.png',
                                                                        sticky: true,
                                                                        time: ''
                                                                });
                                                }
                                        });

                                        return false;
                                });

			},
	   changesubmit : function(form,title,message) {

				if ((this).attr('hook') != undefined)
                                        return false;
				this.attr('hook','attached');

				$(form+' [type=submit]').css('display','none');
				
                                $(this).bind('change',function() {
					unique_id = $.gritter.add({title: title,  text:  message, image: "/images/site/indicator.gif", sticky: false, time: '' });
					$(form).submit();
				});
			},
	   onchange     : function(qfArray,form,title,message,hide) {
			
				var sourceArr=qfArray;

				if (hide == undefined)
					hide=true;

				if ((this).attr('hook') != undefined)
					return false;

				if (hide) {
                                	$(form+' [type=submit]').css('display','none');

                        	        l = $(form+' label');
					l.each(function() {
						btn = $(form+' #'+$(this).attr('for'));
						if ($(btn).attr('type') == 'submit') {
                                        		$(this).css('display','none');
						}
					});
				}
				this.attr('hook','attached');

				$(this).bind('change',function() {
					value = $(form+' [type=submit]').value;

                                        value = '"'+$(form+' [type=submit]').attr('value')+'"';


					if (value == undefined) {
					
						l = $(form+' label');
	
 						l.each(function() {
							subm = $(form+' #'+$(this).attr('for'));

							if ($(subm).attr('type') == 'submit') {
								value = '"'+$(subm).attr('value')+'"';
							}
						});
					}

                                        cmd = $(form+' [value='+value+']').val();
                                        nme = $(form+' [value='+value+']').attr('name');

					values = $(form).serialize();
                                        action = $(form).attr('action');
                                        if (action.length == undefined || action.length ==0  || { }.toString.call(action) == '[object NodeList]' || action.substr(0,1) == '#') {
 	                                       action='/ajax_action.php';
                                        }

                                        meth= $(form).attr('method');

					if (meth == '')
                                        	meth='POST';

                                        sendmethod = meth.toUpperCase();
                                        unique_id = $.gritter.add({title: title,  text:  message, image: "/images/site/indicator.gif", sticky: false, time: '' });

					$.ajax({
                                        	type: sendmethod,
                                                url: action,
                                                dataType: "html",
                                                data: nme+'='+cmd+'&'+values ,
                                                success: function(data, textStatus, XMLHttpRequest) {
							
							for (var i=0; i<sourceArr.length; i++) {
	                                                        source = sourceArr[i][0];
                                                                target = sourceArr[i][1];

                                                                if (target == '') {
        	                                                        $(source).fadeOut(500, function() { $(source).remove(); });
                                                                } else {
									//$(target).fadeOut(500);
                	                                                var $response=$(data);
                                                                        var ourdata = $response.find(source);

                                                                        $(target).replaceWith(ourdata);
									//$(target).fadeIn(500);
                                                                }
                                                         }

                                                         $.gritter.remove(unique_id, { fade: true });
                                                         $(document).linitx('init');
						},
						error: function(XMLHttpRequest, textStatus, errorThrown) {

                                                	$.gritter.remove(unique_id, { fade: true });

                                                        $.gritter.add({ title: 'Error performing update',
                                                                        text:  "Could not update the page. The reason returned was '"+XMLHttpRequest.status+': '+textStatus+"'. <br><br>To remove this message place your mouse over it and click the X in the top left corner.",
                                                                        image: '/images/site/warning.png',
                                                                        sticky: true,
                                                                        time: ''
                                                                });
						}
					});
					return false;
				});
			}
	};

       $.fn.linitx = function(method) {

		// Method calling logic
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.linitx' );
	    	}
	};

})(jQuery);


