$.fn.serializeForm = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$.fn.center = function()
{
    $(this).css("top", ( $(window).height() - $(this).height() ) / 2+$(window).scrollTop() + "px");
    $(this).css("left", ( $(window).width() - $(this).width() ) / 2+$(window).scrollLeft() + "px");
}
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;
var scrolled = false;
var lastScroll;
    var curPage = 0;
$(document).ready(function() 
{          
    lastScroll = $(document).scrollTop();
    
    $(window).scroll(function()
    {
         doScroll();
        
    });
    
    setFramesPosition();
    
    $('.send').click(function()
    {      
        $('.thanks').hide();
        var params = $('#contactForm').serializeForm();
        var err = false;
        
        if(params.what.length == 0)
        {
            err = "Please enter what you are looking for.";
        }
        else if(params.name.length == 0)
        {
            err = "Please enter your name.";    
        }
        else if(params.price.length == 0)
        {
            err = "Please enter the price range.";    
        }
        else if(!filter.test(params.mail))
        {
            err = "Please enter your e-mail address.";    
        }               
        
        if(err)
        {
            alert(err);
        }
        else
        {              
            $.post('mail.php',params,function()
            { 
                $('.thanks').fadeIn("slow");
            });
        }
   });
   
    $('.price_first').click(function()
    {
        GoPrice();
    });
    
    $('.pricebtn').click(function()
    {
        GoPrice();
    });
    
    $('.galerrybtn').click(function()
    {
        GoGallery();
    });
    
    $('.branding').click(function()
    {
        $('.galpage').hide();
        $('.gallogo').hide();
        $('#branding').show();
        $('#branding_logo').show();
    });
    
    $('.webgui').click(function()
    {
        $('.galpage').hide();
        $('.gallogo').hide();
        $('#webgui').show();
        $('#webgui_logo').show();
    });
    
    $('.logos').click(function()
    {
        $('.galpage').hide();
        $('.gallogo').hide();
        $('#logos').show();
        $('#logos_logo').show();
    });
    
    $('.gall_btn').click(function()
    {
        GoGallery();
    })
    
    $('.home_btn').click(function()
    {
        GoHome();
    })
    
    
    function GoGallery()
    {   
        var docHeight = $(window).height();
        
        if(lastScroll == docHeight || curPage == 1 || scrolled)
            return;
            
        
        scrolled = true;
        $.scrollTo({ top: docHeight + 'px', left: '+=0px' }, 800);
        lastScroll = docHeight;
        curPage = 1;
        
        setTimeout("scrolled = false;",900);
    }
    
    function GoHome()
    {                        
        if(lastScroll == 0 || curPage == 0 || scrolled)
            return;
                          
        scrolled = true;
        $.scrollTo({ top: '0px', left: '+=0px' }, 800);
        lastScroll = 0;
        curPage = 0;         
        setTimeout("scrolled = false;",900);
    }
    
    function GoPrice()
    {
        var docHeight = $(window).height() * 2;
        
        if(lastScroll == (docHeight * 2) || curPage == 2  || scrolled)
            return;
        
        scrolled = true;
        $.scrollTo({ top: docHeight + 'px', left: '+=0px' }, 800);
        lastScroll = docHeight * 2;
        curPage = 2;
        setTimeout("scrolled = false;",900);
    }
    
    
    $('.imageFrame').click(function()
    {
        closeImage(); 
    });
    
    $('.disable').click(function()
    {
        closeImage(); 
    });
    
    $('img.branding_btn').click(function()
    {
        var src = $(this).attr('src').replace("thumbs","brands");
        openImage(src);
    });
    
    $('img.logos_btn').click(function()
    {
        var src = $(this).attr('src').replace("thumbs","logos");
        openImage(src);
    });
    
    
            
});

function doScroll()
{   
        if(scrolled)
        {
           return;                                                                  
        }   
        
        var newScroll = $(document).scrollTop();      
        var docHeight = $(window).height();
        scrolled = true;
        closeImage();
        if(newScroll > lastScroll)
        {          
            
            if(newScroll < docHeight)
            {
                lastScroll = docHeight;
                $.scrollTo({ top: docHeight + 'px', left: '+=0px' }, 500);
            }
            else
            {
                lastScroll = docHeight * 2;
                $.scrollTo({ top: docHeight*2 + 'px', left: '+=0px' }, 500);
                curPage = 2;
            }
        }
        else
        {     
            if(curPage <= 1)
            {
                lastScroll = 0;
                $.scrollTo({ top: '0px', left: '+=0px' }, 500);
                curPage = 0;
            }
            else if(curPage == 2)
            {
                lastScroll = docHeight;
                $.scrollTo({ top: docHeight + 'px', left: '+=0px' }, 500);
                curPage = 1;
            }                                                  
        }
                                    
        setTimeout("scrolled = false;",700);
    }
    
function openImage(src)
{
    $('.disable').fadeIn();
    $('.imageFrame img').attr('src',"");
    $('.imageFrame img').attr('src',src);
    
    $('.imageFrame img').ready(function()
    {
        $('.imageFrame').show(400,function()
        {                       
            $('.imageFrame').center();      
        });
    });
    
    
    
}

function closeImage()
{
    $('.disable').fadeOut('slow');   
    $('.imageFrame').hide('slow');
    $('.imageFrame').unbind('ready');
    
}

function setFramesPosition()
{
    var docWidth = $(window).width();
    var docHeight = $(window).height();

    var pageWidth = 1024;
                            
    if(pageWidth > docWidth)
    {
        pageWidth = docWidth / 10 * 8 ;
    }
    
    $('.wrapper').width(pageWidth);
    $('.wrapper').height(docHeight);
    $('.wrapper').css('left',(docWidth - pageWidth) / 2 + 'px');
    
    $('.side').css('height',(docHeight / 100 * 80) + 'px');
    $('.side').css('width',((pageWidth/ 100 )* 63) + 'px');
    $('.side').css('right','0px');
    $('.side').css('top',(docHeight / 5) + 'px');
    
    var w = ((pageWidth/ 100 )* 60);
    var h = (docHeight / 100 * 80);
    
    if(w*1.3 > h)                                        
        w = h/1.3;             
    else          
        h = w*1.3;
    
    $('.side img').css('width',w + 'px');    
    $('.side img').css('height',h + 'px');
    $('.side img').css('top','0px');  
    $('.side img').css('right','0px');
    
    var sideWidth = $('.side img').width();
    var sideHeight = $('.side img').height();
    
    $('.facebook').css('width',((sideWidth / 100) * 12) +  'px');
    $('.facebook').css('height',((sideHeight / 100) * 10) +  'px');
    $('.facebook').css('right',((sideWidth / 100) * 23.8) +  'px');
    $('.facebook').css('top','0px');
    
    $('.branding').css('width',((sideWidth / 100) * 27.5) +  'px');
    $('.branding').css('height',((sideHeight / 100) * 10.5) +  'px');
    $('.branding').css('right',((sideWidth / 100) * 37) +  'px');
    $('.branding').css('top',((sideHeight / 100) * 4.7) +  'px');
    
    $('.logos').css('width',((sideWidth / 100) * 25) +  'px');
    $('.logos').css('height',((sideHeight / 100) * 8) +  'px');
    $('.logos').css('right',((sideWidth / 100) * 44.5) +  'px');
    $('.logos').css('top',((sideHeight / 100) * 19) +  'px');
    
    $('.webgui').css('width',((sideWidth / 100) * 29.5) +  'px');
    $('.webgui').css('height',((sideHeight / 100) * 9) +  'px');
    $('.webgui').css('right',((sideWidth / 100) * 39.1) +  'px');
    $('.webgui').css('top',((sideHeight / 100) * 29.2) +  'px');
    
    $('.pricebtn').css('width',((sideWidth / 100) * 30) +  'px');
    $('.pricebtn').css('height',((sideHeight / 100) * 9.2) +  'px');
    $('.pricebtn').css('right',((sideWidth / 100) * 3.3) +  'px');
    $('.pricebtn').css('top',((sideHeight / 100) * 13) +  'px');
    
    $('.logo').css('height',(docHeight / 18) + 'px');
    $('.logo').css('top',(docHeight / 8) + 'px');
    $('.logo').css('left',((docWidth - pageWidth) / 2)  + 'px');
    
    //$('.logo').css('height',(docHeight / 18) + 'px');
    //$('.logo').css('top',(docHeight / 8) + 'px');
    //$('.gallogo').css('left',$('.logo').width()  + 'px');
    
    $('.weLove').css('width',(pageWidth / 100 * 31) + 'px');
    
    $('.content_inner').css('left','0px');
    $('.content_inner').css('top', (parseInt($('.logo').height() * 2) + parseInt($('.logo').css('top'))) +  'px');
    
    $('.getPrice').css('width',(pageWidth / 100 * 35.6) + 'px');
    $('.getPrice').css('height',(docHeight / 100 * 7.1) + 'px');
    $('.getPrice').css('left','0px');
    $('.getPrice').css('top', (parseInt($('.logo').height() * 1.2) + parseInt($('.logo').css('top'))) +  'px');
    
    $('.contact_box').css('width',(pageWidth / 100 * 35) + 'px');
    $('.contact_box').css('height',(pageWidth / 100 * 42) + 'px');
    $('.contact_box img').css('width',(pageWidth / 100 * 35) + 'px');
    $('.contact_box img').css('height',(pageWidth / 100 * 42) + 'px');
    $('.contact_box img').css('top','0px');
    $('.contact_box img').css('left','0px');
    $('.contact_box').css('left',(pageWidth / 100 * 8.7) + 'px');
    $('.contact_box').css('top', (parseInt($('.getPrice').css('top')) + parseInt($('.getPrice').height())) +  'px');
    
    
    var txtSize = ($('.contact_box').height() / 100 * 3);
    
    $('.texts').css('width',($('.contact_box').width() / 100 * 99) + 'px');
    $('.texts').css('height',($('.contact_box').height() / 100 * 6) + 'px');
    $('.texts').css('left',($('.contact_box').width() / 100) + 'px');
    $('.texts').css('font-size',txtSize + 'px');
    
    $('#txtprice').css('width',($('.contact_box').width() / 100 * 85.47486033519553) + 'px');
    $('#txtprice').css('left',($('.contact_box').width() / 100 * 14.52513966480447) + 'px');
    $('#txtprice').css('top',($('.contact_box').height() / 100 * 33.48837209302326) + 'px');
    
    $('#txtname').css('top',($('.contact_box').height() / 100 * 52.55813953488372) + 'px');
    
    $('#txtmail').css('top',($('.contact_box').height() / 100 * 71.86046511627907) + 'px');
    
    
    $('.whatarea').css('width',($('.contact_box').width() / 100 * 99) + 'px');
    $('.whatarea').css('max-width',($('.contact_box').width() / 100 * 99) + 'px');
    $('.whatarea').css('height',($('.contact_box').height() / 100 * 14.418) + 'px');
    $('.whatarea').css('max-height',($('.contact_box').height() / 100 * 14.418) + 'px');
    $('.whatarea').css('top',($('.contact_box').height() / 100 * 6) + 'px');
    $('.whatarea').css('left',($('.contact_box').width() / 100) + 'px');
    $('.whatarea').css('font-size',txtSize + 'px');
    
    $('.send').css('width',($('.contact_box').width() / 100 * 31.56424581005587) + 'px');
    $('.send').css('height',($('.contact_box').height() / 100 * 11.62790697674419) + 'px');
    $('.send').css('top',($('.contact_box').height() / 100 * 86.51162790697674) + 'px');
    
    w = ((pageWidth/ 100 )* 45);
    h = (docHeight / 100 * 42);
    
    if(h*1.12 > w)
        h = w/1.12;             
    else          
        w = h*1.12;
    
    $('.gallbox').css('width',w + 'px');
    $('.gallbox').css('height',h + 'px');
    
    
    $('.price_first').css('left','0px');
    $('.price_first').css('width',(pageWidth / 100 * 10) + 'px');
    $('.price_first').css('height',(pageWidth / 100 * 10) / 100 * 30 + 'px');
    $('.price_first').css('top',(docHeight - 10 - parseInt($('.price_first').height()))+ 'px');
    
    
    $('.upper_arrow').css('top','5px');
    $('.upper_arrow').css('width',(pageWidth / 100 * 10) + 'px');
    $('.upper_arrow').css('height',(pageWidth / 100 * 10) / 100 * 30 + 'px');
    $('.upper_arrow').css('left',(pageWidth - parseInt($('.upper_arrow').width()))+ 'px');
    
    doScroll();
}

$(window).resize(function(){
        setFramesPosition();
    });
