/**
 * Convert a single file-input element into a 'multiple' input list
 *
 * Usage:
 *
 *   1. Create a file input element (no name)
 *      eg. <input type="file" id="first_file_element">
 *
 *   2. Create a DIV for the output to be written to
 *      eg. <div id="files_list"></div>
 *
 *   3. Instantiate a MultiSelector object, passing in the DIV and an (optional) maximum number of files
 *      eg. var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
 *
 *   4. Add the first element
 *      eg. multi_selector.addElement( document.getElementById( 'first_file_element' ) );
 *
 *   5. That's it.
 *
 *   You might (will) want to play around with the addListRow() method to make the output prettier.
 *
 *   You might also want to change the line
 *       element.name = 'file_' + this.count;
 *   ...to a naming convention that makes more sense to you.
 *
 * Licence:
 *   Use this however/wherever you like, just don't blame me if it breaks anything.
 *
 * Credit:
 *   If you're nice, you'll leave this bit:
 *
 *   Class by Stickman -- http://www.the-stickman.com
 *      with thanks to:
 *      [for Safari fixes]
 *         Luis Torrefranca -- http://www.law.pitt.edu
 *         and
 *         Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com
 *      [for duplicate name bug]
 *         'neal'
 */
 
function MultiSelector( list_target, max, button_label2 ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	}
	//text for the delete button
	var button_label3 = button_label2;
	
	/**
	 * Add a new file input element
	 */
	this.addElement = function( element ){

		    

			// Element name -- what number am I?
			element.name = "file_" + this.id++;

			// Add reference to this object
			element.multi_selector = this;

			// What to do when a file is selected
			element.onchange = function(){

				// New file input
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';

				// Add new element
				this.parentNode.insertBefore( new_element, this );

				// Apply 'update' to element
				this.multi_selector.addElement( new_element );

				// Update list
				this.multi_selector.addListRow( this );

				// Hide this: we can't use display:none because Safari doesn't like it
				this.style.position = 'absolute';
				this.style.left = '-1000px';

			}
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			}

			// File element counter
			this.count++;
			// Most recent element
			this.current_element = element;



	}

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element ){

		// Row div
		var new_row = document.createElement( 'div' );
//                new_row.style.float =  'right';
                
//                alert(new_row.innerHTML);
		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = button_label3;
//                element.innerHTML += '<br style = "clear: both" />';
		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.multi_selector.current_element.disabled = false;

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		}

		// Set row value
		new_row.innerHTML = element.value;

		// Add button
		new_row.appendChild( new_row_button );

		// Add it to the list
		this.list_target.appendChild( new_row );

	}

}

function addToFavorites(urlAddress , pageName) 
{ 
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(pageName , urlAddress, "");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( urlAddress, pageName); 
	} else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}  

function showmenu(el) {
	if (document.getElementById(el).style.display == "none") {
		document.getElementById(el).style.display="block";
	} else {
		document.getElementById(el).style.display="none";
	}
}

function change_currency( oDiv )
{
	var currency_id = 2;
	switch( oDiv){

		case 'gbr_div' :
			currency_id = 2;
		break;
		
		case 'euro_div' :
			currency_id = 4;
		break;
		
		case 'dollar_div' :
			currency_id = 1;
		break;
		
		case 'bgn_div' :
			currency_id = 3;
		break;
	}
	
	document.getElementById( 'old_currency_d' ).value 		= 1;
	document.getElementById( 'currency_d' ).value 		= currency_id;
	document.getElementById( 'gbr_div' ).className 		= 'prices';
	document.getElementById( 'euro_div' ).className 	= 'prices';
	document.getElementById( 'dollar_div' ).className 	= 'prices';
	document.getElementById( 'bgn_div' ).className 		= 'prices';
	document.getElementById( oDiv ).className 			= 'prices-selected';
}

function showPicGaleryPopup( url )
{
	var winl = (screen.width - 850) / 2;
	var wint = (screen.height- 650) / 2;
	
	var dlg = window.open(url, "pic_galery",
		      "toolbar=no,menubar=no,personalbar=no,width=850,height=650," +
		      "scrollbars=yes,resizable=no,top=" + wint + " , left = " + winl + "");
	
	return false;	      
}

function showPrintPopup( url )
{
	var winl = (screen.width - 700) / 2;
	var wint = (screen.height- 650) / 2;
	
	var dlg = window.open(url, "pic_galery",
		      "toolbar=no,menubar=no,personalbar=no,width=700,height=650," +
		      "scrollbars=yes,resizable=no,top=" + wint + " , left = " + winl + "");
	
	return false;	      
}

function showAddNotesForm( url ) {
	var winl = (screen.width - 850) / 2;
	var wint = (screen.height- 650) / 2;
	
	var dlg = window.open(url, "add_notes",
		      "toolbar=no,menubar=no,personalbar=no,width=600,height=400," +
		      "scrollbars=yes,resizable=no,top=" + wint + " , left = " + winl + "");
	
	return false;
}

function showImage( url ) {
	var winl = (screen.width - 850) / 2;
	var wint = (screen.height- 650) / 2;
	
	var dlg = window.open(url, "image",
		      "toolbar=no,menubar=no,personalbar=no,width=600,height=400," +
		      "scrollbars=yes,resizable=yes,top=" + wint + " , left = " + winl + "");
	
	return false;
}

function showPostAd( url ) {
	var winl = (screen.width - 550) / 2;
	var wint = (screen.height- 250) / 2;
	
	var dlg = window.open(url, "image",
		      "toolbar=no,menubar=no,personalbar=no,width=550,height=250," +
		      "scrollbars=yes,resizable=no,top=" + wint + " , left = " + winl + "");
	
	return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document;if(d.images){if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){d.MM_p[j]=new Image;d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;if(!d) d=document;if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n];for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments;document.MM_sr=new Array;for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;if(!x.oSrc) x.oSrc=x.src;x.src=a[i+2];}
}


/*function switchImages(new_image, new_link, clicked_where, default_pic, selected_pic)*/
function switchImages(new_image, new_link)
{
	/*document.getElementById('image_front').src		= default_pic
	document.getElementById('image_back').src		= default_pic
	document.getElementById('image_full').src		= default_pic
	document.getElementById('image_detail').src		= default_pic
	document.getElementById(clicked_where).src		= selected_pic*/
	document.getElementById('main_pic').src			= new_image;
	var obj = document.getElementById('main_pic_link');
	if (obj) {
		obj.href = new_link;
	}
}

