// JavaScript Document
var COLLINS_PIPE = {};

var timerID = 0;
var newImgNumber = 0;
	
$(function(){
		   
		jQuery.COLLINS_PIPE.navigation();
		jQuery.COLLINS_PIPE.link_target('.xLink', '_blank');
		jQuery.COLLINS_PIPE.multi_tabs('#suppliersCategoryLinks', '.supplierCategory');
		//jQuery.COLLINS_PIPE.video_player('body.videoTour #flashVideo', 'rtmp://fvss.playstream.com/fvssod/visualconceptsinc/collins/collinsv4');
		//jQuery.COLLINS_PIPE.video_player('body.videoTour #flashVideo', 'http://collinspipe.visualconceptsinc.com/video/flv/collinsv4.flv');
		jQuery.COLLINS_PIPE.video_player('body.videoTour #flashVideo', '/video/flv/collinsv6_hi.flv', '/video/flv/collinsv6_low.flv', 10);
		
		jQuery.COLLINS_PIPE.cut_sheet_compactor();
		
		jQuery.COLLINS_PIPE.deleteAlert();
		
		/* ******* HOME IMAGE ROTATIONS ********* */
		var imgArray = new Array (
			'/images/header-images/home/home_01.jpg',
			'/images/header-images/home/home_09.jpg',
			'/images/header-images/home/home_05.jpg',
			'/images/header-images/home/home_07.jpg',
			'/images/header-images/home/home_04.jpg',
			'/images/header-images/home/home_10.jpg',
			'/images/header-images/home/home_11.jpg',
			'/images/header-images/home/home_12.jpg',
			'/images/header-images/home/home_13.jpg'			
		);
		
		jQuery.COLLINS_PIPE.rotator_init('body.home', '#headerImage', imgArray, 3000);
		
		
		/* ******* VALVES IMAGE ROTATIONS ****** */
		var imgArray = new Array (
			'/images/header-images/valves_5.jpg',
			'/images/header-images/valve_automaters.jpg'
		);
		
		jQuery.COLLINS_PIPE.rotator_init('body.valveAutomationCenter', '#headerImage', imgArray, 3000);
		
		
		
		$('#content.home #sideImages #ourStaff a.group_1 img').not('img:first').css({display : 'none'});
		setTimeout (
			function () {
				jQuery.COLLINS_PIPE.rotate('#content.home #sideImages #ourStaff a.group_1 img:first', 3000);
			} , 1000
		);
		
		$('#content.home #sideImages #ourStaff a.group_2 img').not('img:first').css({display : 'none'});
		setTimeout (
			function () {
				jQuery.COLLINS_PIPE.rotate('#content.home #sideImages #ourStaff a.group_2 img:first', 3000);
			} , 2000
		);
		
		//jQuery.COLLINS_PIPE.multiTabs('#faqsPage','#faqsNav', '.section');
		//jQuery.COLLINS_PIPE.Compactor('.faqSection', '.question', '.answer');
		//jQuery.COLLINS_PIPE.modal_hijack_link(document, '.enlarged');
		
		$('#upperNav a:first').css({background: 'none'});
		$('#headerQuote').css({ opacity: '0.85'});
		
		
		/* ****** ADMIN *** */
		jQuery.COLLINS_PIPE.page_admin_form();
		jQuery.COLLINS_PIPE.preview_page();
		
});

jQuery.COLLINS_PIPE = {

	navigation : function() {
		$('#navigation').find('li').children('ul').css({display: 'none'});
		$('#navigation').find('li').hover(
			function(){
				//$(this).children('a').not('.select').css({background: 'transparent url(/images/nav/nav_bkgd.jpg) repeat-x 0 -26px;', color: '#000'});
				$(this).children('ul').css({display: 'block'});
			},
			function() {
				//$(this).children('a').not('.select').css({background: 'transparent', color: '#FFF'});
				$(this).children('ul').css({display: 'none'});
			}
		);
	},
	
	multi_tabs : function(linkList, items) {
		
		$(items).css({display: 'none'});
		$(linkList).find('a').click(
			function() { 
				var href = $(this).attr('href');
				
				$(linkList).find("a").not(this).removeClass("select");
				$(this).addClass("select");
				
				$("div" + items +":visible").not(href).css({display: 'none'});
				$(href).css({display: 'block'});
				
				return false;
			}
		);
		
		if (location.hash.length > 0){
			var tempLink = $(location.hash+'_btn');
			
			if (tempLink.length > 0) {
				$(tempLink).click();	
			} else {
				$(linkList).find('a:first').click();
			}
		} else {
			$(linkList).find('a:first').click();
		}
	},
	
	
	Compactor : function ( list, btnLink, targetItem ) {
		
		$(list).find(targetItem).css({display: "none"});
		$(list).find(btnLink).addClass("link").hover( function () {
			$(this).addClass(" linkOver");
		}, function () {
			$(this).removeClass("linkOver");
		}).toggle( function () {
			$(this).siblings(targetItem+":visible").slideUp("fast");
			$(this).siblings(btnLink).removeClass("linkOn");
			$(this).addClass(" linkOn").next(targetItem+":hidden").slideDown("fast");
			
		}, function () {
			$(this).removeClass("linkOn").next(targetItem+":visible").slideUp("fast");
		});
		
		$(list).find(btnLink+':first').click();
	},
	
	
	cut_sheet_compactor : function () {
		var wrapper = $('#pdfList ul.level_1');
		
		if (wrapper.length > 0) {
			
			$(wrapper).find('ul').css({display: 'none'});
			
			$(wrapper).find('h4').append('<span>open</span>');
			
			$(wrapper).find('h4').toggle(
				function(){
					$(this).addClass('select');
					$(this).find('span').html('close');
					$(this).next('ul').show();
				},
				function(){
					$(this).removeClass('select');
					$(this).find('span').html('open');
					$(this).next('ul').hide();
				}						 
			).hover(
				function(){
					$(this).addClass('over');
				},
				function(){
					$(this).removeClass('over');
				}		
			);
		}
	},
	
	link_target : function (ID, target) {
		$(document).find(ID).each(
			function (){
				$(this).click(
					function (){
						var xLink = $(this).attr('href');
						window.open(xLink, target);
						return false;
					}
				);
			}
		);		
	},
	
	// CREATE A NEW METHOD AND PROPERTIES
 	modal_hijack_link : function( wrapper, trigger) 
	{
		// START AT THE TRIGGER <div> GET ALL THE CHILD <a> TAGS AND BUILD AN ARRAY TO HIGHJACK 
		$(wrapper).find('a'+trigger)
		
		// BY PASS ALL .delete BUTTONS TO PREVENT HIJACKLINK
		.not('.delete')
		
		// PREPARE FOR THE ONCLICK EVENT
		.click(function() {  
		
			// BUILD WIDTH AND HEIGHT FOR MODAL
			var W = $(window).width();
			var WH = $(window).height();
			var H = $('html').height();
			
			// HELPER FOR MODAL TO WORK ON SHORT PAGES
			if(H < WH){ 
				H = WH;
			}

			// INJECT THE MODAL INTO THE BODY TAG
			$("body").append('<div id="modal"></div>'); 
			
			// STYLE THE MODAL 
			$("#modal").css({display: "block", width: W, height: H, opacity: 0.7});		 

			// CREATE THE AHAH WINDOW TO EDIT THE RESULTS IN
			$("body").append('<div id="modalBox"></div>'); 
			
			// GET THE LINK TO HIJACK AS A STRING
			var urlJack = $(this).attr('href'); 
			var modalTitle = $(this).attr('rel');
			// STYLE THE AHAH WINDOW AND TRIGGER THE CANEL HIJACK
			var L = ($(window).width() - 630 ) / 2; // CENTER THE MODAL WINDOW IN THE VIEWPORT
			
			var modal = '<div id="modalTop">';
			if (modalTitle) modal += '<h1>'+modalTitle+'</h1>';
			modal += '<a id="cancelModal">Cancel</a>';
			modal += '</div><iframe src="" />';
			
			$("#modalBox").css({left: L}).append(modal);
			$('#modalBox iframe').attr({
				src: urlJack,
				width: 600,
				height: 600,
				frameborder: 0
			});
			
			jQuery.HOLTS.modal_cancel();
							
			// FOR IE6
			//$('html, body').animate({scrollTop:0}, 'slow'); 

		// DISABLE THE <a> TAG CUZ WE'RE HIJACKIN' IT
		return false; 
		
		});
		
	},
	
 	// CREATE A NEW METHOD AND PROPERTIES
	modal_cancel : function ()
	{
		$('#cancelModal').click(function(){
			$("#modalBox").remove();	
			$("#modal").css({display: 'none'});
		});
	},
		
	rotator_init : function(wrapper, container, image_array, speed)
	{
		var holder = $(wrapper).find(container);
		var array_length = image_array.length;
		
		// determine if container truely exists on page
		if (array_length > 0 && holder.length > 0){
		
			// interat through array and build images
			for (i=0; i < array_length; ++i) {
				$(holder).append('<img class="rotate" src="'+image_array[i]+'">');
			}
		
			// define starting image
			$(holder).find('img').not('img:first').css({display: 'none'});
			
			var img_start = $(holder).find('img:first');
			
			// begin rotation loop
			jQuery.COLLINS_PIPE.rotate(img_start, speed);
		}
	},
	
	rotate : function (image, speed)
	{
		// set time out for rotation loop
		var t = setTimeout(
			function () {
				var next_image = $(image).next();
				
				// fade image
				$(image).fadeOut(750,
					function () {
						// determine if there is a next image. if not return to first to loop to
						if (next_image.length > 0) {
							jQuery.COLLINS_PIPE.rotate(next_image, speed);
						} else {
							next_image = $(image).parent().find('img:first');
							jQuery.COLLINS_PIPE.rotate(next_image, speed);
						}
					}
				);
				
				// determine if there is a next image. if not return to first to fade in
				if (next_image.length > 0) {
					$(next_image).fadeIn(750);
				} else {
					$(image).parent().find('img:first').fadeIn(750);							
				}
				
			}, speed // loop based on speed in miliseconds
		);
	},
	
	deleteAlert : function()
	{
		$('.delete').click(function(){
		return confirm('Are you sure you want to delete this? It can not be undone.'); // OLD SCHOOL :)
		});
		
		$('.deleteFile').click(function(){
		return confirm('Are you sure you want to delete this file? It can not be undone.'); // OLD SCHOOL :)
		});
		
		$('.deleteFolder').click(function(){
		return confirm('Are you sure you want to delete this Folder? It will permanetly delete all files inside it. This can not be undone.'); // OLD SCHOOL :)
		});
	},
	
	video_player : function (ID, hi_res, low_res, buffer) {
		
		var holder = $(document).find(ID);
		
		if (holder.length > 0) {
			var flashvars = 'hi_res='+hi_res+'&low_res='+low_res+'&buffer='+buffer;
			
			$.post("/utilities/flash", { src:'/flash/videoPlayer.swf', width:766, height:460, flashvars: flashvars , version: 8, flash_id : 'headerVideo'},
				function(data){
					$(holder).html(data);
					$(holder).css({display: "block"});
				}
			);
		}
		
	},
	
	page_admin_form : function()
	{
		var form = $('#adminForm');
		
		if (form.length > 0) {
			var submission = $(form).attr('action');
			var btn = $(form).find('a.preview');
			
			if (btn.length > 0) {
				var page = $(btn).attr('rel');
				$(btn).attr({'href': page});
				
				$(btn).click(
					function (){
						$(form).attr({'action':page});
						
						var href = $(form).find('#href').attr('value');
												
						if (href && href.length > 0) {
							$(form).attr({'target':'_blank'});
						} else {
							$(form).attr({'target':'_self'});
						}
						
						$(form).submit();
						return false;
					}
				);
				
				$(form).find('input.submit').click(
					function (){
						$(form).attr({'action':submission, 'target':'_self'});
					}
				);			
			}
		}		
	},
		
	preview_page : function() 
	{
		if ($('#container').hasClass('previewPage')){
			$('a').not('.confirmPreview').click(
				function () {
					return confirm ('Are you sure you wish to navigate away from this page.  It is a preview of a page submission. If changes are not confirmed they will not be saved.');
				}				
			);
		}
	}
	
	
	
	
};









