// JavaScript Document

$(document).ready(function(){

	        $("a[rel^='gallery-images']").prettyPhoto({
				animationSpeed: 'normal', /* fast/slow/normal */
				padding: 40, /* padding for each side of the picture */
				opacity: 0.35, /* Value betwee 0 and 1 */
				showTitle: true, /* true/false */
				allowresize: true, /* true/false */
				counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
				theme: 'dark_square', /* light_rounded / dark_rounded / light_square / dark_square */
				callback: function(){}
            });
    
    //doing our auto-init stuff
	base_url=$('base').attr('href');
	//homepage featured images


    $('#tradeitems a img').tooltip({ 
        track: true, 
        delay: 0, 
        showURL: false, 
        showBody: " - ", 
        fade: 250 
    });


	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);
	});


    $('.fade').innerfade({ speed: 1000, timeout: 6500, type: 'sequence', containerheight: '468px'}); 

	$('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;
}
