var _CONFIG = {"locale": {"lang": "all", "msgBundle": {"untranslate_action": "Original", "realtime_btn_pause_title": "Pause real-time updates", "noSavedSearches": "You don't have any saved searches yet", "signin_twitterpassword_label": "password:", "gallery_title": "Live Pictures", "signin_twitterlogin_label": "user name:", "savedQuery_name_label": "Name", "realtime_btn_resume_title": "Resume real-time updates", "send_a_message": "Send a message", "googlenews_title": "News", "savedQuery_description_label": "Description", "search_adv_language": "Language", "search_languagePrefs_show": "Preferences", "realtime_speed_title_H": "Tweets Per Hour", "translate_action": "Translate", "favorite_action": "Favorite", "tagline": "Cutting through the noise", "highlights_title": "Highlights", "about_title": "about&nbsp;twazzup", "trendmakers_title": "Top Trendmakers", "about_link": "About", "speakers_title": "Speakers", "realtime_title": "Real-time tweets for", "savedQuery_delete_button_label": "Delete", "signin_error": "Wrong user name or password", "save_search_button": "Save this Search", "search_removed_notification": "Search was removed", "tweet_this_page": "Share this page", "more_results": "More results", "hottopics_label": "Hot&nbsp;Topics", "signin_cancel_button_label": "Cancel", "signin_button_label": "Sign in", "search_button": "Search", "realtime_pendingTweetsMessage_1": "<b>1<\/b> new result. <a href='#' class='btnRtRefresh'>Show it<\/a> or <a href='#' class='btnRtResume'>resume real-time updates<\/a>.", "savedSearches_label": "My Searches", "links_title": "Top Links", "any_language": "Any language", "sponsors_title": "Sponsors", "featuredtopics_label": "Featured", "savedQuery_save_button_label": "Save", "realtime_pendingTweetsMessage_0": "<b>Paused<\/b>, press <a href='#' class='btnRtResume'>play<\/a> to resume real-time updates.", "vids_title": "Related Videos", "savedQuery_cancel_button_label": "Cancel", "signout_button_label": "Sign out", "search_languagePrefs_hide": "Hide Preferences", "realtime_pendingTweetsMessage_2": "<\/b> new results. <a href='#' class='btnRtRefresh'>Show them<\/a> or <a href='#' class='btnRtResume'>resume real-time updates<\/a>", "want_to_tweet_signin_now": "Want to tweet? <b>Sign in now!<\/b>", "usersWidget_title": "Community", "signed_in_as": "Signed in as", "search_saved_notification": "Your search was saved", "reply_action": "Reply", "preview_action": "preview"}, "uilang": "en"}, "widgets": {"trends": [], "featured": {"default": [{"q": "\u266b", "style": "background-color:#39A9A8;color:white", "name": "&#9835;"}, {"q": "WOW OR awesome OR \"very cool\"", "style": "background-color:#58AA00;color:white", "name": "wow!"}, {"q": "twazzup -via", "style": "background-color:#45b4da;color:white", "name": "twazzup"}], "directory": []}}};
var _LOCALE = _CONFIG.locale;
//var _USERPROFILE = _CONFIG.user;
var _WIDGETS = _CONFIG.widgets;

var _CONTEXT = {};

var TWAZZUP=null;

function toggleAdvSearch(){
	$('#advSearch').slideToggle();
	$('.advSearchToggleButton').toggle();
}

$(function(){
	var lang = _LOCALE.lang;
	var uilang = _LOCALE.uilang;
	$("#preferedLang").val(lang);
	var plr = $("#preferedLanguageReminder");
	var uilangText = $("#preferedLang option[value="+uilang+"]").text();
	var html=[];
	html.push("<input type='radio' name='plrRadio' value='all' id='clearPreferedLang' "+(lang!="all"?"":"checked='checked'")+">"+_LOCALE.msgBundle.any_language+" ");
	var showLangReminder = uilang!="en" || lang!='all';
	if (lang!='all'){
		// show the reminder lang pref
		var langText = $("#preferedLang option[value="+lang+"]").text();
		html.push("<input type='radio' name='plrRadio' value='"+lang+"' checked='checked'> "+langText);
	}
	if (lang!=uilang){
		html.push("<input type='radio' name='plrRadio' id='setPreferedLangToUI' value='"+uilang+"'> "+uilangText);
	}
	plr.html(html.join(""));
	
	// TODO : understand how to handle the UI for profile and custom pages
	//if (showLangReminder) plr.show();
	$("#clearPreferedLang").click(function(){
		$("#preferedLang").val('all');
		$("#searchForm").submit();
	});
	$("#setPreferedLangToUI").click(function(){
		$("#preferedLang").val(uilang);
		$("#searchForm").submit();
	});
	$("#preferedLang").change(function(){
		$("#searchForm").submit();
	});
	$("#searchForm").submit(function(){
		var q = $("#searchBox").val();
		//if (!q) return false;
		// set the cookie for the selected lang ...
		var lang = $("#preferedLang").val();
		if (lang){
			createCookie("lang",lang,365);
		}
		$("#preferedLanguageReminder").text("");
		return true;
	});
	
	_CONTEXT["currentQuery"]=$("#searchBox").val();

	new SavedQueriesWidget();

	TWAZZUP = new Twazzup({config:_CONFIG/*,trends:*/});
	$.extend(_WIDGETS,{
		widgetsParameters:{
		}
		,pipes:{}
	});

	_WIDGETS["page"] = {
			"type":"UserPage",
			"owner":{'twitter_id':34574129,'screen_name':"Wegaa",'lookup':"wegaa",'name':"Vega",'profile_image_url':"http://a3.twimg.com/profile_images/697365281/stereotype_4_123klan_normal.jpg"},
			"key":"agd0d2F6enVwch0LEgRQYWdlIhNwYWdlOjM0NTc0MTI5OndlZ2FhDA",
			"lookup":"wegaa",
			"basequery":"from:wegaa OR @wegaa",
			"useroverlayQuery":"@wegaa",
			"title":"Vega (Wegaa)",
			"desc":"from:wegaa OR @wegaa",
			"skin":{"html": "\n        <div id=\"twRCol\">\n            <div id=\"sponsorsFeature_PH\" ><\/div>\n            <div id=\"realtimeFeature_PH\" ><\/div>\n        <\/div>\n        <div id=\"twLCol\"> \n            <div id=\"users_PH\"><\/div>\n            <div id=\"picGalleryFeature_PH\" ><\/div>\n            <div id=\"feedFeature_PH\" ><\/div>\n            <div id=\"googlenewsFeature_PH\" ><\/div>\n            <div id=\"youtubeFeature_PH\" ><\/div>\n            <div id=\"linksFeature_PH\" ><\/div>\n        <\/div>        \n", "css": "\/* CSS for page *\/\nbody {\n}\n"},
			"features":{"antispamFeature": {"spammers": [], "type": "AntispamFeature"}, "feedFeature": {"disabled": false, "url": "http:\/\/woopwoop.fr\/blog\/", "type": "FeedFeature", "title": "woopwoop.fr\/blog\/"}, "googlenewsFeature": {"disabled": true, "query": "\"@Wegaa\" OR \"Vega\"", "type": "GooglenewsFeature", "title": "In the news"}, "editorialFeature": {"disabled": true, "query": "from:Wegaa", "type": "EditorialFeature", "modules": [{"filter": {"params": {}, "type": "PicturesPipe"}, "id": "myPics", "title": "My pictures"}, {"filter": {"params": {"tag": ":)"}, "type": "LinksPipe"}, "id": "myLols", "title": "My lols"}]}, "logoFeature": {"url": "http:\/\/a3.twimg.com\/profile_images\/697365281\/stereotype_4_123klan_normal.jpg", "type": "LogoFeature"}, "sponsorsFeature": {"disabled": false, "query": null, "type": "SponsorsFeature", "users": ["wegaa"], "title": "@Wegaa (Vega)"}, "toolbarFeature": {"disabled": true, "type": "ToolbarFeature", "queries": []}, "youtubeFeature": {"disabled": true, "type": "YoutubeFeature", "channel": "", "title": "Videos"}, "pageFeature": {"type": "UserPageFeature", "features": {"picGallery": true, "highlights": false, "links": true, "realtime": true}}}
	};
	
	TWAZZUP.init(_WIDGETS,$('#searchBox').val());
	TWAZZUP.search($('#searchBox').val());
	if($("#flash").html()){
		$("#flash").slideDown(function(){
			setTimeout(function(){$("#flash").slideUp();},5000);
		});
	}
	$("input[type=text]").fieldtag();
});
