$(document).ready(function(){
    var w_body = $(window).width();
    style = 'medium';
    
    if (w_body > 1200) {
        $('#wrapper').addClass('wide').width(w_body);
        style = 'wide';
    }

    if (w_body < 900) {
        $('#wrapper').width(990);
        style = 'small';
    }
    
    var max = $('#slides > div').size();
    if (max>=2) {
	$('#slides > div').each(function() {

            var h = $(this).height();

            
            var h_c = $(this).find('.slide_content').height();
            

            
            new_pt = (h - h_c) / 2;
            
            $(this).find('.slide_content').css("paddingTop",new_pt+'px');
            $(this).hide();
	});
	    
	max = max-1;
	var count = -1;
	
	slide();
	slideIn = setInterval( slide ,4000);
    }
    function slide()  {
	$('#slides > div:eq('+count+')').fadeOut('slow');
	$('#slides_nav div.focus').removeClass('focus');
	if (count==max) count = -1;
	    count++;
	$('#slides > div:eq('+count+')').fadeIn('slow');
	$('#slides_nav div:eq('+count+')').addClass('focus');
    }
    
    $('#slides_nav div').click(function() {
	var v = $(this).attr('id');
	var count = v -1;
	clearInterval(slideIn);
	slide();
	slideIn = setInterval( slide ,4000);
    });
    
    $('#thumbnails a').each(function() {
        $(this).click(function() {
            path = $(this).attr('href');
            alt = $(this).attr('title');
            loadim (path, alt);
            return false;
        });
    });
    
    slide_count= 0;
    sliding = false;
    
    $('#solutions_nav .nav_next').click(function() {
        
        if ( sliding==false && slide_count < 2) {
            slide_count++;
            sliding = true;
            
            if (style == 'wide') {
                $('#solutions').animate({
                    marginLeft: '+=-400',
                    marginTop: '+=30'
                }, 1000, function() {
                    sliding = false;
                });
            }else {
                $('#solutions').animate({
                    marginLeft: '+=-550',
                    marginTop: '+=30'
                }, 1000, function() {
                     sliding = false;
                });
            }
        }
        return false;
    });
    
    $('#solutions_nav .nav_prev').click(function() {
        
        if ( sliding==false && slide_count > 0) {
            slide_count--;
            sliding = true;
            
            if (style == 'wide') {
                $('#solutions').animate({
                    marginLeft: '+=+400',
                    marginTop: '+=-30'
                }, 1000, function() {
                    sliding = false;
                });
            }else {
                $('#solutions').animate({
                    marginLeft: '+=+550',
                    marginTop: '+=-30'
                }, 1000, function() {
                     sliding = false;
                });
            }
        }
        return false;
    });
    
    $('.phase').each(function() {
        $(this).click(function() {
            var target = $(this).attr('id');
            // $('#phase_contents .phase_content').hide().filter('#'+ target + '_content').show();;
        })
    });
    /*
    $('#phase_contents .phase_content').each(function() {
        $(this).hide();
    }).filter(':first').show();
    */
    var animating = false;
    
    $('#clients > .client').each(function () {
        $(this).hover(
            function () {
                
                if ( $(this).attr('rel') == '') {
                    var mt = $(this).css("margin-top");
                    var ml = $(this).css("margin-left");
                    
                    $(this).attr('rel',mt);
                    $(this).attr('ref',ml);
                } else {
                    mt = $(this).attr('rel');
                    ml = $(this).attr('ref');
                }
                
                $(this).stop().animate({
                    width: 212+'px',
                    height: 168+'px',
                    marginTop: (Number(mt.replace('px','')) - 42 ) +'px',
                    marginLeft: (Number(ml.replace('px','')) - 42 ) +'px'
                }, 300);
                
                $(this).find('img').stop().animate({
                    width: 212+'px',
                    height: 168+'px'
                }, 300, function() {
                    sliding = false;
                });
            }, 
            function () {
        
                mt = $(this).attr('rel');
                ml = $(this).attr('ref');
                
                $(this).stop().animate({
                    width: 128+'px',
                    height: 102+'px',
                     marginTop: mt,
                    marginLeft: ml
                }, 300);
                
                $(this).find('img').stop().animate({
                    width: 128+'px',
                    height: 102+'px'
                }, 300, function() {
                    sliding = false;
                });
            }
        );

    });
    
    $('a.infographic').click(function() {
	urls = $(this).attr('href');
	var url_arr =  urls.split("||");
	
	titles = $(this).attr('href');
	var title_arr =  titles.split("||");

	$('#boxy').fadeIn();
	boxy_loadim (url_arr[0],'');
	
	$('#boxy_detail_title').html( title_arr[0] );
	$('#boxy_detail_date').html( $(this).attr('rev') );
	$('#boxy_cat').html( $(this).attr('rel') );
	$('#boxy_download').attr( 'href',url_arr[0] );
	
	return false;
    });
    $('#boxy_close').click(function() {
	$('#boxy').hide();
    });
    
    $("#submit_img").click(function() {
	$('#submit').click();
    })
    
    $("#resume_btn").click(function() {
	$('#resume').click();
    })
   
    
    
    $('#newsletter_email').bind('click', function(){
	$(this).val('');
    });
	
    $('#newsletter_email').bind('blur', function(){
	if($(this).val() == '') {
	    $(this).val('EMAIL');
	}
    });
    $('#s').bind('click', function(){
	$(this).val('');
    });
	
    $('#s').bind('blur', function(){
	if($(this).val() == '') {
	    $(this).val('SEARCH');
	}
    });
    $('#s').bind('click', function(){
	$(this).val('');
    });
	
    $('#s').bind('blur', function(){
	if($(this).val() == '') {
	    $(this).val('SEARCH');
	}
    });
    
    /* add input to newsletter */
    if (ne!='') {
	$('input[name=ne]').val(ne);
    } else {
	$('td input[name=ne]').val('EMAIL');
    }
    
    $('td input[name=ne]').bind('click', function(){
	    if($(this).val() == 'EMAIL') {
		$(this).val('');
	    }
    });
	    
    $('td input[name=ne]').bind('blur', function(){
	    if($(this).val() == '') {
		$(this).val('EMAIL');
	    }
    });
    $('#province').change(function () {
	$('#city optgroup').hide();
	$('#city optgroup option').hide();
	
	id=$('#province option:selected').attr('title');
	
	options = $(id).html();
	$("#city").html(options);
	
	//$("#city optgroup[title="+title+"]").show();
	//$("#city optgroup[title="+title+"] option").show();
	//$("#city optgroup[title="+title+"] option:first").attr('selected','selected');
	
    });
   
    id=$('#province option:selected').attr('title');
    
    options = $(id).html();
    $("#city").html(options);
	
    $('.load_whitepaper').each(function () {
	
	$(this).click(function () {
	    url = $(this).attr('href');
	    loadPage(url,'#slideshare_preview');
	    return false;
	});
	
    });
    
});
function loadPage(url,id) {
    $(id).html('');
    $.ajax({
	url: url,
	type: "GET",   
	dataType: "html",
        success: function(html) {
	    $(id).html(html);
	}
    });
}
function boxy_loadim(path, alt) {
    var img = new Image();
    $(img).load(function () {
	$(this).hide();
	$('#boxy_image img').remove();
	$('#boxy_image').append(this);
	$(this).fadeIn('slow');
    
    }).error(function () {
    }).attr({
	src: path,
	alt: alt
    });
}

function resume_change (v) {
    $('#resume_txt').val(v);
}
function loadim (path, alt) {
    $('#preview #image img').animate({
        opacity: 50
    }, 500 );
    var img = new Image();
    $(img).load(function () {
        $(this).hide();
        $('#preview #image img').remove();
        $('#preview #image').append(this);
        
        //$('#image_container').css('height', $(this).height() );
        
        $(this).fadeIn('slow');
        $('#preview h3').html(alt);
        /*newpath = "?scr="+path.replace("http://", "");
        new_loc = "#image_container";
        window.location.replace(new_loc);
        return false;*/

    }).error(function () {
        }).attr({
        src: path,
        alt: alt
    });
}
