$(document).ready(function () {
	
	handleSearchPlaceHolderText();
	
	handleLanguageMenu();
	
	switch(getUrlVarsFrom(window.location.href, true)['tabid']) {
		case '1':
			initHome();
			break;
		case 'undefined':
			initHome();
			break;
		case '9':
			hideEmptyRequisitionMessages();
			handlePopupFormat();
			break;
		case '49':
			hideEmptyRequisitionMessages();
			handlePopupFormat();
			break;
		case '50':
			/*load();*/
			handleContactPage();
			hideEmptyRequisitionMessages()
			handleInputs();
			break;
		
		
		}
	
	$(window).load(function(){
	contentHeight = $('.content').height();
	setContentHeight();
	
	$(window).resize(function() {
 		setContentHeight();
	});
	})
})


function getUrlVarsFrom(url, preventUnwantedVars) {
    var vars = [],
        hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        if (preventUnwantedVars == 1) {
            if (hash[0] != 'lg' && hash[0] != 'objectid' && hash[0] != 'tabindex' && hash[0] != 'page' && hash[0] != 'keyword' && hash[0] != 'mediaid') {
                if (hash[0] == 'categoryid' && hash[1] == 0) {} else {
                    vars.push(hash[0]);
                    vars[hash[0]] = hash[1];
                }
            }
        } else {
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
    }
    return vars;
}


function initHome() {
	if($('a#en').hasClass('active')) {
		
		$('div.description h2').html('Welcome');
		
		$('div.description p').html('The Fouquet Gallery 157, Faubourg Saint Honor&#233; in Paris is quite unique; the warm welcom of the amateurs,the collectors,the decorators to share the passion of esthetic and rarity. Every piece of art exhibited in the gallery is selected in accordance with strict criteria of design, quality and authenticity. It is unique opportunity to enjoy creating and outstanding collection in the best possible conditions while securing an undisputable investment with a return guarantee. Life is worth living with elegance and refinement , a kind of quintessence of french taste prevailling during the 18th century, a way of life never surpassed since then.');
	
	}
	
}




function handleSearchBox() {
    if ($('div#searchBox input').val() != '') {
        $('div#searchBox label').css('display', 'none');
    }
    $('div#searchBox input').focus(function () {
        if ($(this).val() == '') {
            $('div#searchBox label').animate({
                opacity: 0.25
            }, 250);
        }
    });

    $('div#searchBox input').blur(function () {
        if ($(this).val() == '') {
            $('div#searchBox label').css('display', 'block');
            $('div#searchBox label').animate({
                opacity: 1
            }, 250);

        }
    });
    $('div#searchBox input').keydown(function () {

        $('div#searchBox label').css('display', 'none');

    });

    $('div#searchBox input').change(function () {
        if ($(this).val() == '') {
            $('div#searchBox label').css('opacity', 1);

        }
    });

}


function handleInputs() {

   
        //handle text input
        $('div.inputBox').each(function (index, domEle) {

            var target;
            if ($(domEle).children('input').length > 0) {
                target = 'input';
            } else {
                target = 'textarea'
            }

            if ($('label', domEle).length == 0) $(domEle).append('<label />');
            $('label', domEle).attr('for', $(target, domEle).attr('id'));
            $('label', domEle).html($('.label', domEle).html().replace(':', ''));
            $('span.label', domEle).remove();




            if ($(target, domEle).val() != '') {
                $('label', domEle).css('display', 'none');
            }
            $(target, domEle).focus(function () {
                if ($(this).val() == '') {
                    $('label', domEle).animate({
                        opacity: 0.25
                    }, 250);
                }
            });

            $(target, domEle).blur(function () {
                if ($(this).val() == '') {
                    $('label', domEle).css('display', 'block');
                    $('label', domEle).animate({
                        opacity: 1
                    }, 250);

                }
            });
            $(target, domEle).keydown(function () {

                $('label', domEle).css('display', 'none');

            });

            $(target, domEle).change(function () {
                if ($(this).val() == '') {
                    $('label', domEle).css('opacity', 1);

                }
            });


        });
}
function handleSelects() {
        //handle select
        $('select.selectBox').parent().prepend('<div class="selectBoxMask" />').prepend('<label />');
        $('select.selectBox').parent().children('label').attr('for', $('select.selectBox').attr('id')).addClass('label').attr('style', '');
        $('select.selectBox').css('opacity', 0);
        $('select.selectBox').change(function () {
            var val = $(this).find('option:selected').text();
            $('select.selectBox').parent().children('label').html(val);
        });


        // anulate hidden status from comment label
        $('inputBox.comments label').attr('style', '');

}
function hideEmptyRequisitionMessages() {
	$('span.req').each(function (index, domEle) {
            if ($(domEle).css('visibility') == 'hidden') $(domEle).css('display', 'none');

        })
}
function handleContactPage() {
        // hide confirmation span if it has no content
        if ($('span.confirmation').is(":empty")) $('span.confirmation').css('display', 'none');
        


        $('.inputBox.select label').html($('.inputBox.select select option:first').html());

    
}

/* Search Functions */

function submitQuickSearch() {
    var keyWords = document.getElementById('inpQuickSearch').value;
    document.location.href = document.location.href.split('?')[0] + '?tabid=5&keyword=' + keyWords;
}

function submitCategory() {
    var url = document.getElementById('selCategory').value;
    document.location.href = document.location.href.substr(0, document.location.href.indexOf('DesktopDefault.aspx')) + url;
}

function entsub(e) {
    var evt = window.event ? e.keyCode : e.which;
    var keyPressed = evt;

    if (keyPressed == 13) {
        submitQuickSearch();
        return false;
    } else {
        return true;
    }
}



function handleLanguageMenu() {
	
    $("ul#langbox > li > a").each(function (index, domEle) {
        var currentLg = getUrlVarsFrom(window.location.href, false)['lg'],
            newLg = getUrlVarsFrom($(domEle).attr('href'),false)['lg'];
        if (currentLg != newLg) {
			
            if (currentLg === undefined) {
				if (getUrlVarsFrom(window.location.href, false).length > 1) {
                $(domEle).bind('click', function () {
                    window.location = window.location.href + '&lg=' + newLg;
                    return false;
                })
				}
            } else {
                $(domEle).bind('click', function () {
                    window.location = window.location.href.replace('lg=' + currentLg, 'lg=' + newLg);
                    return false;
                })
            }
		
        } else {
			$(domEle).bind('click', function () {
                    window.location = window.location.href;
                    return false;
                })
			
			}
		
    })
	
}


function handleSearchPlaceHolderText() {
    $('input#inpQuickSearch').focus(function () {
        if ($(this).val() == $(this).attr('defaultValue')) {
            $(this).val('');

        }
    });

    $('input#inpQuickSearch').blur(function () {
        if ($(this).val() == '') {
            $(this).val($(this).attr('defaultValue'));

        }
    });
	
	
	 
	
}


// map functions (contact page)
    function load() {
  if (GBrowserIsCompatible()) {
    var map1 = new GMap2(document.getElementById("map1"));
	var point = new GLatLng(48.874424,2.306738);
    map1.setCenter(point, 16);
	var marker = new GMarker(point);
    map1.addOverlay(marker);
	map1.addControl(new GSmallMapControl());
	
	
    
  }
}

function handleContactPage() {
        // hide confirmation span if it has no content
        if ($('span.confirmation').is(":empty")) $('span.confirmation').css('display', 'none');
        $('span.req').each(function (index, domEle) {
            if ($(domEle).css('visibility') == 'hidden') $(domEle).css('display', 'none');

        })
		$('div#leftcolumn').prepend('<img id="contactPic" src="media/fouquetmedia/img/fouquetPicture.jpg" />');
		$('div.content').css('height', '1100px');
/*		if ($('#fr').hasClass('active')) {
			$('small a').append('Agrandir le plan');
		} else $('small a').append('View Larger Map');*/
    
}




function handleInputs() {

   
        //handle text input
        $('div.inputBox').each(function (index, domEle) {

            var target;
            if ($(domEle).children('input').length > 0) {
                target = 'input';
            } else {
                target = 'textarea'
            }

            if ($('label', domEle).length == 0) $(domEle).append('<label />');
            $('label', domEle).attr('for', $(target, domEle).attr('id'));
            $('label', domEle).html($('.label', domEle).html().replace(':', ''));
            $('span.label', domEle).remove();




            if ($(target, domEle).val() != '') {
                $('label', domEle).css('display', 'none');
            }
            $(target, domEle).focus(function () {
                if ($(this).val() == '') {
                    $('label', domEle).animate({
                        opacity: 0.25
                    }, 250);
                }
            });

            $(target, domEle).blur(function () {
                if ($(this).val() == '') {
                    $('label', domEle).css('display', 'block');
                    $('label', domEle).animate({
                        opacity: 1
                    }, 250);

                }
            });
            $(target, domEle).keydown(function () {

                $('label', domEle).css('display', 'none');

            });

            $(target, domEle).change(function () {
                if ($(this).val() == '') {
                    $('label', domEle).css('opacity', 1);

                }
            });


        });
}

function handlePopupFormat() {
	$('.mainTable').css('width', '520px');
}

var contentHeight;
function setContentHeight() {
	var headerHeight;
	$('#top').length > 0 ? headerHeight = 380 : headerHeight = 0;
	if ($(window).height() - headerHeight > contentHeight)
	$('.content').css('height', $(window).height() - headerHeight + 'px');
}


