/*  jJSON Object  */
var jJSON_page = {};
var jJSON_push = {};
 
/* methods fill date */
var displayPush = {
	
	linkJson: "",
	rotation: 0,
	time : 6000,
	inv_cur : false,
	
	// init methode
	init : function(){
		this.linkJson = JSONPushIndex;
		// call getJson methode
		this.getJson();		
	},
	
	getJson : function(){
		
		jQuery.getJSON(displayPush.linkJson, function(jsonpush) { 
			
			// store page config in object jJSON_page
			jJSON_page["page"] = (function() {
				response = {
					page_title: [],
					desc_page: [],
					lib_next: [],
					low: [],
					rotation:[],
					inv_cur:[]
				};
				
				jQuery.each(jsonpush,function(i,item) {
					if(item['title'])	{									
						response.page_title = item['title'];
					}else{
						response.page_title ="";
					}
					if(item['desc']){
						response.desc_page = item['desc'];
					}else{
						response.desc_page = "";
					}
					if(item['lib-url']){
						response.lib_next = item['lib-url'];
					}else{
						response.lib_next = "";
					}
					if(item['low']){
						response.low = item['low'];
					}else{
						response.low = "";
					}
					
					if(item.config){
						jQuery.each(item.config,function(j,itemtheme) {
							if( itemtheme.rotation){
								response.rotation = itemtheme.rotation;	
							}else{
								response.rotation	= displayPush.rotation;
							}
							if(itemtheme['inv-cur']){
								response.inv_cur  = itemtheme['inv-cur'];
							}else{
								response.inv_cur  = displayPush.inv_cur;
							}
						});
					}else{
						response.rotation	= displayPush.rotation;
						response.inv_cur  = displayPush.inv_cur;
					}
				});

				return response;
			})();
			
			// store pushs in object jJSON_push
			jJSON_push["push"] = (function() {
				response = {
							title: [],
							desc: [],
							theme: [],
							subtitle: [],
							img:[],
							amount:[],
							cur:[],
							url:[]
						};
				
				jQuery.each(jsonpush['push-theme']['push'],function(i,itemsP) {	
																	
						response.title[i] = itemsP['title']?itemsP['title']:'';
						response.desc[i] = itemsP['desc']?itemsP['desc']:'';
						response.theme[i] = itemsP['theme']?itemsP['theme']:'';
						response.subtitle[i] = itemsP['subtitle']?itemsP['subtitle']:'';
						response.img[i] = itemsP['img']?itemsP['img']:'';
						response.amount[i] = itemsP['amount']?itemsP['amount']:'';
						response.cur[i] = itemsP['cur']?itemsP['cur']:'';
						response.url[i] = itemsP['url']?itemsP['url']:'';
					
				});
				return response;
			})();
			
			// display html output
			function displayData(){
				var $html = "";
				var price = "";
				var displayNavigator ="";
					
				// build html output	
				for(v =0; v < jJSON_push['push']['title'].length; v++){
					if(jJSON_page["page"]['inv_cur'] == 0){
						price = '<span class="amount">'+jJSON_push['push']['amount'][v]+'</span><span class="currency">'+jJSON_push['push']['cur'][v]+'</span>';	
					}else{
						price = '<span class="currency">'+jJSON_push['push']['cur'][v]+'</span><span class="amount">'+jJSON_push['push']['amount'][v]+'</span>';	
					}
					
					$html += '<li><img src="'+jJSON_push['push']['img'][v]+'" title="" alt="'+jJSON_push['push']['title'][v]+'"><div class="content">   <h2 class="titleOffer">'+jJSON_push['push']['title'][v]+'<span>'+jJSON_push['push']['desc'][v]+'</span></h2><p class="pricePackage">'+jJSON_page["page"]['low']+' <span class="price">'+price+'</span><span class="star">*</span><br /><a href="'+jJSON_push['push']['url'][v]+'">'+jJSON_page["page"]['lib_next']+'</a></p></div></li>';
					
				}
				
				// build html tabs
				jQuery.each(jJSON_push,function(i,itemT) {	
					for(var j =0; j < itemT.title.length ; j++){
						displayNavigator += '<li>';
						displayNavigator += '<span class="title">'+itemT['theme'][j]+'</span>';
						displayNavigator += '<span>'+itemT['subtitle'][j]+'</span>';
						displayNavigator += '</li>';
					}
				});
				
				// insert html ind appropriate dom elements
				jQuery('h1').html(jJSON_page['page']['page_title']);
				jQuery('p.textTab').html(jJSON_page['page']['desc_page']);
				jQuery('.carrouselContent').html($html);
				jQuery('.carrouselNavigator').html(displayNavigator);
				
				// init auto rotation
				var retationAuto = false;
				if(jJSON_page["page"]['rotation'] == 1)  retationAuto = true; else retationAuto = false;
				
				// call lofJSidernews methode
				jQuery('#carrousel').lofJSidernews( { interval:6000,	duration:700,auto:retationAuto } );						
			}
			
			// display html output
			displayData();
		});		
	}
}

// call init methode whene DOM loaded
jQuery(document).ready(function(){ 
	displayPush.init();	
});

