jQuery.noConflict();

var parents=new Array(); 

var currentMedia		= '';	



var currentImage		= null; //currentimage set line 23 in /Securikey.co.uk/modules/mod_securikey_vm_cats/tmpl/default.php

var currentCat			= '';   //

var flagUp				= null;

var displayed			= 0;



var currentLock			= '';	//used when changing Locking Instructions

var currentVideoCat		= 'Safes';	//used when changing Video

var linkClick			= 0;

var origColour			= '';

var ImageLibraryCurrent = null; 

var count				= 0;

var tableRowColourSet	= ''; // this is set in html via /Securikey.co.uk/plugins/content/securikey_vm_table.php line 266





/* variables for the colours - these should be the same as in the css */

var cssTableBackground	= '#fff';

var cssRowSelected		= '#f0f0f0'; //"#D4161C";

/**

* Desc set the js so we know a link has been clicked

* global var linkClick

*/





/////////////////////////////////////////////////////////////////////////////////////////////

// category page table initialise

//

jQuery(document).ready(function() {     							   

		jQuery(".vm-table-link").click(function() {	

		linkClick = 1;				

	}); 

	if(tableRowColourSet!=''){

	jQuery('.' + tableRowColourSet).css("background",cssRowSelected);

	}	

});



/////////////////////////////////////////////////////////////////////////////////////////////



/**

* this section is required for the left hand module

* slide down a category to reveal products within that category

* File of interest /Securikey.co.uk/modules/mod_securikey_vm_cats/helper.php

*/

jQuery(document).ready(function() {     							   

	jQuery(".vm-cat-item-display").click(function() {	

		if(this.id==currentCat){

			return false;

		}		

		jQuery( '#'+ this.id + '-item' ).slideToggle();	

		jQuery( '.'+ this.id + '-ula' ).slideToggle();			

	});	

	jQuery(".vm-cat-item-display").toggle(function() {

		if(this.id==currentCat){

			return false;

		}	

		this.src = '/templates/standard/images/arrow1.gif';
        this.title = 'hide';
        this.alt = 'hide';
        
	}, function() {

		if(this.id==currentCat){

			return false;

		}		

	  this.src = '/templates/standard/images/arrow.gif';
      this.title = 'show';
      this.alt = 'show';
      

	});	

	jQuery(".selected + img").css("cursor","default");

	

});





///////////////////////////////////////////////////////////////////



/**

* This section changed the the image for left hand module

* file of interest /Securikey.co.uk/modules/mod_securikey_vm_cats/helper.php and 

* /Securikey.co.uk/modules/mod_securikey_vm_cats/tmpl/helper.php

*/

jQuery(document).ready(function() {	

	jQuery("tr.vm-table-row").click(function() {

	   jQuery(this).siblings().css("background", "");

	   jQuery(this).css("background", cssRowSelected);

	   

	   image_to_show = jQuery(this).attr("id").replace('vm-row-id-','');

	   image_to_show = image_to_show.replace('z','');

	   

	   if(currentImage!=image_to_show){

    	   jQuery("#vm-image-" + currentImage).fadeOut(function(){		

    			jQuery("#vm-image-" + image_to_show).fadeIn();

    			currentImage = image_to_show;

    			flagUp = 0;

    		});

    	}

	});		

});



////////////////////////////////////////////////////////////////////////

/**

* the table featured on certain category pages will need hovering on mouse over

* code below take care of that

* @todo needs optimising!!

*/

jQuery(document).ready(function() {	

	jQuery(".vm-table-row").hover(

        function() {

            jQuery(this).addClass("hover");

	    },

	    function() {

            jQuery(this).removeClass("hover");

	});

});

/////////////////////////////////////////////////////////////////////////

/**

* media centre - videos

**/

jQuery(document).ready(function() {	

	

	jQuery("#media-centre li").click(function() {	

	

		var catName 		= this.className;

		var catHeader		= jQuery(this).text();		

		var catNameNew	   	= '';

		catNameNew	  		= catName.replace('cat-','video-');

		

		

		if(catName == currentVideoCat || flagUp==1){

			return false;

		}



		jQuery(".video-" + currentVideoCat).fadeOut(function(){

			flagUp = 1;

			jQuery("." + catNameNew).fadeIn();

			currentVideoCat	= catName.replace('cat-','');

			jQuery("#cat-name").text(catHeader);			

			flagUp = 0;			

		});		 	

	});

});

/////////////////////////////////////////////////////////////////////////

/**

* media centre - locking 



	There is a top list with UL id is media-centre-locking

	this has LIs with ids doc-xxx

	the lower list has associated LIs with class=xxx

**/





// media center initialise

//

jQuery(document).ready(function() {   

	jQuery("#media-centre-locking .start").each(function() {

		showLockingInfo(this);

	});

	 

});



// whenever the folder is clicked, display the results

jQuery(document).ready(function() {	

	

	jQuery("#media-centre-locking li").click(function(){

		showLockingInfo(this);

	});		 	

	

});



//this function shows the locking options associated with the clicked folder

function showLockingInfo(obj){



	if(!obj) return false;

	

	var catName 		= obj.className;

	var catHeader		= jQuery(obj).text();

	

	//the LIs associated with the upper list

	var catToShow	   	= '';

	catToShow	  		= catName.replace('doc-','');

	catToShow	  		= catToShow.replace(' start','');

			

	if(catToShow == currentLock || flagUp==1){

		return false;

	}

	

	flagUp = 1;



	if(currentLock==''){

		//there is nothing currently set up

			//fade in the new category

			jQuery("." + catToShow).fadeIn();

			//set the heading text

			jQuery("#doc-name").text(catHeader);	

	} else {

		jQuery("." + currentLock).fadeOut(function(){

			//fade in the new category

			jQuery("." + catToShow).fadeIn();

			//set the heading text

			jQuery("#doc-name").text(catHeader);	

			

		});

	};

	currentLock = catToShow;	

	flagUp = 0;			

	

}



///////////////////////////////////////////////////////////////////////////////////////////////////////*

/**

* Change the video that is playing....  



* the declaration of youtubeVideoReplace is in /components/mediacentre.php

* need to replace the video in order to refresh the browser

*/



jQuery(document).ready(function() {		

	jQuery(".media-centre-videos").click(function() {

		var url = youtubeVideoReplace.replace('{replace}',this.id);

		jQuery('#youtubeParam').attr('value', url  );

		jQuery('#youtubeSource').attr('src' , url );

		

		var cloney = jQuery('#media-centre-video').clone();

		jQuery('#media-centre-video').replaceWith(cloney);

		return false;

	});

});

/*

*/

////////////////////////////////////////////////////////////////////////////////////////////

/**

* image library require lists to be slid out as they have been set to hidden because they sub lists

*/



jQuery(document).ready(function() {	

	jQuery("ul li.slide-open").click(function(e) {	

		

 		e.stopPropagation();

		var ulID = jQuery(this).attr('id');

		//jQuery("#"+ulID + " ~ ul").slideToggle();



		var tag = 'ul';

	    element =  jQuery('#'+ulID);



	    var aElements = jQuery(tag,element.parent());

	    var index = (aElements.index(element));

	

	    for(i=(index+1);i<aElements.length;i++) {

	        jQuery('#'+jQuery(aElements.get(i)).attr('id')).slideToggle();

	    }		

		if(origColour!=''){

			origColour = jQuery("#"+ulID).css("color");

		}

		jQuery("#"+ImageLibraryCurrent).css({"color":origColour});

		ImageLibraryCurrent = ulID;		

		

		var docId	= ulID.replace('doc-','');



		if((docId==currentMedia && displayed==1) || flagUp==1){

			

			return false;

		}

		// 

		if(!parents[currentMedia]){	

			jQuery("." + currentMedia).fadeOut(function(){

				flagUp = 0;	

				jQuery("." +docId).fadeIn('fast');

				displayed 	 = 1;

				flagUp		 = 0;

			});

		}

		else if(!currentMedia){			

			jQuery("." + docId).fadeIn(function(){

				flagUp = 0;	

				displayed 	 = 1;

				flagUp		 = 0;

			});	

		}		

		else{

			jQuery("." + currentMedia).fadeIn(function(){

				flagUp = 0;	

				jQuery("." +docId).fadeIn('fast');

				displayed 	 = 1;

				flagUp		 = 0;

			});		

		}

		

		currentMedia = docId;

		count++;	

		return false;			

	});

});



/**

* display image on the fly, if its a pdf open in new window

**/

////////////////////////////////////////////////////////////////

jQuery(document).ready(function() {	

	jQuery(".image-library-doc").click(function() {

		var myRegExp = /.pdf/;

		var link = jQuery('#'+jQuery(this).attr('id')).attr('href');

		var sku = jQuery('#'+jQuery(this).attr('id')).text();

		var matchPos1 = link.search(myRegExp);

		if(matchPos1==-1){

			jQuery('#myImage').css('visibility','visible');		

			jQuery('#myImage').attr('src', link).load(function() {

				jQuery('#imageLibraryLink').attr('href', link);				

 			});

 			jQuery('#myImageText').text('');	

 			var url = '/index.php?option=com_imagelibrary&sku='+sku+'&do=getimagedetails';		

 			jQuery.ajax({

				url: url,

				dataType: 'json',

				success: function(data) {	

					if(data[0]['product_s_desc']!='null'){	

						jQuery('#myImageText').html('<p>'+data[0]['product_s_desc']+'</p><p> Find out more about <a href="'+data[0]['link']+'" alt="'+sku+'" >'+data[0]['product_name']+'</a> </p>');	

					}		

 				}

			});			

 		}else{

 			return true;

 		}	

	return false;

	});

});



/**

* This is the ajax for admin.

* It updates the cat_menu fields, which hold the menu html, whenever the button is clicked.

**/

////////////////////////////////////////////////////////////////

function ajaxmenuclick(id){

	

	var spinner = document.getElementById('spinner');

	var url = '/administrator/index.php?option=com_virtuemart&menuajaxcall=1&do=vmextra&category_id='+id;

	jQuery(spinner).show();	

	jQuery.ajax({

		url: url,

		dataType: 'json',

		success: function(data) {	

			jQuery(spinner).hide();

		},

		fail: function(data) {	

			alert('the menu has not been updated');	

			jQuery(spinner).hide();

		}

	});	

	

	return false;	

}




