var args = arguments[0] || {};

var id = args.entryId;

var model = Alloy.createModel('entries');
$.sublabel.text=L('details');
model.fetch({
	data : {
		"0" : {
			id : id
		}
	},
	localOnly : true,
	success : function(data) {
		tracker.trackEvent({
	        category: "newentry_detail",
	        action: "view",
	        label: model.get('title'),
	        value: id
		});
		if (model.get('image')) {
			$.mainImg.image = model.get('image');
		} else {
			$.entrydetail.remove($.mainImg);
			$.mainImg = null;
		}

		var StyledLabel = require('ti.styledlabel');
		var label = StyledLabel.createLabel({
			html : model.get('description').replace(/<([^a/]\w+).*?>/g, '<$1>'),
			height : Ti.UI.SIZE,
			width : Ti.UI.FILL,
			left : "10dp",
			right : "10dp",
			bottom : "10dp",

		});
		label.addEventListener('click', onClickLink);

		function onClickLink(e) {
			if ( typeof e.url !== 'undefined') {
				payload = {
					url : e.url
				};

				var browsewin = Alloy.createController('webViewer', payload).getView();
				browsewin.open({
					modal : true,
					activityEnterAnimation : Ti.App.Android.R.anim.slide_up
				});
				browsewin.addEventListener('android:back', function() {
					browsewin.close();
				});
			}
		}


		$.detail.add(label);

		$.titleLabel.text = model.get('title');
		$.titlePrice.text = "€" + model.get('price');

		///////////////////////////
		/////  SOCIAL & GALLERY
		///////////////////////////
		var modelurl = model.get('url');
		var modelimages = model.get('images');

		// testing
		//modelimages= '[{"image":"http:\/\/beautylineapp.ideaseven.com\/uploads\/images\/yfso4.jpg","image_small":"http:\/\/beautylineapp.ideaseven.com\/uploads\/images\/yfso4.jpg"}]';
		//modelurl = 'http://www.google.com';

		if (modelurl || modelimages != '[]') {
			// GALLERY
			if (modelimages != '[]') {

				var gallery = Ti.UI.createView({
					layout : "horizontal",
					width : Ti.UI.SIZE,
					height : Ti.UI.FILL,
					left : "0dp"
				});
				var icon = Ti.UI.createImageView({
					image : "/images/gallery.png",
					top : "13dp",
					bottom : "12dp",
					left : "5dp"
				});
				gallery.add(icon);
				var label = Ti.UI.createLabel({
					text : L('more_photos'),
					color : "#003876",
					font : {
						fontFamily : 'Helvetica',
						fontSize : '12dp',
						fontStyle : 'normal',
						fontWeight : 'bold'
					},
					left : "5dp",
					height : Ti.UI.FILL
				});
				var sep = Ti.UI.createView({
					height : Ti.UI.FILL,
					width : "1dp",
					backgroundColor : "#efefef",
					left : "5dp",

				});
				gallery.add(label);
				gallery.add(sep);
				$.socialBox.add(gallery);
				gallery.addEventListener('click', function(e) {
					var payload = {
						images : modelimages
					};
					var galwin = Alloy.createController('imgGallery', payload).getView();
					galwin.open({
						modal : true,
						activityEnterAnimation : Ti.App.Android.R.anim.slide_up,
						backgroundColor : "#000",
						navBarHidden : true,
						fullscreen : true
					});
					galwin.addEventListener('android:back', function() {
						galwin.close();
					});
				});
				if (model.get('image')) {
					$.mainImg.addEventListener('click', function(e) {
						var payload = {
							images : modelimages
						};
						var galwin = Alloy.createController('imgGallery', payload).getView();
						galwin.open({
							modal : true,
							activityEnterAnimation : Ti.App.Android.R.anim.slide_up,
							backgroundColor : "#000",
							navBarHidden : true,
							fullscreen : true
						});
						galwin.addEventListener('android:back', function() {
							galwin.close();
						});
					});
				}
			}

			/// SHARE & WEB
			if (modelurl.trim() != '') {
				var share = Ti.UI.createView({
					layout : "horizontal",
					width : Ti.UI.SIZE,
					height : Ti.UI.FILL,
				});
				var icon = Ti.UI.createImageView({
					image : "/images/share.png",
					top : "11dp",
					bottom : "10dp",

				});
				share.add(icon);
				var label = Ti.UI.createLabel({
					text : L('share'),
					color : "#003876",
					font : {
						fontFamily : 'Helvetica',
						fontSize : '12dp',
						fontStyle : 'normal',
						fontWeight : 'bold'
					},
					left : "2dp",
					height : Ti.UI.FILL
				});
				share.add(label);

				$.socialBox.add(share);
				share.addEventListener('click', function(e) {
					var activity = Ti.Android.currentActivity;
					var intent = Ti.Android.createIntent({
						action : Ti.Android.ACTION_SEND,
						type : 'text/plain'
					});
					intent.putExtra(Ti.Android.EXTRA_TEXT, model.get('title') + " " + modelurl);
					//intent.putExtra(Ti.Android.EXTRA_SUBJECT, 'Place the subject of your message here');
					activity.startActivity(Ti.Android.createIntentChooser(intent, 'Share'));
				});

				var webview = Ti.UI.createView({
					layout : "horizontal",
					width : Ti.UI.SIZE,
					height : Ti.UI.FILL,
					right : "5dp"
				});
				var sep = Ti.UI.createView({
					height : Ti.UI.FILL,
					width : "1dp",
					backgroundColor : "#efefef",
					right : "5dp",

				});
				webview.add(sep);
				var icon = Ti.UI.createImageView({
					image : "/images/webview.png",
					top : "11dp",
					bottom : "10dp",

				});
				webview.add(icon);
				var label = Ti.UI.createLabel({
					text : "view on web",
					color : "#003876",
					font : {
						fontFamily : 'Helvetica',
						fontSize : '12dp',
						fontStyle : 'normal',
						fontWeight : 'bold'
					},
					left : "5dp",
					height : Ti.UI.FILL
				});
				webview.add(label);
				$.socialBox.add(webview);

				webview.addEventListener('click', function(e) {
					payload = {
						url : modelurl
					};

					var browsewin = Alloy.createController('webViewer', payload).getView();
					browsewin.open({
						modal : true,
						activityEnterAnimation : Ti.App.Android.R.anim.slide_up
					});
					browsewin.addEventListener('android:back', function() {
						browsewin.close();
					});
				});

			} else {

			};

		} else {
			$.entrydetail.remove($.social);
		}

	}
});


// then create an adMob view
var adMobView = Admob.createView({
    publisherId:"ca-app-pub-5570465018686719/5215786182",
    testing:true, // default is false
    //top: 0, //optional
    //left: 0, // optional
    //right: 0, // optional
    bottom: 0, // optional
    adBackgroundColor:"FF8855", // optional
    backgroundColorTop: "738000", //optional - Gradient background color at top
    borderColor: "#000000", // optional - Border color
    textColor: "#000000", // optional - Text color
    urlColor: "#00FF00", // optional - URL color
    linkColor: "#0000FF" //optional -  Link text color
    //primaryTextColor: "blue", // deprecated -- now maps to textColor
    //secondaryTextColor: "green" // deprecated -- now maps to linkColor
    
});
 

//listener for adReceived
adMobView.addEventListener(Admob.AD_RECEIVED,function(){
   // alert("ad received");
   Ti.API.info("ad received"+Admob.AD_RECEIVED);
   Ti.API.info("ADVIEW HEIGHT:"+$.ad.size.height);
   Ti.API.info("ADVIEW HEIGHT:"+adMobView.size.height);
    
    $.ad.height=PixelsToDPUnits(adMobView.size.height)+"dp";
    $.ad.backgroundColor="#000000";
	$.scroller.bottom=PixelsToDPUnits(adMobView.size.height)+"dp";
});

 

 


$.ad.add(adMobView);
$.entrydetail.addEventListener('open',function(e){
	 adMobView.requestAd();	
	
	
});


function PixelsToDPUnits(ThePixels) {
	return (ThePixels / (Titanium.Platform.displayCaps.dpi / 160));
}

function DPUnitsToPixels(TheDPUnits) {
	return (TheDPUnits * (Titanium.Platform.displayCaps.dpi / 160));
}

$.entrydetail.addEventListener("close", function() {
	Alloy.Globals.popwin = false;
	$.destroy();
});

function closeMe(e) {

	$.entrydetail.close();
	$.destroy();
}
