
function DESemail(emailname, emailserver) {
    document.write('<a href="mailto:' + emailname + '@' + emailserver +'">' + emailname + '@' + emailserver + '</a>');
}

	//	Global Variables lol
		
		//	Multi Images
			hex            = false;
			old_hex        = false;
			selected_index = false;

			
	
	//	Get on it with dem functions, son..
	var Des = Des || {};

    //  Currency Convertor
    $.prototype.currencyConvert = function (settings) {

        this.each(function () {

            var convertForm = $(this);
            var convertSelect = convertForm.find('select');
            var convertHiddenAmount = convertForm.find('input#convert_amount');
            var showPrice = convertForm.find('p');

            convertSelect.change(function() {
                var convertCurrency = convertSelect.val();
                var convertAmount = convertHiddenAmount.val();

                $.ajax({
                    url: '/incs/ajax/currency_convert.php?amount=' + convertAmount + '&currency=' + convertCurrency,
                    type: 'POST',
                    success: function(data) {

                        showPrice.html(data);

                        if (data != '') {
                            showPrice.addClass('converted_price');
                        }
                        else {
                            showPrice.removeClass('converted_price');
                        }
                    }
                });
            });
            
            if (convertSelect.val() != '') {
                convertSelect.trigger('change');
            }
            
        });

        return this;

    };

	/* Multiple images on Full Product (Cycle) */
	Des.multi_images = function() {
		
		if ($('.multi_single').get(0)) {
			post_id = $('.multi_single').attr('id').split('_')[1];

			$.ajax({
				data: 'ajax=true&id=' + post_id,
				dataType: 'html',
				method: 'get',
				success: function(response) {
					$('.multi_single').remove();
					$(response)
						.insertBefore('.multi_thumbs')
						.cycle({
							speed: 250,
							timeout: 0,
							pager: '.multi_thumbs',
							pagerAnchorBuilder: function(idx) {
								return '.multi_thumbs li:eq(' + idx + ')';
							}
						});
				},
				url: '/incs/actions/multi_images.php'
			});

			$('.add_to_basket input:radio').click(function() {
				hex = $('.add_to_basket input:radio:checked')
					.siblings('span')
					.attr('class')
					.split('_')[1];


                //alert(hex);

				if (hex != old_hex) {
					thumbs = $('.multi_thumbs li.thumb_' + hex);

					selected_index = $('.multi_thumbs li:visible a').index($('.activeSlide')[0]);

                    //alert(selected_index);
                    
					$('.multi_thumbs li:visible').animate({ opacity : 0 }, 250, 'linear', function() {
						$(this).css({ display : 'none' });

						thumbs
							.css({ opacity : 0, display : 'block' })
							.animate({ opacity : 1 }, 250, 'linear', function() {
								$('.multi_thumbs li:visible a:eq(' + selected_index + ')').click();
							});
					});

					old_hex = hex;
				}
			});

			initial_selector = $('.add_to_basket input:radio:checked').get(0) ? '.add_to_basket input:radio:checked' : '.add_to_basket input:radio:first';

			hex = $(initial_selector)
				.siblings('span')
				.attr('class')
				.split('_')[1];

			$('.multi_thumbs li.thumb_' + hex).css({ opacity : 1, display : 'block' });

			selected_index = $('.multi_thumbs li a').index($('.activeSlide')[0]);

            
			$('.multi_master').cycle(selected_index);
		}
	}	//	multi_images()



	Des.tweaks = function() {

		$('body').addClass('js');

		$('.button').hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);

		$('.multi_thumbs li').hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);
		/*
		$('.products_list li').hover(
			function() { $(this).addClass('hover'); },
			function() { $(this).removeClass('hover'); }
		);
		*/

	}	//	tweaks()
	
	
	/* Adds a product to home trial, well, calls an iframe via colorbox */
	Des.home_trial_add = function() {
		
		$("a.add_trial").click(function() {
			var productID = $(this).attr('rel');

			$.fn.colorbox({
				href:"/incs/iframes/color_selector.php?id="+productID,
				innerWidth: 430,
				innerHeight: 300,
				opacity: .7,
				open: true,
				iframe: true
			});
			return false;
		});

	}
	
	
	Des.frame_list_actions = function() {
		
		/* Changes the picture when color selected in frame list */
		$("ol.color_opts a").click( function() {
			
			// color id
			ids = $(this).attr('rel').split('_');
			frameID = ids[0];
			colorID = ids[1];
			
			imgSrc = '/img/medium/frame_'+frameID+'_'+colorID+'_001.jpg';
			currentImg = $(this).parent().parent().parent().find('li.photo img');
			
			// hide image
			currentImg.hide();
			
			// active indicator
			$(this).parent().siblings().removeClass('active');
			
			//$(this).parent().parent().find
			$(this).parent().addClass('active');
			
			// show loading
			
			// preload the new image
			$('<img />')
			    .attr('src', imgSrc)
			    .load(function(){
			        
					// replace src
					currentImg.attr('src',imgSrc);
					// and show the damn image already
					currentImg.fadeIn(250);
			    });

			//alert(imgSrc);
			return false;
		});
		
		/* Changes border color of product list item, when hover over picture */
		$(".products_list li.photo a").hover(
			function() {
				$(this).parent().parent().parent().addClass('hovered');
			},
			function() {
				$(this).parent().parent().parent().removeClass('hovered');
			}
		);
	}
	
	
	// Scroll to Error Messages
	Des.error_focus = function() {
		if (document.getElementById("form_errors")) {
			var $target = $("#form_errors");
			var targetOffset = ($target.offset().top)-20;
			$('html,body').animate({scrollTop: targetOffset}, 500);
		}
	}
	
	// Popup Help
	Des.popup_help = function() {
		
		$('a.help_popup').click(function() {
			
			itemID = $(this).attr('rel');

            orderID = $(this).attr('rev');
            
			var itemLink = '';

            if ($(this).hasClass('help_page')) {
				itemLink = '/incs/iframes/help_popup.php?page='+itemID;
				popWidth = 750;
				popHeight = 500;
			}
			
			else if ($(this).hasClass('comp_terms')) {
				itemLink = '/incs/iframes/help_popup.php?competition='+itemID;
				popWidth = 550;
				popHeight = 400;
			}

			else {

                var path = '/';

                if ($(this).hasClass('remote')) {

                    path = 'http://glassescomplete.co.uk/';

                }

                if (orderID != '') {
                    itemLink = path+'incs/iframes/help_popup.php?snippet='+itemID+'&amp;'+'order='+orderID;
                    popWidth = 650;
                    popHeight = 500;
                }
                else {
                    itemLink = path+'incs/iframes/help_popup.php?snippet='+itemID;
                    popWidth = 650;
                    popHeight = 500;
                }
			}
			
			$.fn.colorbox({
				href:itemLink,
				innerWidth: popWidth,
				innerHeight: popHeight,
				opacity: .7,
				iframe: true,
				open: true
			});
			
			return false;
		});
	}
	
	
	
	
	Des.faq = function() {
		if ($('.faqs_questions').get(0)) {
			
			// check for existing hash
			var strHash = window.location.hash;
			if (strHash!='') {
				var $target = $(strHash);
				var targetOffset = ($target.offset().top)-20;
				$('html,body').animate({scrollTop: targetOffset}, 500);
				$target.animate({ backgroundColor: "#ffffa2" }, 750).animate({ backgroundColor: "#fff" }, 2000);
			}
			//alert(strHash);
			
			$("ol.faqs_questions a").click(function() {
				var answerID = $(this).attr('href');
				var $target = $(answerID);
				var targetOffset = ($target.offset().top)-20;
				$('html,body').animate({scrollTop: targetOffset}, 500);
				$target.animate({ backgroundColor: "#ffffa2" }, 750).animate({ backgroundColor: "#fff" }, 2000);
				
				window.location.hash = answerID; // set hash (so can use return false on link!)
				return false;
			});
		}
	}	//	faq()
	
	
	Des.loginForm = function() {

        if($.browser.msie && $.browser.version < "8.0" ) {
            innHeight = 280;
        }
        else {
            innHeight = 260;
        }
        $('#global_login').colorbox({
			href:"/incs/iframes/login_form.php",
			innerWidth: 330,
			innerHeight: innHeight,
			initialWidth: 320,
			initialHeight: 260,
			opacity: .7,
			iframe: true
		});
		
		$('#prescription_login').colorbox({
			href:"/incs/iframes/login_form.php",
			innerWidth: 320,
			innerHeight: innHeight,
			initialWidth: 320,
			initialHeight: 260,
			opacity: .7,
			iframe: true
		});
	}

    Des.bestFitFilters = function() {

        if (document.getElementById('filter_bestfit_form')) {

            // Only numeric values in inputs
            $('#filter_bestfit_form input').numeric();

            // focus event for finter inputs, if 0, empty
            $('#filter_bestfit_form input').focus( function() {
                var filterVal = $(this).val();
                if (filterVal=='0') {
                    $(this).val('');

                    $(this).css('color','#333');
                }
            });

            // Catch the submit of filter form, and change action to include values
            $('#filter_bestfit_form').submit(function() {
                var action = $('#filter_bestfit_form').attr('action');

                var measureArray = [];

                $('#filter_bestfit_form input.filter_measure').each( function() {

                    var measure = $(this).val();

                    // assign 0 value if empty
                    if (measure=='') {
                        measure = '0';
                    }
                    // else only allow numbers
                    //else {
                    //    measure = measure.replace(/[^\d]/);
                    //}

                    // add to array
                    measureArray.push(measure);

                });

                // JS equiv implode()
                action = action + measureArray.join('-') + '/';

                // set form action
                $('#filter_bestfit_form').attr('action', action);

                // set flag, so can redirect if no JS
                $('#filter_bestfit_hasjs').val('true');

                // submit form
                return true;
            });
        }
    }

    
	//	Hyper-Hypno Space Explosion!
	$(function() {

        // ajax process the sort frames dropdown
        if ($('#frame_pagination_sort').length) {

            // hide the go button
            $('#filter_frames_go').hide();
            
            $('#frame_sort_type').change(function() {
               var sortVal = $('#frame_sort_type').val();


               $.ajax({
                    data: 'sort=' + sortVal,
                    method: 'get',
                    success: function(response) {

                        location.reload(true);
                    },
                    url: '/incs/ajax/sort_direction.php'
                });

               
            });
            
        }

		//	FireBug, yeah? - Load it up, load it up real good!
		if ($.isFunction(window.loadFirebugConsole)) window.loadFirebugConsole();

        if ($('div.reglaze_ad').length) {
            $('div.reglaze_ad').hover( function(){
                $(this).addClass('reglaze_hovered');
            },
            function() {
                $(this).removeClass('reglaze_hovered');
            });
            $('div.reglaze_ad').click( function(){
                var lnk = $(this).find('a').attr('href');
                window.location.href = lnk;
            });
        }


        if ($('#brands_carousel').length) {
            $('#brands_carousel').jcarousel({
                auto: 3,
                scroll: 3,
                visible: 5,
                wrap: 'circular'
            });
        }

		if (document.getElementById('reglaze_products_list')) {
			$('#reglaze_products_list li').hover(
                function () {
                    $(this).addClass('hovered');
                },
                function () {
                    $(this).removeClass('hovered');
                }
            );

            $('#reglaze_products_list li').click(
                function () {
                    var lnk = $(this).find('a.reglaze_start_btn').attr('href');
                    window.location.href = lnk;
                }
            );
		}
		
		// home slideshow
		if (document.getElementById('home_slide')) {
			$('#home_slide ul').cycle({ 
			    fx:      'scrollDown', 
			    speed:    750, 
			    timeout:  7000 
			});
		}
		
		// show stuff hidden by CSS
		$("span.add_to_home_trial").show();
		$("p.frame_list_buttons em").css('display','block');
		$("#add_trial_full").show();
		
        

		//	Stick your functions here, like..

        // Currency Convertor
        $('form.currency_convert').currencyConvert();

        Des.bestFitFilters();
        
		Des.faq();
		
		//Des.filters();
		Des.multi_images();
		Des.tweaks();
		
		Des.error_focus();
		Des.home_trial_add();
		
		Des.loginForm();
		
		Des.frame_list_actions();
		Des.popup_help();
		
		$('a.colorbox_pic').colorbox({
			opacity: .7
		});

        var interval;

        if($('.feature_roundabout ul').length) {
            $('.feature_roundabout ul')
                .roundabout({
                    reflect: true,
                    minOpacity: 0.6
                })
                .hover(
                    function() {
                        clearInterval(interval);
                    },
                    function() {
                        interval = startAutoPlay();
                    }
                );
            interval = startAutoPlay();
        }

	
	});

    function startAutoPlay() {
        return setInterval(function() {
            $('.feature_roundabout ul').roundabout_animateToNextChild();
        }, 8000);
    }
	

