/*
    Document   : index.js
    Created on : 24.01.2011
    Author     : noz
*/

//Global Speed
var gspSlow    = 2500;
var gspMedium  = gspSlow/2;
var gspFast    = gspMedium/2;
var gspBlink   = gspFast/2;

//Global Sizes
var gWidth = 0;
var gHeight = 0;
var gLeft = 0;

//Page
var pgWidth = 960;
var pgHeight = 935;
var pgMargin = 5;
//Header
var hdHeight = 120;
//Logo
var lgWidth = 320;
//Carousel
var csHeight = 240;
//Middle
var mdHeight = 40;
//Catalog Search Brands
var clWidth = 240;
var srWidth = 480;
var brWidth = 240;
//Content
var ctHeight = 495;
//Footer
var ftHeight = 20;

//Views
var notCatalogView = true;
var goIndex = false;
var firstTime = true;
var fromCarousel = true;
//Parser URL
var parser = 'index.php?give=';
var last = '';
var currentTime = new Date().getTime();
var CID = 0;

function getDisplay(){
    gWidth = $(document).width();
    gHeight = $(document).height();

    return false;
}

function loadMe(name){
    var load = name;
    return $('.'+name).load(parser+load,function(){
        $(this).fadeIn(gspMedium);
    });
}

function carouselIt(){
    $('#carousel li').first().stop().animate({marginLeft: '-165px'},gspMedium,function(){
        var til = $('#carousel li').first();
        $('#carousel li').first().remove();
        til.css('marginLeft','0px').appendTo($('#carousel')).bind({
            click: function(){
                fromCarousel = true;
                rePage('product&pid='+$(this).attr('id'));
            }
        });
        setTimeout('carouselIt()', gspSlow);
    });
}

function contentLoad(sorce){
    var img = sorce.split('&')[0];
    var tmpHeight = 0;
    $('.content').fadeOut(gspFast,function(){
        $('.page').css({backgroundImage:'url("_mod_files/ce_images/'+ img +'.jpg")'});
        $('.content')
            .load(parser+sorce,function(){
                $('.content').find('.content-td').each(function(){
                    $(this).bind({
                        click: function(){
                            fromCarousel = false;
                            rePage('product&pid='+$(this).attr('id'));
                        }
                    });
                });

                $('.content').find('.content-promo').each(function(){
                    $(this).bind({
                        click: function(){
                            rePage('promo&rid='+$(this).attr('id'));
                        }
                    });
                });

                $('.content').find('.content-arct').each(function(){
                    $(this).bind({
                        click: function(){
                            rePage('artc&aid='+$(this).attr('id'));
                        }
                    });
                });

                $('.content').find('.imgs').each(function(){
                    $(this).fancybox({
                        padding: 5,
                        margin: 5
                    });
                });
				
				$('.content').find('.popup').each(function(){
                    $(this).fancybox({
                        padding: 5,
                        margin: 5
                    });
                });

                $('.content').find('.back-link').each(function(){
                    $(this).bind({
                        click: function(){
                             rePage('products&cid='+$(this).attr('id'));
                        }
                    });
                });

                $('.content').fadeIn(gspFast);
        });
    });

    return false;
}

function rePage(source){
    if(source == 'index' || source == 'promo' || source == 'artc' || source == 'regions' || source == 'contacts' || fromCarousel){
        $('.header').animate({height: 105},gspFast);
        $('#carousel').animate({marginTop: 5},gspFast);
        $('.carousel').animate({height: 192},gspFast);
    }else{
        $('.header').animate({height: 100},gspFast);
        $('#carousel').animate({marginTop: -200},gspFast);
        $('.carousel').animate({height: 1},gspFast);
    }

    contentLoad(source);
	
	last = source;
	
    return false;
}

$(function() {
    getDisplay();
    loadMe('menu').animate({top:0}, gspMedium, function(){
        $(this).find('li').children('.overlay').each(function(){
            $(this).bind({
                click:function(){
                    rePage($(this).parent().attr('id'));
                },
                mouseover: function(){
                    var text = $(this).parent().find('.text');
                    text.stop().animate({
                        backgroundPosition: '0px',
                        left: '0px',
                        width: '125px'
                    }, 500);
                },
                mouseout: function(){
                    var text = $(this).parent().find('.text');
                    text.stop().animate({
                        backgroundPosition: '125px',
                        left: '-125px',
                        width: '150px'
                    }, 500);
                }
            }).attr('href',$(this).attr('ref'));
        });
        $('.kwicks').kwicks({
            max : 300,
            spacing : 5
        });
    });

    loadMe('logo').animate({top:0}, gspMedium, function(){
        $(this).bind('click',function(){
            rePage('index');
        });
    });

    loadMe('carousel').animate({top:0}, gspMedium, function(){
        $('.carousel li').each(function(){
            $(this).bind({
                click: function(){
                    fromCarousel = true;
                    rePage('product&pid='+$(this).attr('id'));
                }
            });
        });
        setTimeout('carouselIt()', gspSlow);
        $('.page').fadeIn(gspFast);
        $('.right').fadeIn(gspFast);
    });

    loadMe('catalog').animate({top:0}, gspMedium, function(){
        $('#catalog li p').bind({
            click: function(){
                rePage('products&cid='+$(this).attr('id'));
            }
        });
        $('#catalog li ul li p').bind({
            click: function(){
                rePage('products&cid='+$(this).attr('id'));
            }
        });
    });

	loadMe('currency').animate({top:0}, gspMedium, function(){		
		$('.flag').each(function(){
			$(this).bind({
				click: function(){
					$.cookie('cur', $(this).attr('id'), { expires: 1 });
					
					last == '' ? true : contentLoad(last);					
				}
			});			
        });		
	    $('.left').fadeIn(gspFast);
    });
	
    loadMe('brands').animate({top:0}, gspMedium, function(){
    });

    loadMe('content').animate({top:0}, gspMedium, function(){
        contentLoad('index');
    });

    loadMe('footer').animate({bottom:0}, gspMedium, function(){

    });
});

function goTop(){
    $( 'html, body' ).animate( {scrollTop: 0}, 'slow' );
}
