var top_product;
var num_of_items;
var sub_dd_id;
var current_case_study = 1;

$(function(){
	
				/* Intial Re-scale on page load */
				var this_width;	
				this_width = $(window).width();
				this_width += 400;
				
				$("#backgroundImage img").css( { width:this_width+"px" } );
				
				$("#windowHeight").text(this_width);
				
				/* Re-scale when user rescales their browser window */
				$(window).resize(function() {
				
				this_width = $(window).width();
				this_width += 400;
				
				$("#backgroundImage img").css( { width:this_width+"px" } );
				
				$("#windowHeight").text(this_width);
				});
				
				setInterval("productGlance()",5000);  
				
				num_of_items = $("#itemContainer li").size();
				
				$('.brochure-popup img').click(function(){
																									
								$.unblockUI();																	
				});
				
				$('.slideshow').cycle({
								fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
				});
				
});

function validate_search() {
	
				var q = $( 'input[name=q]' ).val();
								
				if ( q == '' ) { $( 'input[name=q]' ).addClass('error'); $( 'input[name=placeholder]' ).val('TEST'); return false; }
				else { $( 'input[name=q]' ).removeClass('error'); return true; }
				
}


function showProductDD(){ $("#productDD").show(); $('nav ul li').eq(2).css( { background:"#EF7F1A" } )	}
function hideProductDD(){ $("#productDD").hide();	$("#subDD1,#subDD2,#subDD3").hide(); $('nav ul li').eq(2).css( { background:"none" } ) }

function showAboutDD(){ $("#aboutDD").show(); $('nav ul li').eq(3).css( { background:"#EF7F1A" } )	}
function hideAboutDD(){ $("#aboutDD").hide();	$("#subDD1,#subDD2,#subDD3").hide(); $('nav ul li').eq(3).css( { background:"none" } ) }

function showNewsDD(){ $("#newsDD").show(); $('nav ul li').eq(5).css( { background:"#EF7F1A" } )	}
function hideNewsDD(){ $("#newsDD").hide();	$('nav ul li').eq(5).css( { background:"none" } ) }

function showProductDD2(sub_dd_id){ $("#subDD1,#subDD2,#subDD3").hide(); $("#subDD"+sub_dd_id).show(); }

function productGlance(){
	   
	
				
  product_to_be_removed = $("#itemContainer li").eq(2);
  last_product_in_list = $("#itemContainer li").last();
  
  //Animation Phase 1 - fade out the product to be removed
  product_to_be_removed.animate({
								
		    opacity:0
									
  }, 1200, function() { /*product_to_be_removed.hide();*/ 
                
      //Animation Phase 2 - slide down
  			 last_product_in_list.animate({
								
				      height:0
											
  				}, 1000, function() {   
  										
					$("#itemContainer").prepend(last_product_in_list).show();
  
  					last_product_in_list.animate({
								
						opacity:1,
						height:'80px',
						top:'0px'
				
							
  				}, 1000, function() { /*product_to_be_removed.hide();*/ 
  
  					//$("#itemContainer").prepend(last_product_in_list).show(); 
					
					});
					
					});
  });
  			
  
}

function showNewsletter(){

				$( '.response-block' ).empty();
  		$.blockUI( { message:$('.newsletter-popup') } );

}

function closePopup(){

  $.unblockUI();

}

function change_hero(id) {
				
	 // change square
	 $( '.block' ).removeClass('current');
		$( '.block'+id ).addClass('current');
		
		// change left text
	 $( '.left-text' ).hide();
		$( '.left-text'+id ).show();
		
		// change product text
	 $( '.text' ).hide();
		$( '.text'+id ).show();
		
		// change hero image
	 $( '.hero-image' ).hide();
		$( '.hero-image'+id ).show();
		
		clearTimeout(hero_delay)
		
		hero_delay = setTimeout('cycle_hero('+id+')', 10000);
		
}

function cycle_hero(id) {
	
		if ( id == 5 ) {
				id = 1;
		}
		else {
			 id++;
		}
		
		//alert(id);
	
	 // change square
	 $( '.block' ).removeClass('current');
		$( '.block'+id ).addClass('current');
		
		// change left text
	 $( '.left-text' ).hide();
		$( '.left-text'+id ).show();
		
		// change product text
	 $( '.text' ).hide();
		$( '.text'+id ).show();
		
		// change hero image
	 $( '.hero-image' ).hide();
		$( '.hero-image'+id ).fadeIn(400);
		
		
				
		hero_delay = setTimeout('cycle_hero('+id+')', 5000);
				
}

cycle_hero(0);

function newsletter_process() {
	
				$( '.response-block' ).text('One moment...');
	
				var first_name = $( '.newsletter-first-name' ).val();
				var last_name = $( '.newsletter-last-name' ).val();
				var email = $( '.newsletter-email' ).val();
	
				$.post('ajax/newsletter.php', { first_name:first_name, last_name:last_name, email:email  }, function(data){
												
								$( '.response-block' ).text(data.response);
								
								if ( data.valid == 1 ) { 
												
												setTimeout($.unblockUI, 2500); 
												$( 'label.first-name, label.last-name, label.email ').hide(500);
												$( '.newsletter-first-name, .newsletter-last-name, .newsletter-email' ).val('').hide(500);
								}
																													
				}, 'json');
										
}

///////////////// EDIT CONTENT BLOCK ////////////////////
function edit_block(id) {
	
	 $.post('admin/edit-content.php', { id:id }, function(data){
				
				$( '.edit-content-box' ).show();
				CKEDITOR.instances.pageContent.setData(data.content);
				
				$( '.element_id' ).val(id);
				
		}, 'json');
}

///////////////// SAVE CONTENT BLOCK ////////////////////
function save() {
	
	 var id = $( '.element_id' ).val();
	
		$( '.editable'+id ).text( 'Updating...' );
	
		$.post('admin/save-content.php', { id:id,
																															content:CKEDITOR.instances.pageContent.getData() }, function(data){
				
				$( '.edit-content-box' ).hide();
				$( '.editable'+id ).html( data.new_content );
												
		}, 'json');
}

///////////////// NEWS BOX ////////////////////
var page;

function cms_go(page) {
	
				$('.news-box div').hide();
				$('.products-box div').hide();
				$('.case-study-box div').hide();
				
				$('.'+page).show();
				$('.'+page+' .page1').show();
}

function add_news() {
	
				var title = $( '.add-news-title' ).val();
				var content = CKEDITOR.instances.news_content.getData()
				
				if (title == '' || content == '' ) {
					
							alert( 'Please complete all fields.' );				
				}
				else {
				
								$.post('admin/cms-news.php', { action:'add', title:title, content:content }, function(data){
								
								alert('News added successfully');
								$( '.news-box' ).hide();
												
								}, 'json');
				}
}

function edit_news(news_id) {
					
				$( '.news_id' ).val(news_id);	
					
				$.post('admin/cms-news.php', { action:'edit', id:news_id }, function(data){
																
								$( '.edit-news-title' ).val(data.title);
								CKEDITOR.instances.edit_news_content.setData(data.content);
								$( '.edit-news-archive-date' ).val(data.archive_timestamp);
								
								cms_go("news-box .page3");
												
				}, 'json');
				
				
}

function remove_news(news_id) {
									
				$.post('admin/cms-news.php', { action:'remove', id:news_id }, function(data){
									
									alert('News article removed successfully.')
									$( 'li.row'+news_id ).remove();
												
				}, 'json');
				
				
}

function edit_news2(news_id) {
					
				var news_id = $( '.news_id' ).val();	
				var title = $( '.edit-news-title' ).val();
				var content = CKEDITOR.instances.edit_news_content.getData()
				var archive_date = $( '.edit-news-archive-date' ).val();
									
				$.post('admin/cms-news.php', { action:'edit2', id:news_id, title:title, content:content, archive_date:archive_date }, function(data){
								
								alert('News edited successfully!');
								$( '.news-box' ).hide();
												
				}, 'json');
				
				
}

var _id;

/**********CMS PRODUCTS JS**********/
function add_product() {
	
				var name = $( '.add-product-name' ).val();
				var description = CKEDITOR.instances.product_description.getData()
				
				if (name == '' || description == '' ) {
					
							alert( 'Please complete all fields.' );				
				}
				else {
				
								$.post('admin/cms-products.php', { action:'add', name:name, description:description }, function(data){
								
								alert('Product added successfully');
								$( '.products-box' ).hide();
								
								window.location.reload()
												
								}, 'json');
				}
}

function edit_product(_id) {
					
				$( '.product_id' ).val(_id);	
					
				$.post('admin/cms-products.php', { action:'edit', id:_id }, function(data){
																
								$( '.edit-product-name' ).val(data.name);
								CKEDITOR.instances.edit_product_description.setData(data.description);
								
								cms_go("products-box .page3");
												
				}, 'json');
				
				
}

function remove_product(_id) {
									
				$.post('admin/cms-products.php', { action:'remove', id:_id }, function(data){
									
									alert('Product removed successfully.')
									$( 'li.row'+_id ).remove();
									
									window.location.reload()
												
				}, 'json');
				
				
}

function edit_product2(_id) {
					
				var _id = $( '.product_id' ).val();	
				var name = $( '.edit-product-name' ).val();
				var description = CKEDITOR.instances.edit_product_description.getData()
					
				$.post('admin/cms-products.php', { action:'edit2', id:_id, name:name, description:description }, function(data){
																
								alert('Product edited successfully!');
								$( '.products-box' ).hide();
								
								window.location.reload()
												
				}, 'json');
				
				
}

///////// CMS CASE STUDY JS ///////////

var case_study_id;

function add_case_study() {
	
				var title = $( '.add-case-study-title' ).val();
				var content = CKEDITOR.instances.case_study_content.getData()
				
				if (title == '' || content == '' ) {
					
							alert( 'Please complete all fields.' );				
				}
				else {
				
								$.post('admin/cms-case-studies.php', { action:'add', title:title, content:content }, function(data){
								
								alert('Case study added successfully');
								$( '.case-study-box' ).hide();
												
								}, 'json');
				}
}

function edit_case_study(case_study_id) {
					
				$( '.case_study_id' ).val(case_study_id);	
					
				$.post('admin/cms-case-studies.php', { action:'edit', id:case_study_id }, function(data){
																
								$( '.edit-case-study-title' ).val(data.title);
								CKEDITOR.instances.edit_case_study_content.setData(data.content);
								
								cms_go("case-study-box .page3");
												
				}, 'json');
				
				
}

function remove_case_study(case_study_id) {
									
				$.post('admin/cms-case-studies.php', { action:'remove', id:case_study_id }, function(data){
									
									alert('Case study removed successfully.')
									$( 'li.row'+case_study_id ).remove();
												
				}, 'json');
				
				
}

function edit_case_study2(case_study_id) {
					
				var case_study_id = $( '.case_study_id' ).val();	
				var title = $( '.edit-case-study-title' ).val();
				var content = CKEDITOR.instances.edit_case_study_content.getData()
					
				$.post('admin/cms-case-studies.php', { action:'edit2', id:case_study_id, title:title, content:content }, function(data){
																
								alert('Case study edited successfully!');
								$( '.case-study-box' ).hide();
												
				}, 'json');
				
				
}


function exit() {
					
				$( '.edit-content-box, .news-box, .products-box, .case-study-box' ).hide();
						
}

function cycle_case_study(direction) {
	
				// user has clicked left
				if ( direction == 0 ) {
					
								if ( current_case_study == 1 ) { current_case_study = 5; }
								else { current_case_study--; }
								
								$( '.case-study-title, .case-study-text, .case-studies-widget-image' ).hide();
								$( '.case-study-title'+current_case_study+', .case-study-text'+current_case_study+', .case-studies-widget-image'+current_case_study+''  ).show();
					
				}
				else {
	
								if ( current_case_study == 5 ) { current_case_study = 1; }
								else { current_case_study++; }
								
								$( '.case-study-title, .case-study-text, .case-studies-widget-image' ).hide();
								$( '.case-study-title'+current_case_study+', .case-study-text'+current_case_study+', .case-studies-widget-image'+current_case_study+''  ).show();
				
				}
}

function show_brochure() {
	
				$.blockUI( { message:$('.brochure-popup') } );
}

function validate_form() {
												
				var first_name = $( '#first-name' ).val();
				var last_name = $( '#last-name' ).val();
				var email = $( '#email' ).val();
				
				//alert(first_name + last_name + email);
				
				if ( first_name == '' || last_name == '' || email == '' ) {
				
								alert( 'Please complete all fields.' );
								return false;
				}
				else {
				
								return true;
				}
		
			
}



