//  ---------------------------------------------------------------------------
//	variables
//  ---------------------------------------------------------------------------

    var iCounterTimer    = 0;      //  timeout length for counter
    var currentSelect    = null;   //  current select element being changed
    var isCounterRunning = false;  //  
    var counterTimer     = null;



//  ---------------------------------------------------------------------------
//	video popup
//  ---------------------------------------------------------------------------

    function videoPopup(e) {
	    
	    e.preventDefault();
		
	    if (oOverlay == null) {
		    var oOverlay = document.createElement("div");
		    oOverlay.id = "ScreenOverlay";
		    $("#PageContent").append(oOverlay)
	    }
		
	    if ($.browser.msie) {
		    $(oOverlay).css({height: $(document.body).height() + "px", width: $(document.body).width() + "px"});
		    $("select").css("visibility", "hidden");
	    } else {
	        $(oOverlay).css("position", "fixed");
	        $("#Header_Homepage").hide();
	    }
		
	    $(oOverlay).show().click(function(){
	        $("#VideoPlayer").css("visibility", "hidden");
	        $(this).hide();
	        $("#Header_Homepage").show();
	        if ($.browser.msie) {
	            $("select").css("visibility", "visible");
	        }
	    });
       
        var posX = parseInt((iClientInnerWidth - $("#VideoPlayer").width()) / 2);
        var posY = parseInt((iClientInnerHeight - $("#VideoPlayer").height()) / 3);
        
        $("#VideoPlayer").css({top: posY + 'px', left: posX + 'px', visibility: 'visible'}).find("a.Close").click(function(){
            $("#VideoPlayer").css("visibility", "hidden");
            $(oOverlay).hide();
            $("#Header_Homepage").show();
            if ($.browser.msie) {
	            $("select").css("visibility", "visible");
	        }
        });
    }



//  ---------------------------------------------------------------------------
//	counter animation
//  ---------------------------------------------------------------------------

    function runCounter() {
    
        $(".SearchForm").each(function(){
            
            //  video popup
            $("#ClickToPlay").click(function (e) {
                videoPopup(e);
            });
        
            if (isCounterRunning == true) {
                clearTimeout(counterTimer);
                isCounterRunning = false;
            }
        
            //  set timer delay (IE | Firefox)
            iCounterTimer = (document.all) ? 500 : 250;

            //  get the total quantity
	        var obj = document.getElementById("TotalRows");
	        var iCounterValue = obj.innerHTML;
    	    
	        //  if the search results table is present, don't run the counter animation
	        var searchResults = document.getElementById("SearchResults");
            if (searchResults.getElementsByTagName("TR").length != "0") {
                obj.style.visibility = "visible";
                $("#DealsFound .Inner").fadeIn(300);
                return;
            }
        	
	        //  set the width of the container before clearing html
	        obj.style.width = obj.offsetWidth + "px";
	        obj.innerHTML = "";
	        obj.style.visibility = "visible";

            //  store individual characters in 
	        var current = new Array();
	        for (var j = 0; j < iCounterValue.length; j++) {
		        current.push(iCounterValue.slice(j, j + 1));
	        }

            //  loop through each character in value to create html wrappers
	        for (var i = 0; i < current.length; i++) {
                //  create div tag around each char - used for background animation graphic and bind class and delay timer to numbers only
		        var wrap = document.createElement("span");
		        if (current[i] != ",") {
		            wrap.className = "Number";
		            wrap.Timer = setTimer(wrap);
		        }

                //  create strong tag around each char - to hide char
		        var strong = document.createElement("strong");
		        strong.innerHTML = current[i];
        		
		        //  insert news tags into DOM
		        obj.appendChild(wrap);
		        wrap.appendChild(strong);
	        }
    	    
	        $("#SearchFormLoadingBar").hide();
	        $("#SearchFormControls").show();
    	    
	        $("#DealsFound .Inner").fadeIn(300);
    	    
	        //  init popups and button hovers
	        initPopUp();
	        $.auto.init();
        });
    }

    function setTimer(wrap) {
	    iCounterTimer += 300;
	    wrap.Timer = setTimeout(function(){
	        if (wrap.childNodes[0]) {
		        wrap.childNodes[0].style.visibility = "visible";
		    }
		    wrap.style.backgroundImage = "none";
		    clearTimeout(wrap.Timer);
	    }, iCounterTimer);
    }
    
    function checkCounterValue(obj) {
        if (obj) {
            currentSelect = obj;
        }
        if (!currentSelect) {
            return;
        }
        
        $("#DealsFound .Inner").hide();
        
//Hidding iPhone4SMessaging
$("#ctl00_cphPageContent_ctl00_iPhoneMessaging").css("visibility", "hidden");

//Start - ITWR2004/2016 to hide the “sort results” heading and below three buttons

        $("#SearchFormControls").hide();

        $("#mm_add_sort").hide();

        //End - ITWR2004/2016 to hide the “sort results” heading and below three buttons

       
        
        if (isCounterRunning == false) {
            $("#SearchFormLoadingBar").show();
        }
        
        if (document.getElementById(currentSelect.id)) {
            counterTimer = setTimeout('checkCounterValue()', 250);
            isCounterRunning = true;
        } else {
            runCounter();
        }
    }
    
    

//  ---------------------------------------------------------------------------
//	onload init
//  ---------------------------------------------------------------------------

    objectAttachEvent(window, 'load', runCounter);
    
    
    
    
    
    
 //Code Add By Amit Kumar Saxena
 // ITWR 2004 bugfix Task1   
function mm_onclick(s)
{
//$("#mm_add_sort").html(div_h_inner+div_h_inner2);
var n=[];
var q=document.getElementById("ctl00_cphPageContent_ctl00_ddlSortBy");
if(q)
{
    n=q.getElementsByTagName("option");
    for(var o=0;o<n.length;o++)
    {
        if(n[o].value==s)
        {
        n[o].selected="selected";
        n[o].parentNode.onchange()
        }
    }
}
}
