/*

	Script provides AJAX functions

*/
	/*

    Function creates HttpObject according to browser

    Arguments:
    	none

    */
	function CreateHttpObject()
	{
		var obj = null;
		if(window.XMLHttpRequest)
		{
			try {obj = new XMLHttpRequest();} catch(e){}
		}
        else if(window.ActiveXObject)
        {
            try {obj = new ActiveXObject("Microsoft.XMLHTTP");} catch(e){}
            if(!obj)
            	try {obj = new ActiveXObject("Msxml2.XMLHTTP");} catch (e){}
        }
        return obj;
	}
    /*

    Function creates httpRequest object and sends request to server

    Arguments:
    	url - requested url

    */
    function loadXMLDoc(url) {
    	// show Wait Window
		ShowWaitWindow();
    	httpRequest = CreateHttpObject();
        httpRequest.onreadystatechange = function (){processReqChange(httpRequest)}
        httpRequest.open("GET", url, true);
        httpRequest.send(null);
	}
    /*

    Function proccesses server's response

    Arguments:
    	httpRequest - httpRequest object

    */
	function processReqChange(httpRequest){
	            if (httpRequest.readyState == 4) {
	                if (httpRequest.status == 200) {
	                    if (httpRequest.responseXML.documentElement){
	                        var oResponse = httpRequest.responseXML.documentElement;
                            if (oResponse.getElementsByTagName("destination").length>0){
                                for (var i=0;i<oResponse.getElementsByTagName("destination").length; i++){
                                    var sDiv = oResponse.getElementsByTagName("destination")[i].firstChild.data;
                                    if (sDiv.length>0){
                                    			// if tag name is "redirect" performs javascript redirect according "html_code" tag value
	                                            if (sDiv=="redirect"){
	                                               if (window.XMLSerializer){
		                                               s=new XMLSerializer();
	                                               		window.location = s.serializeToString(oResponse.getElementsByTagName("html_code")[i].firstChild);
	                                               }else{
	                                               		window.location = oResponse.getElementsByTagName("html_code")[i].text;
	                                               }
	                                                break;
	                                            }else{
	                                                var oDiv = document.getElementById(sDiv);
	                                                if (oDiv){
                                                    	// Firefox
	                                                    if (window.XMLSerializer){
	                                                        s=new XMLSerializer();
	                                                        oDiv.innerHTML = s.serializeToString(oResponse.getElementsByTagName("html_code")[i].firstChild);
	                                                    }else{
                                                        // IE
	                                                        oDiv.innerHTML = oResponse.getElementsByTagName("html_code")[i].firstChild.xml;
	                                                    }
	                                                }
                                                }
                                    }
                                }
                            }
	                    }else{
	                        alert("There was a problem retrieving the XML data!");

	            		}
						CloseWaitWindow();
	                } else {
	                    alert("There was a problem retrieving the XML data!");
	                }
	            }
	        };
	/*

    Function shows Wait Window at the top left corner of the window

    Arguments:
    	none

    */
	function ShowWaitWindow()
	{
	    CloseWaitWindow();

	    var div = document.body.appendChild(document.createElement("DIV"));
	    div.id = "wait_window_div";
	    div.innerHTML = "Processing";
	    div.className = "waitwindow";
	    div.style.left = document.body.scrollLeft + (document.body.clientWidth - div.offsetWidth) - 5 + "px";
	    div.style.top = document.body.scrollTop + 5 + "px";

	    if(document.attachEvent && !(navigator.userAgent.toLowerCase().indexOf('opera') != -1))
	    {
	        var frame = document.createElement("IFRAME");
	        frame.src = "javascript:void(0)";
	        frame.id = "wait_window_frame";
	        frame.className = "waitwindow";
	        frame.style.width = div.offsetWidth + "px";
	        frame.style.height = div.offsetHeight + "px";
	        frame.style.left = div.style.left;
	        frame.style.top = div.style.top;
	        document.body.appendChild(frame);
	    }
	}
    /*

    Function closes Wait Window

    Arguments:
    	none

    */
	function CloseWaitWindow()
	{
	    var frame = document.getElementById("wait_window_frame");
	    if(frame)
	        frame.parentNode.removeChild(frame);

	    var div = document.getElementById("wait_window_div");
	    if(div)
	        div.parentNode.removeChild(div);
	}
	
	
	
	
	
	//Mobile eye update on page load
	
	$(function(){		
		//$('marquee').marquee();		
		
		if(typeof $.fn.prettyPhoto != "undefined")
			$("a[rel^='prettyPhoto']").prettyPhoto();
		
		$('ul#imagetransition').innerfade(
		{
			speed: 		500,
			timeout: 	4000,
			type: 		'random',
			containerheight: '295px'
		});		
		
		
		

		$("body #benefits ul li:last-child, body #benefitswide ul li:last-child").css("border-bottom", "0");
		
		
		
		//serach
		/*
		var select = "<div id='search'><select id='level1'>" +
					"<option value=''>Select vehicle type...</option>" +
					 	"<option value='hgv'>HGV</option>" + 
						"<option value='van'>Van</option>" + 
					"</select>" +
					"<select id='level2' style='display:none;'>" +
					"<option value='' class='firstopt'>Select category...</option>" +
					"<option value='security'>Security</option>" +
					"<option value='safetey'>Saftey</option>" +
					"<option value='performance'>Performance</option>" +
					"</select></div>";
					
		$("#login").prepend(select);
		
		
		
		$("#search select").bind("change", function() {
			switch($(this).val()) {
				case "hgv":
				case "van":
					//$("#level1").hide();					
					$("#level2").fadeIn();		
					$("#level2 option").eq(0).select();
					//wait for user to select next level
					break;				
				
				case "security":
				case "safetey":
				case "performance":						
					//$("#level1").show();					
					//$("#level2").hide();
				
					//search...
					
					window.location = "/search/" + $("#level1").val() + "/" + $("#level2").val();
				
					break;
				
				
			}			
		});
		*/
		
		
		
		
		
		
	});
