var base = "/ds-v2/";

var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();

var feedback_box = '<div id="site-feedback"><img id="site-feedback-tab" src="'+base+'images/feedback.png"><h3 class="no-top-margin no-top-padding">Leave Site Feedback</h3><p>Whether it\'s something you really like or really hate, we want to know about it. Let us know what you think to help us build a better site.</p><div class="clear"></div><textarea id="feedback-textarea" class="text" style="margin-top:6px;width:542px;height:120px;"></textarea><div class="white-submit" id="cancel-send-site-feedback" style="float:left;margin:15px 0 0 10px;"><a>Cancel</a></div><div class="green-submit" id="send-site-feedback" style="float:right;margin:15px 10px 0 0;"><a>Send Feedback</a></div></div>'

$(function() {
	$('#ebay-get-quotes').click(function(){
		$('#new-ebay-delivery').submit();
	});
	
	$('.rollover').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	}, function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	});

	$("a.logo img").hover(function(){
		$(this).attr("src", base+"images/delivery-supermarket-logo-hover.png");
	},function(){
		$(this).attr("src", base+"images/delivery-supermarket-logo.png");
	});
	
	$("#list-your-shipment").hover(function(){
		$(this).attr('src', base+'images/list-your-shipment-hover.png');
	}, function(){
		$(this).attr('src', base+'images/list-your-shipment.png');
	});
	
	$("#ebay-get-quotes").hover(function(){
		$(this).attr('src', base+'images/ebay-get-quotes-over.png');
	}, function(){
		$(this).attr('src', base+'images/ebay-get-quotes.png');
	});
	
	$("body").append(feedback_box);
	
	$("#site-feedback-tab").click(function(){
		var new_position = ($(window).width()-590)/2;
		$("#site-feedback").css({'z-index': 999}).animate({"right":new_position}, 300);
		$("#mask").fadeTo(300, 0.4);
	});
	
	$("#cancel-send-site-feedback").click(function(){
		$("#site-feedback").animate({"right":"-590px"}, 300);
		$("#mask").fadeOut(300);
		setTimeout(function(){
			$("#site-feedback").css({'z-index': 997});
		}, 300);
	});
	
	$("#send-site-feedback").click(function(){
		
		if (!$(this).hasClass("disabled")){
			$(this).addClass("disabled");
			$("#cancel-send-site-feedback").hide();
			
			$("#site-feedback p").html("<img src=\""+base+"images/white-ajax-loader.gif\" style=\"float:left;margin:0 8px 0 0;\"> Please hold on while we send your message.<br>&nbsp;");
			
			$.post(base+'ajax/contact-form.php?action=feedback', {'message':$("#feedback-textarea").val()}, function(data){
			
				if (data.success == 1){
					setTimeout(function(){
						$("#site-feedback p").html("<img src=\""+base+"images/tick.png\" style=\"float:left;margin:0 8px 0 0;\"> Your message has been sent. Thanks for the feedback!<br>&nbsp;");	
					}, 500);
				}
				
				setTimeout(function(){
					$("#mask").fadeOut(300);
					$("#site-feedback").animate({"right":"-590px"}, 300, function(){
						$("#site-feedback p").html("Whether it\'s something you really like or really hate, we want to know about it. Let us know what you think to help us build a better site.");
						$("#feedback-textarea").val("");
						$("#send-site-feedback").removeClass("disabled");
						$("#cancel-send-site-feedback").show();
					});
				}, 2500);
			
			}, 'json');
		}
	});
	
	
});


function ds_select_from_array(array, id, checked, className, style){

	if (!id){alert('ID is required please.');}
	if (!className){className = 'position-regular';}
	
	var output = '<select class="'+className+'" id="'+id+'" name="'+id+'"';
	if (style){output += ' style="'+style+'" ';}
	output += '>';
	
	$.each(array, function(index, value){

		// We should have 2 parts, an 'id' and a 'name'.
		output += '<option value="'+value.id+'"';
		if (value.id == checked){output += ' selected';}
		output += '>'+value.name+'</option>';

	});
	
	output += '</select>';
	
	return output;
}

// fix checkboxes for IE
$(function() {
	if ($.browser.msie) {
		$('input:checkbox').click(function () {
			this.blur();
			this.focus();
			});
		}
	});

function preload(image_array)
	{
    $(image_array).each(function()
    	{
        $('<img/>')[0].src = this;
        });
	}

// preload the images that are used all over the place
preload([
	base+'images/social/facebook_hover.png',
	base+'images/social/twitter_hover.png',
	base+'images/social/linkedin_hover.png',
	base+'images/social/digg_hover.png',
	base+'images/social/delicious_hover.png',
	base+'images/tick.png',
	base+'images/cross.png',
	base+'images/delivery-supermarket-logo-hover.png',
	base+'images/list-your-shipment-hover.png',
	base+'images/ebay-get-quotes-over.png'
	]);


function isset(variable){
	return typeof(variable) != "undefined" && variable !== null;
}
