﻿var adID = 1;
var col1 = 0;
var col2 = 0;
var col3 = 0;
var numOfAds = 3;
var drawSpeed = 400;
var dispSpeed = 50;

$(document).ready(function() {
    //loadAds();
});

//SEARCH
function loadSearch() { loadAds('search', $('#txtSearch').val()) }
function ifReturnKey(theCode) { if (theCode == 13) { loadSearch(); } }
function inputFocus(inp) {
    if (inp.id == "txtSearch") {
        if (inp.value == "e.g. apartment") {
            inputClear(inp);
        }
    }
}
function inputBlur(inp) {
    if (inp.id == "txtSearch") {
        if (inp.value == "") {
            inputUnClear(inp, "e.g. apartment");
        }
    }
}
function inputClear(inp, num) {
    $(inp).val('');
    $(inp).css('color', '#000000');
}
function inputUnClear(inp, value) {
    $(inp).val(value);
    $(inp).css('color', 'gray');
}
//END SEARCH

function loadAds(param, value) {
    $("#imgLoading").show();
	if(param == 'cat')
	{
	    $("#theAds").load("loadads.aspx?cat=" + value, function() {
	        drawAds();
	    });
	}
	else if(param == 'sec')
	{
	    $("#theAds").load("loadads.aspx?sec=" + value, function() {
	        drawAds();
	    });
	}
	else if(param == 'search')
	{
	    $("#theAds").load("loadads.aspx?search=" + value, function() {
	        drawAds();
	    });
	}
	else
	{
	    $("#theAds").load("loadads.aspx", function() {
	        drawAds();
	    });
	}

	$("#ad_mediumrectangle").html("<iframe width=\"300\" height=\"250\" scrolling=\"no\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" src=\"http://www.idsnews.com/banmanpro/a.aspx?Task=Get&Mode=SSI&ZoneID=1&SiteID=1&Keywords=classifieds,all\"></iframe>");
	$("#ad_skyscraper").html("<iframe width=\"120\" height=\"600\" scrolling=\"no\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" src=\"http://www.idsnews.com/banmanpro/a.aspx?Task=Get&Mode=SSI&ZoneID=2&SiteID=1&Keywords=classifieds,all\"></iframe>");
	$("#ad_leftbutton").html("<iframe width=\"120\" height=\"90\" scrolling=\"no\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" src=\"http://www.idsnews.com/banmanpro/a.aspx?Task=Get&Mode=SSI&ZoneID=5&SiteID=1&Keywords=classifieds,all\"></iframe>");
	$("#ad_rightbutton").html("<iframe width=\"120\" height=\"90\" scrolling=\"no\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" src=\"http://www.idsnews.com/banmanpro/a.aspx?Task=Get&Mode=SSI&ZoneID=6&SiteID=1&Keywords=classifieds,all\"></iframe>");
}

function loadHome(){$("#theAds").load("includes/home.txt");}

function drawAds() {
    adID = 1;
    col1 = 40;
    col2 = 177;
    col3 = 177;
    $.timer(dispSpeed, function(timer) {
        if (adID >= numOfAds) {
            timer.stop();
            $("#imgLoading").hide();
        }
        else {
            drawTimer(adID, drawSpeed);
        }
    });
}

function drawTimer(id, speed)
{
	var column = 1;
    drawAd('anad' + id, speed, column);
    adID++;
}

function drawAd(id, speed, column) {
    $("#" + id).show();
    if (col1 > 1200) {
        $("#" + id).css('visibility', 'hidden');
    }
	
	thisAdID = $("#" + id).attr("rel");
	strMailIt = '<div class="mailBox"><center><strong>E-mail this ad to a friend.</strong></center>'+
	'<textarea id="txtNote' + thisAdID + '" style="width:98%; height:33px;">Thought you might be interested.</textarea>' +
	'<input id="txtEmail' + thisAdID + '" style="width:112px" type="text" value="e-mail address" />' +
	'<input id="txtName' + thisAdID + '" style="width:112px" type="text" value="Your Name" />' + 
	'<input value="Send" type="button" onclick="sendAd(' + thisAdID + ');" />' + '</div>';
	$("#" + id).append(strMailIt);
	
    var topVal;
    var leftVal = 20;
    if (column == 1) {
        topVal = col1 + "px";
        col1 += document.getElementById(id).offsetHeight + 5;
    }
    else if (column == 2) {
        topVal = col2 + "px";
        col2 += document.getElementById(id).offsetHeight + 5;
        leftVal += 210;
    }
    else if (column == 3) {
        topVal = col3 + "px";
        col3 += document.getElementById(id).offsetHeight + 5;
        leftVal += 420;
    }

    //var containerheight = topVal + 25000;
    leftVal = leftVal + "px";
    $("#" + id).css("left", leftVal)
    //if ($(".container").css("height") < topVal) {
        $(".container").css("height", 5000); //Make sure there is enough height - Adam Rochford
    //}
    $("#" + id).animate({ "top": topVal }, speed, function() {
        $("#" + id).css('visibility', 'visible');
        if (id == 'anad' + (numOfAds - 1)) {
            //$("#" + id).width(190); alert('done' + id + ' * ' + numOfAds);
            //adHover();
        }
    });
}
function adHover() {
    $(".classBox").hover(
		function() {
		    curHeight = $(this).height();
		    curWidth = $(this).width();
		    newHeight = 300;
		    newWidth = 300;
		    var offset = $(this).offset();
		    curX = offset.left;
		    curY = offset.top;
		    newX = curX - Math.round((newWidth - curWidth) / 2);
		    newY = curY - 20;

		    if (curWidth < 191) {
		        $(this).attr("cw", curWidth).attr("ch", curHeight).attr("x", offset.left).attr("y", offset.top);
		    }
		    $(this).css("z-index", "3000").animate({ "width": newWidth, "fontSize": "17pt", "top": newY, "left": newX }, 500, function() {
		        //$('#' + $(this).attr('id') + ' > .mailBox').slideDown("slow")
		    });

		},
		function() {
		    $('#' + $(this).attr('id') + ' > .mailBox').hide();
		    $(this).css("z-index", "1").animate({ "width": $(this).attr("cw"), "fontSize": "11pt", "top": $(this).attr("y"), "left": $(this).attr("x") }, 500, function() {
		        //$('#' + $(this).attr('id') + ' > .mailBox').hide();
		    });
		}
	);
}