// JavaScript Document
$(document).ready(function() {
	externalLinks();
	$('#dialog').dialog({
			autoOpen:false,
			modal:true,
			closeOnEscape: true,
			width: 600,
			height: 410			
		});
	$('#bottomnav a.dialog,#tsandcs a.dialog,#freedelivery a.dialog').each(function(){
		this.href = (this.href).replace(/spages/,"ispages");	
	});
	$('.required').focus(function(){clearDefault(this);});
	$('#productlist li').css({cursor:"pointer"});
	$('#productlist li a').click(function(){
		window.location= $(this).attr('href');
		return false;
	});
	$('#productlist li').click(function(){
		window.location= $(this).children('a').attr('href');
	});
	
	$('a.dialog').click(function(){
		// if class = dwide then make dialog box wider
		var href = $(this).attr('href');
		var dclass = $(this).attr('class');
		$('#dialog').dialog('option', 'title', $(this).attr('title'));
		if(dclass.indexOf('dwide') != -1){$('#dialog').dialog('option', 'width', 800);$('#dialog').dialog('option', 'height', 450);}		
		if(href.indexOf('#') == 0){
			$('#dialog').html($(href).html());	}
		else if(href.indexOf('http:') == 0){
			$('#dialog').dialog('option', 'width', 700);
			$('#dialog').dialog('option', 'height', 650);
			$('#dialog').html("<" + "iframe src='" + href + "'" + " frameborder='0'><" + "/iframe" + ">");
		}
		else{
			$('#dialog').load(href);
		}
		$('#dialog').dialog('open');
		$('#dialog input:first').focus();
		$('#dialog .focus').focus();
		return false;
	});
	$('#subForm').submit(function (e) {
            e.preventDefault();
            $.getJSON(
            this.action + "?callback=?",
            $(this).serialize(),
            function (data) {
                if (data.Status === 400) {
                    alert("Error: " + data.Message);
                } else { // 200
                    alert("Success: " + data.Message);
                }
            });
        });
	$('#sortspecial').change(function(){
		$.post('/library/elxr/setting-post.asp',{ sortspecial: $('#sortspecial').val()});
		window.location.reload();
	});
});

function clearDefault(el) {
	var	input =  $(el).val()
  	if (input =="Required" | input =="Search") {$(el).val("")}
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
 }

