
var topStories = parseInt(attributes.topStories);
var imgCount = 0; //bb

function load()
{
    //bb{
    var href = window.location.href;
    var i = href.indexOf('?');
    if (i > 0) {
        var feedURL = href.substring(i + 1);
        attributes["feedURL"] = feedURL;
        var popup = document.getElementById("feedPopup");
        for (var item = popup.firstChild; item != null; item = item.nextSibling) {
            if (typeof item.selected != "undefined") {
                item.selected = (item.value == feedURL);
            }
        }
    }
    
    var popupCookie = readCookie('popup');
    if (popupCookie == null) {
        alert("Puoi creare un'icona per Dolomiti Turismo\nsul desktop del tuo iPhone.\n\nPer farlo, usa il bottone \"+\"\nalla base dello schermo\ne scegli \"Aggiungi a Home\".");
        createCookie('popup', 1, 3650);
    }
    //bb}
    
    rssLoad();
    dashcode.setupParts();
    
    var todaysDate = document.getElementById("todaysDate");
    todaysDate.innerText = createDateStrIt(new Date(), false).toUpperCase();
}

//bb{
var socialNote = 'sent with mobc3';

function socialDelicious() {
	var url = "http://del.icio.us/post?v=5&amp;noui&amp;jump=close&amp;"
		+"&amp;title="+encodeURIComponent('Siena Mobile - '+social_title)
		+"&amp;notes="+encodeURIComponent(socialNote)
		+"&amp;url="+encodeURIComponent(social_link);
	window.open(url, 'delicious', 'width=320,status=yes');
}

function socialDigg() {
	var url = "http://digg.com/submit?phase=2"
		+"&amp;title="+encodeURIComponent('Siena Mobile - '+social_title)
		//+"&amp;notes="+encodeURIComponent(socialNote)
		+"&amp;url="+encodeURIComponent(social_link);
	window.open(url, 'digg', 'width=320,status=yes');
}

function socialFacebook() {
	var url = "http://facebook.com/sharer.php"
		+"?u="+encodeURIComponent(social_link);
		//+"&t="+encodeURIComponent(social_title);
	window.open(url, 'facebook', 'width=520,status=yes');
}

function socialGoogle() {
	var url = "http://www.google.com/bookmarks/mark?op=edit&output=popup"
		+"&title="+encodeURIComponent('Siena Mobile - '+social_title)
		//+"&notes="+encodeURIComponent(socialNote)
		+"&bkmk="+encodeURIComponent(social_link);
	window.open(url, 'google', 'width=320,status=yes');
}

function socialTwitter() {
	//TODO: autochiusura?
	var url = "http://twitter.com/home?jump=close"
		+"&status="+social_link; //+encodeURIComponent(social_link)
		/*+"+"+encodeURIComponent(social_title)*/;
	window.open(url, 'twitter', 'width=320,status=yes');
}

function socialMail() {
	var url = "mailto:?subject="+encodeURIComponent('Siena Mobile - '+social_title)
		+"&body="+encodeURIComponent("<a href="+social_link+">Articolo</a>")
		+encodeURIComponent("<br/><br/><i>("+socialNote+")</i>");
	window.location = url;
}

function goToSite() {
    window.location = "http://www.comune.siena.it/";
}

function imageLoaded(flag) {
	if (flag==1) imgCount--;
	if (imgCount <= 0)	{
		window.scrollTo(0, 1);
		document.getElementById("StackLayout").object.setCurrentView("frontPage", false, true);
	}
}

function createDateStrIt(date, dayOfWeekFlag)
{
    var day;
    switch (date.getDay()) {
        case 0: day = "Domenica"; break;
        case 1: day = "Lunedì"; break;
        case 2: day = "Martedì"; break;
        case 3: day = "Mercoledì"; break;
        case 4: day = "Giovedì"; break;
        case 5: day = "Venerdì"; break;
        case 6: day = "Sabato"; break;
    }
    var month;
    switch (date.getMonth()) {
        case 0: month = "Gennaio"; break;
        case 1: month = "Febbraio"; break;
        case 2: month = "Marzo"; break;
        case 3: month = "Aprile"; break;
        case 4: month = "Maggio"; break;
        case 5: month = "Giugno"; break;
        case 6: month = "Luglio"; break;
        case 7: month = "Agosto"; break;
        case 8: month = "Settembre"; break;
        case 9: month = "Ottobre"; break;
        case 10: month = "Novembre"; break;
        case 11: month = "Dicembre"; break;
    }
    var ret = (dayOfWeekFlag ? day + ", " : "");
    ret += date.getDate() + " " + month + " " + date.getFullYear();
    return ret;
}

function feedPopupHandler() {
    var url = null;
    var popup = document.getElementById("feedPopup");
    for (var item = popup.firstChild; item != null; item = item.nextSibling) {
        if (typeof item.selected != "undefined" &&
            item.selected) {
            url = item.value;
            break;
        }
    }
    if (url != null) {
        var href = window.location.href;
        var i = href.indexOf('?');
        if (i > 0) {
            href = href.substring(0, i);
        }
        href += "?" + url;
        window.location = href;
    }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var 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);
}
//bb}

var headlineList = {

numberOfRows: function() {
    if (feedResults == null)
        return 0; 
    else
        if (feedResults.length > topStories)
            return topStories;
        else
            return feedResults.length;
},
    
prepareRow: function(rowElement, rowIndex, templateElements) {
    var thumbimageurl = feedResults[rowIndex].thumbimageurl;
    if (thumbimageurl != null) {
        var headline = templateElements.headlineTitle;
        if (headline) {
            headline.style['margin-left'] = '100px';
            headline.style['min-height'] = '61px';
        }
        var descr = templateElements.headlineDescription;
        if (descr) {
            rowElement.removeChild(descr);
        }
        var img = templateElements.img1;
        if (img) {
            img.setAttribute('src', thumbimageurl);
		   imgCount++;
        }
    } else {
        var img = templateElements.img1;
        if (img) {
            rowElement.removeChild(img);
        }
    }
    if (templateElements.headlineTitle) {
        var content = extractText(feedResults[rowIndex].title);
        templateElements.headlineTitle.innerHTML = content;
    }
    
    if (templateElements.headlineDescription) {
        var content = extractText(feedResults[rowIndex].description);
        templateElements.headlineDescription.innerHTML = content;
    }
    
    var self = this;
    var handler = function() {
        detailController.setRepresentedObject(rowIndex);
        document.getElementById("StackLayout").object.setCurrentView("articlePage", false, true);
    };
    rowElement.onclick = handler;
}
};


var secondHeadlineList = {
    
numberOfRows: function() {
    if (feedResults == null)
        return 0; 
    else
        if (feedResults.length > topStories)
            return (feedResults.length-topStories);
        else
            return 0;
},
    
prepareRow: function(rowElement, rowIndex, templateElements) {
	imageLoaded(0);
    var tempRow = rowIndex + topStories;
    if (templateElements.secondHeadlineTitle) {
        templateElements.secondHeadlineTitle.innerHTML = extractText(feedResults[tempRow].title);
    }
    
    var self = this;
    var handler = function() {
        detailController.setRepresentedObject(tempRow);
        document.getElementById("StackLayout").object.setCurrentView("articlePage", false, true);
    };
    rowElement.onclick = handler;
}
};

var social_title = '';
var social_link = '';

var detailController = {
    
setRepresentedObject: function(representedObject) {
    this._representedObject = representedObject;
    
	social_title = extractText(feedResults[this._representedObject].title);
	social_link = feedResults[this._representedObject].link;
	
    var title = document.getElementById('articleTitle');
    var article = document.getElementById('articleDescription');
	
    var thumbimageurl = null; //feedResults[this._representedObject].thumbimageurl;
    var img = document.getElementById('articleImage');
    if (thumbimageurl != null) {
        if (title) {
            title.style['margin-left'] = '100px';
            title.style['min-height'] = '62px';
        }
        if (img) {
            img.setAttribute('src', thumbimageurl);
        }
    } else {
        if (title) {
            title.style['margin-left'] = '10px';
            title.style['min-height'] = '22px';
        }
        if (img) {
            img.setAttribute('src', 'Images/dummy.png');
        }
    }
	
    title.innerHTML = extractText(feedResults[this._representedObject].title);
    article.innerHTML = extractHTML( feedResults[this._representedObject].description );
    
    var self = this;
    var moreLink = document.getElementById('readMore');
    moreLink.object.onclick = function() {
        window.location = social_link;
    };
    var backLink = document.getElementById('backToHeadlines');
    backLink.object.onclick = function() {
        document.getElementById("StackLayout").object.setCurrentView("frontPage", true);
    };
}
};



function goToMobc3(event)
{
    window.location = "http://www.mobc3.com/";
}

