/**
 * 
 */

	/*$(function() {
		$('#myOrderContainer').mouseover(function() {
			
			setBasketDestination('#userNavigation #myOrder');
			setBasketAnimation('slide');
			getBasket();
			
			$(getBasketElement()).css({
				position:'absolute',
				right:'0px',
				top:'20px',
				background:'#ffffff'
			});
			
			setBasketDestination('#domain-search-results');
			
			return false;
		});
		
		$('#myOrderContainer').mouseout(function() {
			closeBasket();
		})
	});*/
	
	$(function() {
		
		// Finds all links with rel="external" and opens in new window when clicked
		$('a[rel*=external]').click( function() {
			window.open(this.href);
			return false;
		}); 
		
		// Sitemap Toggle
		$('#sitemap-toggle-container span').click(function() {
  			
			$('#sitemap-container').slideToggle('slow', function() {
				$('#sitemap-toggle-container span').hasClass('open') ? $('#sitemap-toggle-container span').removeClass('open') : $('#sitemap-toggle-container span').addClass('open') ;
			});
		});
		
		if ($.browser.msie && parseInt($.browser.version) < 9){
			// here goes some code for all Internet Explorer versions older than 9 
		}
		
		else {
			$('#wrapper').wrap( '<div class="negetive-fill" />');
		}
		
		
		$('.popUpWindow').click(function() {
			
			window.open($(this).attr('href'), "myWindow", "height = 500, width = 620, resizable = 0, scrollbars=1");
			
			return false;
			
		});
		
		!! $("#group1").length && setInterval("swapImages()", 12000);	
		
		$('.textBoxClear').live('click', function() {
			if($(this).val() == $(this).attr('rel')) {
				$(this).val('');
			}
		});
		
		$('.textBoxClear').live('blur', function() {
			$(this).val( $(this).val() == '' ? $(this).attr('rel') : $(this).val() );
		});		
		
		if($('.tab-container').length > 0) {
			$('.tab-container').siteopiaTabs();
		}
		
		if($('.logoutButton').length > 0) {
			$('.logoutButton').click(function() {
				var button = this;
				
				$.ajax({
					type:		'post',
					dataType:	'json',
					url:		'/default/index/logout',
					success:	function(r) {
						if(r!=null) {
							if(r == 'success') {
								$(window).trigger('user.loggedOut');
								$('#loggedInAs').fadeOut(300);
								$(button).fadeOut(500, function() {
									$('#loggedInAs').html('You have successfully logged out!').addClass('loggedOut');
									$('#loggedInAs').fadeIn(500);
									
									setTimeout(function() {
										$('#loggedInAs').fadeOut(500,function() {
											$(this).remove();
											$(button).remove();
										});
										
									},2400);
								});
							} else {
								var loggedInAsHTML = $('#loggedInAs').html();
								$('#loggedInAs').fadeOut(500,function() {
									$(this).html('An error occurred whilst trying to log out!').addClass('errorLoggedOut');
									$(this).fadeIn(500);
									setTimeout(function() {
										$('#loggedInAs').fadeOut(500,function() {
											$(this).html(loggedInAsHTML);
											$(this).fadeIn(500);
										});
										
									},2400,loggedInAsHTML);
								});
								
							}
						}
					}
				});
				return false;
			});
		}
		
		/* SLider JS Goes Here */
		
		if($('.slider-wrapper').length) {
			$('.slider-wrapper').vtBanners();
		}
		
		if($('.testimonial').length > 1) {
			$('.testimonial:nth(0)').addClass('current');

			setInterval(function() {
				var $current = $('.testimonial.current');
				var $nextTestimonial = $('.testimonial.current').next('.testimonial');
				if(!$($nextTestimonial).length) {
					$nextTestimonial = $('.testimonial:nth(0)');
				}
				$('blockquote',$current).fadeOut(400,function() {
					$current.fadeOut(400, function() {
						$current.removeClass('current').hide();
						$('img',$nextTestimonial).hide();
						$('blockquote',$nextTestimonial).show();
						$nextTestimonial.addClass('current').fadeIn(400,function() {
							$('img',$nextTestimonial).fadeIn(400);
						});
					});
				});
				
			},6000);
		}
		
		

		/* On Home Page Makes Services Click-able 
		$("#servicesContainer div.service, li.dir, li.dir ul.stage2 li").click(function(){
			window.location=$('a',this).attr("href"); return false;
		});
		
		/* Adds hover effect on services to IE 
		$("#servicesContainer .service").hover(function() {
     		$(this).addClass("hover");
   		},function(){
     		$(this).removeClass("hover");
   		});
		*/
		
		
		// This removes the default input value and replaces it if eu doesnt enter anything
    	$('input#search2[type="text"], #international-domain-search #idsearch').focus(function() {
    	if (this.value == this.defaultValue){ 
    			this.value = '';
			}
			if(this.value != this.defaultValue){
	    		this.select();
	    	}
   	 	});
	
	 	$('input#search2[type="text"]').blur(function() {		
    		if ($.trim(this.value) == ''){
				this.value = (this.defaultValue ? this.defaultValue : '');
			}
     	});
		
	});
	
	function swapImages() {
		if( $("#group1").hasClass('active') ) {
			 $("#group1").fadeOut(2000, function() { $(this).removeClass('active'); });
			 $("#group2").fadeIn(2000);
		}else{
			 $("#group2").fadeOut(2000);
			 $("#group1").fadeIn(2000, function() { $(this).addClass('active'); });
		}
	}	
	
	function doBasketScroller() {
		
	}
	
	function getAddressListByPostCode(postCode,url) {
		var ajaxUrl = '/default/index/get-address-list';
		if(url !=null) {
			ajaxUrl = url;
		}
		
		var addresses = null;
		if(postCode) {
			$.ajax({
				type:		'post',
				dataType:	'json',
				data:		{search:postCode},
				async:		false,
				url:		ajaxUrl,
				success:	function(r) {
					addresses = r;
				}
			});
			return addresses;
		}
	}
	
	function getAddressById(id,url) {
		var address = null;
		var ajaxUrl = '/default/index/get-address';
		if(url !=null) {
			ajaxUrl = url;
		}
		if(id) {
			$.ajax({
				type:		'post',
				dataType:	'json',
				data:		{id:id},
				async:		false,
				url:		ajaxUrl,
				success:	function(r) {
					address = r;
				}
			});
		}
		
		return address;
	}
	
	function getLoaderImage(loader,html) {
		var path = '/images/siteopia/loaders/';
		switch(loader) {
			case 'green':
				var image = path + 'greenLoader.gif';
				break;
			case 'grey':
				var image = path + 'greyLoader.gif';
				break;
			case 'pleaseWait':
				var image = path + 'pleaseWait.gif';	
				break;
			case 'largePleaseWait':
				var image = path + 'largePleaseWait.gif';
				break;
			case 'searching':
				var image = path + 'searching.gif';
				break;
			case 'white':
				var image = path + 'whiteLoader.gif';
				break;
			case 'blue':
				var image = path + 'blueLoader.gif';
				break;
			case 'miniBasketLoader':
				var image = path + 'miniBasketLoader.gif';
				break;
			case 'greyOnWhiteLoader':
				var image = path + 'greyOnWhiteLoader.gif';
				break;
			case 'largeAddToBasket':
				var image = path + 'largeAddingToBasket.gif';
				break;
		}
		
		if(html) {
			return '<img src="'+image+'" />';
		} else {
			return image;
		}
	}
	
	function iFramePopper() {
    	var wrapper = document.getElementById('wrapper');
    	wrapper.style.display = "none";
    	if (top.location != self.location) { 
        	top.location.replace(self.location);
        } else {
        	var wrapper = document.getElementById('wrapper');
        	wrapper.style.display = "block";
            var content = getElementById('complete');
            content.style.display = "block";
        }
	}
	
	jQuery.fn.siteopiaTabs = function() {
		var thisElement = this;
		var activeClass = 'activeTab';
		
		$('.tab-content',thisElement).hide();
		$('.tab-content:nth(0)',thisElement).show();
		$('.tabs li a',thisElement).removeClass(activeClass);
		$('.tabs li a:nth(0)',thisElement).addClass(activeClass);
		
		$('.tabs li a',thisElement).click(function() {
			$('.tabs li a',thisElement).removeClass(activeClass);
			$(this).addClass(activeClass);
			$('.tab-content',thisElement).hide();
			$($(this).attr('href'),thisElement).show();
			
			return false;
		});
		
		return this;
	}
	
	jQuery.fn.center = function (position) {
		if(position) {
			this.css("position",position);
		} else {
			this.css("position","absolute");
		}
		
		if(position == 'fixed') {
			this.css("top", ( $(window).height()/2 - this.height()/2 ) + "px");
		    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		} else {
		    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
		    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		}
		
	    return this;
	}
	
    $.fn.clearForm = function() {
        return this.each(function() {
          var type = this.type, tag = this.tagName.toLowerCase();
          if (tag == 'form')
            return $(':input',this).clearForm();
          if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
          else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
          else if (tag == 'select')
            this.selectedIndex = -1;
        });
      };


	
