﻿function goToAd(id)
{
    window.location = 'Results.aspx?AdId=' + id;
}

function goToProfile(profileSubdomain) {
    window.location = profileSubdomain;
}

function getAdId() {

    var txtBox = document.getElementById('adId');
    if (txtBox == null)
        return;

    if (txtBox.value.length < 2 || txtBox.value.length > 15)
        return;

    goToAd(txtBox.value);
}


function setTab(id) {
    var tabs = $("#tabArea").children("div");
    tabs[id].style.display = "block";
    tabs[id].innerText = id;
}



function popup(url, width, height, scrollbars) {

    newwindow = window.open(url, 'name', 'height=' + height + ',' + 'width=' + width + ',scrollbars=' + scrollbars);

    newwindow.focus();
	    
    return false;
}

function AddToList(adID) {
    alert('Η αγγελία ' + adID + ' προστέθηκε στην λίστα των αγγελίων σας');

    var currentList = readCookie('myAdList');
    if (currentList == null || currentList.length < 1) {
        currentList = '-' + adID;
    }
    else {
        currentList = currentList + '-' + adID;
    }

    createCookie('myAdList', currentList, 1000);    
    return false;
}

function RemoveFromList(adID) {
    alert('Η αγγελία ' + adID + ' διαγράφτηκε απο την λίστα των αγγελίων σας');

    var currentList = readCookie('myAdList');
    if (currentList == null || currentList.length < 1)         
        return false;

    if (currentList[0] != '-')
        currentList = '-' + currentList;
    
    currentList = currentList.replace('-' + adID, '');

    createCookie('myAdList', currentList, 1000);        
}



function createCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else 
        expires = "";

    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}


function calculateLoan() {

    var amount = parseFloat($("#amountTextBox").val());
    var interest = parseFloat($("#interestTextBox").val()) / 1200.0;
    var months = parseInt($("#yearsTextBox").val()) * 12;

    var monthlyPayments = (interest * amount) / (1 - Math.pow((1 + interest), -months));

    $("#loanResult").html(monthlyPayments.toFixed(2));
    
}

function showSiteMap() {
    
    $("#siteMapDiv").dialog({
        title: '<img src="Images/logoTransparent.gif" alt="Logo" />',
        bgiframe: true,
        resizable: false,
        modal: true,
        width: 950,
        height: 550
    });
}

function showContacts() {

    $("#contactDiv").dialog({
        title: '<img src="Images/logoTransparent.gif" alt="Logo" />',
        bgiframe: true,
        resizable: false,
        modal: true,
        width: 400,
        height: 250
    });
}

var PromoCategorySource;

function loadPromoContent() {

    AdServices.GetPromoContentAds(PromoCategorySource, onPromoContentAds_CallBack);    
}

function isFavoriteAd(adId) {    
        
    var currentList = readCookie('myAdList');
    if (currentList == null || currentList.length < 1)
        return false;

    if (currentList[0] != '-')
        currentList = '-' + currentList;

    return currentList.indexOf('-' + adId) > -1; 
}


function onPromoContentAds_CallBack(result) {

    for (var i = 0; i < result.length; i++) {
        $('#adSlot' + (i + 1).toString()).html(getAdRenderHtml(result[i]));
        if (i >= 3)
            break;
    }
}

function getAdRenderHtml(adJson) {

	var htmlText = '<br />';

	if (adJson == null)
		return htmlText;
    
    htmlText += '<div class="promoAd" onclick="window.location=\'Results.aspx?AdId=' + adJson.Id + '\';">';
        htmlText += '<div class="title">' + adJson.Title + '</div>';
        htmlText += '<img class="pointer" width="100" height="80" alt="' + adJson.Location + '" title="' + adJson.Title + '" src="' + adJson.Image + '"/>';
        htmlText += '<div class="category">' + adJson.Category + '</div>';
        htmlText += '<div class="location">' + adJson.Location + '</div>';
        htmlText += '<div class="price">' + adJson.Price + '</div>';
    htmlText += '</div>';

    return htmlText;    
}


function isProfileSeen(profileId) {
	
	var seenProfileList = readCookie('ProfilesSeen');
	if (seenProfileList == null || seenProfileList.length < 1)
		return false;
	
	if (seenProfileList[0] != '-')
		seenProfileList = '-' + seenProfileList;

	return seenProfileList.indexOf('-' + profileId) > -1;
}

function adToSeenProfiles(profileId) {	

	var seenProfileList = readCookie('ProfilesSeen');
	if (seenProfileList == null || seenProfileList.length < 1)
		seenProfileList = '-';
	
	seenProfileList = seenProfileList + '-' + profileId;

	createCookie('ProfilesSeen', seenProfileList, 30);
}

function incrementProfileImpressions(profileId) {

	if (isProfileSeen(profileId))
		return;

	AdServices.IncrementProfileImpressions(profileId);

	adToSeenProfiles(profileId);	

}



function setSaveAdImageAttributes(adId, adImageId) {

    var saveAdImage = $('#' + adImageId);    

    if (isFavoriteAd(adId)) {
        saveAdImage.attr('src', 'Images/heart_minus.gif');
        saveAdImage.attr('title', 'Διαγραφή αγγελίας από τις Επιλεγμένες');        
        saveAdImage.click(function () {
            saveAdImage.unbind();
            RemoveFromList(adId);
            setSaveAdImageAttributes(adId, adImageId);
        });
    }
    else {
        saveAdImage.attr('src', 'Images/heart_plus.gif');
        saveAdImage.attr('title', 'Προσθήκη αγγελίας στις Επιλεγμένες');
        saveAdImage.click(function () {
            saveAdImage.unbind();
            AddToList(adId);
            setSaveAdImageAttributes(adId, adImageId);
        });
    }

}

var TabCurrentPage = new Array();
var CurrentPage = 0;

function LoadTab(index) {

    if (index == 0)
        AdServices.GetTabContentAds(Tab0_Id, TabCurrentPage[index], onTab0_ContentAds_CallBack);
    else if (index == 1)
        AdServices.GetTabContentAds(Tab1_Id, TabCurrentPage[index], onTab1_ContentAds_CallBack);
    else if (index == 2)
        AdServices.GetTabContentAds(Tab2_Id, TabCurrentPage[index], onTab2_ContentAds_CallBack);
    else if (index == 3)
        AdServices.GetTabContentAds(Tab3_Id, TabCurrentPage[index], onTab3_ContentAds_CallBack);
    else if (index == 4)
        AdServices.GetTabContentAds(Tab4_Id, TabCurrentPage[index], onTab4_ContentAds_CallBack);
    else if (index == 5)
        AdServices.GetTabContentAds(Tab5_Id, TabCurrentPage[index], onTab5_ContentAds_CallBack);
}

function onTab0_ContentAds_CallBack(results) {
    placeResultAdHtmlInContainer(0, $('#tabs-0'), results);
}

function onTab1_ContentAds_CallBack(results) {
    placeResultAdHtmlInContainer(1, $('#tabs-1'), results);
}

function onTab2_ContentAds_CallBack(results) {
    placeResultAdHtmlInContainer(2, $('#tabs-2'), results);
}

function onTab3_ContentAds_CallBack(results) {
    placeResultAdHtmlInContainer(3, $('#tabs-3'), results);
}

function onTab4_ContentAds_CallBack(results) {
    placeResultAdHtmlInContainer(4, $('#tabs-4'), results);
}

function onTab5_ContentAds_CallBack(results) {
    placeResultAdHtmlInContainer(5, $('#tabs-5'), results);
}

function placeResultAdHtmlInContainer(tabIndex, container, results) {

    if (container == null)
        return;

    if (results == null || results.length == 0)
        return;

    container.css("height", "180px");

    var finalHtml = '';

    for (var i = 0; i < results.length; i++) {

        if (tabIndex != 5) 
            finalHtml += getAdTabHtml(results[i]);
        else            
            finalHtml += getProfileTabHtml(results[i]);        
    }

    finalHtml = getLeftImageHtml(tabIndex) + finalHtml + getRightImageHtml(tabIndex);

    container.fadeOut('slow', function () {
        container.html('<div class="tabAd"></div>');
        container.html(finalHtml);
        container.fadeIn('slow');
    });

}

function LoadNextPage(index) {
    TabCurrentPage[index]++;

    if (TabCurrentPage[index] > TabMaxPages[index])
        TabCurrentPage[index] = TabMaxPages[index];

    LoadTab(index);
}

function LoadPreviousPage(index) {
    TabCurrentPage[index]--;

    if (TabCurrentPage[index] < 0)
        TabCurrentPage[index] = 0;

    LoadTab(index);
}

function getLeftImageHtml(index) {

    if (TabCurrentPage[index] <= 0)
        return '';

    var htmlText = '';
    htmlText += '<div class="imageButton" onclick="LoadPreviousPage(' + index + ');">';
    htmlText += '<br/><br/><br/>';
    htmlText += '<img id="previousImageButton" src="images/back_slide.png"/>';
    htmlText += '</div>';
    return htmlText;
}

function getRightImageHtml(index) {

    if (TabCurrentPage[index] >= TabMaxPages[index])
        return '';

    var htmlText = '';
    htmlText += '<div class="imageButton" onclick="LoadNextPage(' + index + ');">';
    htmlText += '<br/><br/><br/>';
    htmlText += '<img id="nextImageButton" src="images/next_slide.png"/>';
    htmlText += '</div>';
    return htmlText;
}

function getProfileTabHtml(adJson) {

    var htmlText = '';
    htmlText += '<div class="profileTab" onclick="window.location=\'' + adJson.Url + '\';">';
    htmlText += '<div class="title">' + adJson.Title + '</div>';
    htmlText += '<img width="150"  src="' + adJson.Image + '"/>';
    htmlText += '</div>';
    return htmlText;
}

function getAdTabHtml(adJson) {

    var htmlText = '';

    if (adJson == null)
        return htmlText;
    
    htmlText += '<div class="tabAd" onclick="window.location=\'' + adJson.Url + '\';">';
    htmlText += '<div class="title">' + adJson.Title + '</div>';
    htmlText += '<img alt="' + adJson.Location + '" title="' + adJson.Title + '" src="' + adJson.Image + '"/>';	
    
	if (adJson.Title != adJson.Category)
    	htmlText += '<div class="category">' + adJson.Category + '</div>';

    htmlText += '<div class="location">' + adJson.Location + '</div>';
    htmlText += '<div class="price">' + adJson.Price + '</div>';
    htmlText += '</div>';
    return htmlText;
}


function initializeControlMap(controlId) {

	var latlng = new google.maps.LatLng(37.857507, 23.906250);
	var myOptions = {
		zoom: 15,
		center: latlng,
		streetViewControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU },
		navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL }
	};

	var mapContent = document.getElementById(controlId);
	if (mapContent == null)
		return;

	controlMap = new google.maps.Map(mapContent, myOptions);

	google.maps.event.addListener(controlMap, 'zoom_changed', function (event) {
		if (controlMap.getZoom() < 7)
			controlMap.setZoom(7);
	});

	controlMapBounds = new google.maps.LatLngBounds();
}

function placeResultMarker(location, title, price, imgSrc, id, moreInfo) {
	controlMapBounds.extend(location);

	var marker = new google.maps.Marker({
		position: location,
		draggable: false,
		map: controlMap,
		icon: markerImage
		//,title: adTitle
	});

	var infoContent = getInfoWindowHtml(title, price, imgSrc, id, moreInfo);

	var infowindow = new google.maps.InfoWindow({
		content: infoContent
	});

	google.maps.event.addListener(marker, 'click', function () {

		controlMap.setZoom(15);

		if (currentOpenInfoWindow != null)
			currentOpenInfoWindow.close();

		currentOpenInfoWindow = infowindow;

		infowindow.open(controlMap, marker);

	});

}



function getInfoWindowHtml(title, price, imgSrc, id, moreInfo) {
	var winHtml = '<div class="infoWindow">';

	winHtml += '<div class="left" onclick="openAd(' + id + ');" >';
	winHtml += '<img title="Δείτε την αγγελία..."  src="' + imgSrc + '"/>';
	winHtml += '</div>';

	winHtml += '<div class="right">';

	winHtml += '<div class="title" onclick="openAd(' + id + ');" >';
	winHtml += title;
	winHtml += '</div>';

	winHtml += '<div class="price">';
	winHtml += price;
	winHtml += '</div>';

	if (moreInfo) {
		winHtml += '<div class="moreInfo">';
		winHtml += moreInfo;
		winHtml += '</div>';
	}

	winHtml += '</div>';

	winHtml += '<div class="clear"></div>';

	winHtml += '</div>';

	return winHtml;
}


function ValidateEmailFields() {

	if (userTel == null || userTel.value.length < 10 ||
			userName == null || userName.value.length < 10 || userEmail == null || userEmail.value.length < 7) {

		$('#requiredFieldDiv').text('Παρακαλώ συμπληρώστε το όνομα (10 χαρακτήρες) σας, το τηλέφωνο (10 ψηφιά) σας & το email σας!');

		return false;
	}

	$('#requiredFieldDiv').text('');
	return true;
}

function initializeAdMap() {

	var latlng = new google.maps.LatLng(37.857507, 23.906250);

	if (adLat && adLng)
		latlng = new google.maps.LatLng(adLat, adLng);
	else
		return;

	var myOptions = {
		zoom: 13,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU },
		navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL }
	};

	map = new google.maps.Map(document.getElementById("content"), myOptions);

	google.maps.event.addListener(map, 'zoom_changed', function (event) {
		if (map.getZoom() < 7)
			map.setZoom(7);
	});

	placeMarker(latlng);
}

function placeMarker(location) {

	map.setCenter(location);

	var marker = new google.maps.Marker({
		position: location,
		draggable: false,
		map: map,
		title: 'Τοποθεσία Ακινήτου'
	});
}

function backSlide() {
	currentImageIndex--;
	changeImage(true);
}

function nextSlide() {
	currentImageIndex++;
	changeImage(true);
}


function changeImage(focusOnButtomLink) {

	var adPhoto = $('#AdPhoto');

	if (adPhoto == null)
		return;

	if (currentImageIndex == 0)
		$('#backSlideImage').hide();
	else
		$('#backSlideImage').show();

	if (currentImageIndex == photoPaths.length - 1)
		$('#nextSlideImage').hide();
	else
		$('#nextSlideImage').show();

	//adPhoto.attr('src', photoPaths[currentImageIndex]);                
	adPhoto.hide().fadeOut('slow', function () {
		adPhoto.css('background-image', 'url(' + photoPaths[currentImageIndex] + ')');
	});
	adPhoto.fadeIn('slow', function () {
		adPhoto.css('background-image', 'url(' + photoPaths[currentImageIndex] + ')');
	});

	if (focusOnButtomLink)
		document.getElementById('TextBox1').focus();
}

