// JavaScript Document

    function SetTab(active)
    {
        document.getElementById("tab1").setAttribute("class", "inactiveTab");
        document.getElementById("tab2").setAttribute("class", "inactiveTab");
        document.getElementById("tab3").setAttribute("class", "inactiveTab");

        document.getElementById("tabContent1").setAttribute("class", "invisibleTabContent");
        document.getElementById("tabContent2").setAttribute("class", "invisibleTabContent");
        document.getElementById("tabContent3").setAttribute("class", "invisibleTabContent");
        
        if (active==1){
            document.getElementById("tab1").setAttribute("class", "activeTab");
            document.getElementById("tabContent1").setAttribute("class", "visibleTabContent");
            document.getElementById("tabContentAll").style.overflow="";
        }
        if (active==2){
            document.getElementById("tab2").setAttribute("class", "activeTab");
            document.getElementById("tabContent2").setAttribute("class", "visibleTabContent");
            document.getElementById("tabContentAll").style.overflow="auto";
        }
        if (active==3){
            document.getElementById("tab3").setAttribute("class", "activeTab");
            document.getElementById("tabContent3").setAttribute("class", "visibleTabContent");
            document.getElementById("tabContentAll").style.overflow="auto";
        }
    }

    function hideelement(i) {

        var e = document.getElementById(i);
        if (e) {
        e.style.display = 'none';
        }
    }
    function showelement(i) {
        var e = document.getElementById(i);
        if (e) {
        e.style.display = 'block';
        }
    } 


function getParameter ( parameterName ) {
    var queryString = window.top.location.search.substring(1);
   // Add "=" to the parameter name (i.e. parameterName=value)
   var parameterName = parameterName + "=";
   if ( queryString.length > 0 ) {
      // Find the beginning of the string
      begin = queryString.indexOf ( parameterName );
      // If the parameter name is not found, skip it, otherwise return the value
      if ( begin != -1 ) {
         // Add the length (integer) to the beginning
         begin += parameterName.length;
         // Multiple parameters are separated by the "&" sign
         end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
         end = queryString.length
      }
      // Return the string
      return unescape ( queryString.substring ( begin, end ) );
   }
   // Return "null" if no parameter has been found
   return "null";
   }
}

$(document).ready(function(){

	          
    //doing our auto-init stuff
	base_url=$('base').attr('href');
	//homepage featured images

    //$('#editorDiv').draggable();
    
    
    $('.fade').innerfade({ speed: 1000, timeout: 6500, type: 'sequence', containerheight: '280px'}); 
    
   $("#uploadify").uploadify({
		'uploader'       : 'uploadify.swf',
		'script'         : 'adminGalleryMedia.aspx?gid='+getParameter('galleryID')+'&eid='+getParameter('eid'),
		'cancelImg'      : 'images/cancel.png',
		'folder'         : 'media',
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'multi'          : true,
		onAllComplete: function() {
                location.href=window.top.location.href;
        }
	});
   
   
    $('.topNavLink').each(function() {
    
        $(this).mouseenter(function(){
            //show submenu
            $('.popDownMenuItems', $(this).parent()).show();
        });  
    });


    $('.popDownMenu').each(function(){
        //add the show/hide pop out menu for top nav
        
        
    });
    
    $('.popDownMenu').mouseleave(function() {
            //hide submenu
            $('.popDownMenuItems', this).hide();
    });

	var active=0;
	var stopped=0;

	$('#mainImages').each(function(){

		//preparing the iframe preloader - (c) brainchild@gwsol.com.ua
		$('body').append('<iframe src="preload.html" frameborder="0" width="0" height="0"></iframe>');

		//homepage next/prev navigation
		$('#imageNavPrev').click(function(){
			stopped=1;
			switchPrev()
		});

		$('#imageNavNext').click(function(){
			stopped=1;
			switchNext()
		});


		window.switchPrev=function(){
			if(active==0){
				active=$('#mainImages A').length-1;
			}
			else{
				active--;
			}
			$('#mainimgSection>A').each(function(){
				clone=$('#mainImages A').clone().get(active);
				$('#mainimgDescription').html($(clone).attr('title'));
				$(this).attr('href',$(clone).attr('href'));
				$('IMG',this).hide().attr('src',$('IMG',clone).attr('src')).fadeIn('slow');
			})

		}


		window.switchNext=function(){
		
//			if(active==$('#mainImages A').length-1){
//				active=0;
//			}
//			else{
//				active++;
//			}
//			$('#mainimgSection>A').each(function(){
//				clone=$('#mainImages A').clone().get(active);
//				$('#mainimgDescription').html($(clone).attr('title'));
//				$(this).attr('href',$(clone).attr('href'));
//				$('IMG',this).hide().attr('src',$('IMG',clone).attr('src')).fadeIn('slow');
//			})

            if(active==$('#mainImages IMG').length-1){
				active=0;
			}
			else{
				active++;
			}
			$('#mainimgSection>IMG').each(function(){
			    clone=$('#mainImages IMG').clone().get(active);
			    //$('#mainimgDescription').html($(clone).attr('title'));
				//$(this).attr('href',$(clone).attr('href'));
				$(this).hide().attr('src',$(clone).attr('src')).fadeIn(2000);
			});

			if(!stopped){
				window.setTimeout("window.switchNext()",7000);
			}
		}

		window.setTimeout("window.switchNext()",7000);
	});


    
	$('A.openpopup').click(function(){
	    $('.popupWindow.longer').css('display','block');
    		
		    return false;
	    });
    	
	    $('A.btn.closepopup').click(function(){
		    $('.popupWindow.longer').css('display','none');

	    });
    });



	//registration stuff

	$('#registration').each(function(){
		$('.mandatory',this).each(function(){
			if(!$(this).hasClass('error')){
				$(this).autotip({'text':'Required'});
			}
		});

		$('A.btn.submit').click(function(){
			$('#registration').submit();
			return false;
		})

		$('A.btn.save').click(function(){
			$('#registration').submit();
			return false;
		})

		$('A.btn.cancel').click(function(){
			history.back();
		})

		//removing placholding messages prior to submitting the form
		$(this).submit(function(){
			$('.mandatory').each(function(){
				if($(this).val()=='Required') $(this).val('');
			});

			
		});

	
		//minor things for the checkout process
		$('#userUseBillingAddress').change(function(){
			if($('#userUseBillingAddress:checked').size()==0){
				$('#shippingInformation').fadeIn('slow');
			}
			else{
				$('#shippingInformation').fadeOut('fast');
			}
		})




	});

	$('#login').each(function(){
		$('A.btn.submit').click(function(){
			$('#login').submit();
			return false;
		});
	})

	$('#advancedSearch').each(function(){
		$('A.btn.submit').click(function(){
			$('#advancedSearch').submit();
			return false;
		});
	})



	$('#contact').each(function(){
		$('A.btn.submit').click(function(){
			$('#contact').submit();
			return false;
		})
	})


	//adding limits to limited length inputs
	$('.limit').each(function(){
		var filter = [0,8,9,16,17,18,37,38,39,40,46];
		var limit=parseInt($(this).attr('class').split(' ')[0]);
		$(this).keydown(function(e){
			return ($.inArray(e.keyCode,filter)!=-1)||($(this).val().length<limit);
		});
		$('#'+$(this).attr('id')+'_aux').html('Characters left:'+(limit-$(this).val().length));
		$(this).keyup(function(){
			if($(this).val().length>limit){
				$(this).val($(this).val().substr(0,limit));
			}
			$('#'+$(this).attr('id')+'_aux').html('Characters left:'+(limit-$(this).val().length));
		});



	});

  

	$('#registration').each(function(){
		$('#cvvLocatorTip').click(function(){
			$('#cvvLocator').fadeIn('slow');
		})
		$('.btn',this).click(function(){$('#paymentForm').submit();return false;});
	})


	$('#paymentForm').each(function(){
		$('#cvvLocatorTip').click(function(){
			$('#cvvLocator').fadeIn('slow');
		})
		$('.btn',this).click(function(){$('#paymentForm').submit();return false;});
	})


function showLoginWindow(){
	$('#loginPopup').vCenter().fadeIn('slow');
}

function closeLoginPopup() {
	$('#loginPopup').css('display','none');
}

function objDump (obj, except) {
       return _objDump (obj, [obj], 0);
}
function inArray (arr, elt){
       for (var i = 0; i < arr.length; i++) if (elt == arr[i]) return i;
       return false;
}
function _objDump (obj, used, level) {
       var rez = '';
       var spaces = " ";
       for (var i = 0; i < level; i++) spaces += " ";
       rez += spaces+('* Dumping object: ' + obj.toString () + "\n");
       for (var i in obj) {
               if (used && inArray(used ,obj[i]) !== false) {
                       rez += spaces+"->"+"!Self Reference"+"\n";
                       continue;
               }
               rez += (spaces+"  ->" + i + ': ');
               if ( (typeof obj[i]) == "object"){
                       try{
                               rez += _objDump (obj[i], used.slice(0).push(obj[i]), 1 +level) +"\n";
                       }catch (eee) {rez += spaces+"->"+"!Error"+"\n";}
               }else rez +=(spaces+"->"+obj[i] + "\n");
       }
       return rez;
}


function gotoPage(){
	if (window.location.href.lastIndexOf('/page') > 0) 
		window.location = window.location.href.substring(0,window.location.href.lastIndexOf('/page')) + '/page' + document.getElementById('destinationPage').value;
	else
		window.location = window.location.href + '/page' + document.getElementById('destinationPage').value;
}
