jQuery.noConflict();
jQuery(document).ready(function(){
	
	jQuery(function () {
	
		if (jQuery("#navi_container").is(':visible')) { // Just to check if it's print- or screenversion, because print needs no JS
		
			access_warning();
			back_to_consumer();
			bc_cor();
			big_theme();
			bookmark();
			centrize();
			drucker();
			clonex();
			fancybox();
			fielder();
			fix_assist();
			hide_prof();
			odevs();
			pureprint()
			replace_source();
			set_lupe();
			table_mod();
			validators();
			zero();
			
		}else{
		};
		
	});

    initDownload();
});


function access_warning() {

	var closed_blocks = jQuery("li.disabled");
	jQuery("li.disabled div.access_layer").css({'display':'none'});
	
	closed_blocks.hover(
      function () {
        jQuery(this).find("div.access_layer").fadeIn("fast");
      }, 
      function () {
        jQuery(this).find("div.access_layer").fadeOut("slow");
      }
    );

}



function back_to_consumer() {
	var back_teaser = jQuery(".teasers_line li:contains('SANYO Solar Website'), .teasers_line li:contains('SANYO solaire site web'), .teasers_line li:contains('SANYO Solar sito web')");
	back_teaser.addClass('back_to_consumer');
}




function bc_cor() {
	var bcc = jQuery("#breadcrumb_container ul li:contains('- ')");
	if ( bcc.length ) {
		bcc.html(bcc.html().replace(/- /ig, ""));
	}
}




function big_theme() {
	
	var tabs = jQuery("ul#big_theme > li > h3");
	var bigpics = jQuery("ul#big_theme > li > div.sub_theme");

	
	tabs.hover(function() {
		jQuery(this).addClass('tab_hover');
		
		for (i=0; i<tabs.length; i++) {
			tab = jQuery(tabs[i]);
			tab.parent().removeClass('current');
		}
	
		jQuery(this).parent().addClass('current');
		
	}, function() {
		jQuery(this).removeClass('tab_hover');
	});
	
	bigpics.click(function() {
		var linkit = jQuery(this).find('a').attr("href");
		window.location = linkit;
	});
	
}



function bookmark() {
	if(window.opera) {
        if (jQuery("a.bookmark_page").attr("rel") != "") {
            jQuery("a.bookmark_page").attr("rel","sidebar");
        }
    }
    jQuery("a.bookmark_page").click(function(event) {
        event.preventDefault();
        if (window.sidebar)
        {
            window.sidebar.addPanel(this.title, this.href, "");
        }
        else if(window.external)
        {
            window.external.AddFavorite(this.href, this.title);
        }
        else if(window.opera)
        {
            return false;
        }
        else
        {
            alert('Please bookmark this page manually.');
        }
    });
}



function centrize() {

	kinder =jQuery(".centrize");
	
	if ((jQuery.browser.name == "msie") && (jQuery.browser.version < 7)) {
		thr = 4; // IE6 is very buggy in margins and should be given alway 1/2 of the real amount
	}
	else {
		thr = 2;
	}
	
	for (i=0; i<kinder.length; i++) {
		
		kind_width = jQuery(kinder[i]).width();
		papa_width = jQuery(kinder[i]).parent().innerWidth();
		
		if ( kind_width < papa_width ) {
			var zusatz = parseInt((papa_width - kind_width)/thr);
			jQuery(kinder[i]).css({'margin-left': zusatz});
		}
	}
}



function clonex () {
	var conper = jQuery(".conper");
	var addcon = jQuery(".add_contact");
	conper.hide();
	
	addcon.click(function () { 
  		var hiconper = jQuery(".conper:hidden");
  		jQuery(hiconper[0]).slideDown('slow');
  		if (hiconper.length < 2) {
  			jQuery(".add_more_contacts").hide();
  		} else {
  			return;
  		}
	});
	
	var standort = jQuery(".location");
	var addloc = jQuery(".add_place");
	standort.hide();
	
	addloc.click(function () { 
  		var hiconper = jQuery(".location:hidden");
  		jQuery(hiconper[0]).slideDown('slow');
  		if (hiconper.length < 2) {
  			jQuery(".add_more_places").hide();
  		} else {
  			return;
  		}
	});
}



function drucker () {
	var trigger = jQuery("a.print_page");
	trigger.click(function() {
		window.print();
	});
}



function fancybox() {
	jQuery("a.fb_image").fancybox({
		'imageScale':		true,
		'overlayOpacity'	:	0.7,
		'overlayColor'		:	'#FFF',
		'overlayShow':		false
	});
}



function fielder() {

	jQuery('input.exampled').example(function() {
		return jQuery(this).attr('title');
	});

}


function fix_assist() {
	var conters = jQuery("ul.flowcards li span.wr2 span.text");
	for (i=0; i<conters.length; i++) {
		punkt = jQuery(conters[i]);
		rost = punkt.innerHeight();
		diff = (52 - rost)/2;
		punkt.css({'padding-top' : diff + "px"});
		punkt.css({'padding-bottom' : diff + "px"});
	}
}




function hide_prof() {
	var current_lang = jQuery("#lang_selector li.current a span").attr("id");
	var reci = jQuery("#solar_assist").addClass("lang_" + current_lang);
	var lowlog = jQuery("#footer li a span:contains('Login')").parent().parent().addClass("lang_" + current_lang);
	centrize();
}




function odevs() {
	jQuery("table.alternate tr:even td").addClass('even');
	jQuery("ul.alternate > li:odd").addClass('even');
	jQuery("ul.alternate > li:even").removeClass('even');
}




function pureprint() {
	jQuery('<div class="print_logo"><img src="fileadmin/templates/img/sanyo_logo.gif" alt="sanyo_logo" width="120" height="49" /></div>').insertBefore('#megawrapper');
	jQuery('<p class="afternote">SANYO Component Europe GmbH<br/>Solar Division<br/>Stahlgruberring 4<br/>81829 Munich<br/>Germany</p>').insertAfter('#footer');
}




function replace_source() {  // IE6 can't do inputs type="image" with .png
	
	if ((jQuery.browser.name == "msie") && (jQuery.browser.version < 7)) {
		buttons = jQuery("input[srcjQuery=png]");
		buttons.attr({ 
          src: "img/x.gif"
        });
	}
	else {
		return;
	}
	
}



function set_lupe() {
	jQuery('<div class="lupe"></div>').insertAfter("a.fb_image > img");
}



function table_mod() {
	var down_cell = jQuery("table.downloads a.download, table.dsr_table a.download").parent();
	var labels = jQuery("table.downloads > tbody > tr > td:not(:last-child)");
	var inilabs = jQuery("table.dsr_table tbody td.doc_descr label");
	var multisel =jQuery("input#select_all");
	var boxes = jQuery("table.downloads tbody tr td input");
	
	multisel.toggle(
      function () {
        boxes.attr("checked", "checked");
        multisel.attr("checked", "checked");
      },
      function () {
        boxes.removeAttr("checked");
        multisel.removeAttr("checked");
      }
    );
	
	down_cell.hover(function() {
		jQuery(this).addClass('download_hovered');
		jQuery(this).parent().addClass('normal');
	}, function() {
		jQuery(this).removeClass('download_hovered');
		jQuery(this).parent().removeClass('normal');
	});
	
	down_cell.click(function() {
		var linkit = jQuery(this).find('a').attr("href");
		window.location = linkit;
	});
	
	inilabs.hover(function() {
		jQuery(this).addClass('download_hovered');
	}, function() {
		jQuery(this).removeClass('download_hovered');
	});
	
}



function validators() {
	jQuery(".validate_me").validationEngine(); // Initializing validation for all forms with class .validate_me
	var switcher = jQuery(".switch_submit"); // Finding all "Save / Edit" buttons, that trigger switching on click (on successfull submit)
  // switcher.click(function () { 
  //      
  //      var fieldset = jQuery(this).parent("fieldset");
  //      fieldset.removeClass("edit_mode").addClass("view_mode");
  //      reset_fieldset();
    	
/*
    	jQuery(".validate_me").bind("submit", function(caller){ // Catching validation status on submit and taking apropriate actions...
			if(jQuery.validationEngine.submitValidation(this) == false){
				fieldset.css({'border-top':'5px solid green'});
			}else{
				fieldset.css({'border-top':'5px solid red'});
			}
		});
*/
		
    // });
}



function reset_fieldset() {
  // view_sets = jQuery("fieldset.view_mode");
  // for (i=0; i<view_sets.length; i++) {
  //  field = jQuery(view_sets[i]);
  //  felems = jQuery(field.find('input, textarea, select')).attr('disabled', 'disabled');
  // }
}



function zero() {
	var zero = jQuery("#zero");
	var trig = jQuery("ul#big_theme li h3");
	
	trig.hover(function () {
	
		zero.fadeOut("slow");
		 
    });

}

/* .css({'border-top':'5px solid red'}) */



var download = new Array();

function addToDownloadForm(id, title)
{
    for (i in download)
    {
        if (download[i][0] == id)
            return;
    }
    download.push(new Array(id, title));

    jQuery('#download_collection_list').append(
        jQuery('<li></li>').attr({ id: 'download_item_' + id })
        .append(
            jQuery('<label></label>').attr( { title: 'Remove this item from download basket' })
            .append( jQuery('<span></span>').html(title) )
            .append( jQuery('<input />').attr( { type: 'checkbox', value: id } ).addClass('download_checkbox') )
        )
        .append( jQuery('<input />').attr( { type: 'hidden', name: 'download[]', value: id } ) )
    );
    // IE6 ignores 'checked' attribute unless object already created and shown on page
    jQuery('#download_item_' + id).find(' input[type=checkbox]').each(function(){ jQuery(this).attr('checked', 'checked').click(function(){ downloadClick(this); saveDownload(); }) });

    /*
     * Add block like this:
        <li>
            <label title="Remove this item from download basket">
                <span>HIT Double 190 Data Sheet</span>
                <input type="checkbox" class="download_checkbox" value="[id]" checked="checked"/>
            </label>
            <input type="hidden" name="download[]" value="[id]"/>
        </li>
    */

    // alternate list
    odevs();
}

function saveDownload()
{
    jQuery.cookie('download', serialize(download), { path: '/' });
}

function downloadClick(obj)
{
    var id = jQuery(obj).val();
    if (jQuery(obj).attr('checked'))
    {
        var title = jQuery(obj).parents('tr').find('label').text();
        if (title)
        {
            addToDownloadForm(id, title);
        }
    }
    else
    {
        jQuery('#download_item_' + id).remove();
        jQuery('.download_checkbox[value=' + id + ']').removeAttr('checked');
        jQuery('#download_all').removeAttr('checked');
        for (i in download)
        {
            if (download[i][0] == id)
            {
                download.splice(i, 1);
            }
        }
        // alternate list
        odevs();
    }
}

function initDownload()
{
    jQuery('.download_checkbox').click(function(){ downloadClick(this); saveDownload(); });

    var c = jQuery.cookie('download','','');
    if (c !== null)
    {
        c = unserialize(c);
        if ('object' == typeof c)
        {
            for (i in c)
            {
                if (c[i][0] && c[i][1])
                {
                    jQuery('.download_checkbox[value=' + c[i][0] + ']').attr('checked', 'checked');
                    addToDownloadForm(c[i][0], c[i][1]);
                }
            }
        }
    }

    jQuery('#download_all').click(function(){
        ch = jQuery(this).attr('checked');
        jQuery(this).parents('table').find('.download_checkbox').each(function(){ jQuery(this).attr('checked', ch); downloadClick(jQuery(this)); });
        saveDownload();
    });
}
