By Lauren Hirsch and Lisa Baertlein

NEW YORK/LOS ANGELES (Reuters) - An elaborate structure put in place to preserve Hershey Co's ties to its local community has been roiled by scandal, creating an opening that Mondelez International Inc seized on to launch a $23 billion for the chocolate giant.

While Hershey's board of directors unanimously rejected Mondelez's offer on Thursday, its once impenetrable defenses are now looking weaker due to an investigation into the charitable trust that controls it, as well as controversy facing the Pennsylvania Attorney General (AG), who also has a say in any change in Hershey Co's ownership.

The Hershey Trust, set up by the company's eponymous founder a century ago, holds 81 percent of the company’s voting stock and without its approval, a sale is impossible.

The Trust has blocked Hershey deals in the past, including a 2002 takeover bid for the company, which makes Hershey's Kisses and Reese's Peanut Butter Cups.

But the Trust set up over 100 years ago to help underprivileged children is now being investigated by the Pennsylvania AG's office for how much it spends and how long its directors have served for.

The AG's office has called for the resignation of three of its longest tenured employees. Separately, this year, the Trust fired its executive vice president, after he pled guilty to wire fraud associated with campaign contributions.

The Trust is one of Pennsylvania's wealthiest charities. Its shares in the chocolate giant have created a $12 billion endowment that helps to fund a school as well as an amusement park and resort in Hershey, a small town about 100 miles (160 km) west of Philadelphia.

Another hurdle to the Mondelez bid is the Pennsylvania AG's office, which has the right to intervene in a Hershey deal if it deems it "unnecessary for the future economic viability of the company."

The current AG in Pennsylvania, Kathleen Kane, is set to go on trial next month for allegedly leaking lewd and bigoted emails between prosecutors and judges to a reporter. The scandal has been dubbed 'Porngate' by local media.

Joel Glenn Brenner, author of the 2000 book 'The Emperors of Chocolate: Inside the Secret World of Hershey and Mars', said that the troubles facing the Trust and Kane could make them less resolute in opposing a deal.

"This time, what makes it different is these investigations, the chaos at the attorney general's office, and the fact that there has been a turnover at the trust," she said.

The fiduciary duty of the Trust, laid out in a 1909 deed, is to support the Milton Hershey School, an establishment created for children from low-income families, not Hershey’s shareholders.

In 2002, when Wm. Wrigley Jr. wanted to buy Hershey for $12.5 billion, the Trust called off the sale at the last minute. It also thwarted a deal in 2007, when confectionary company Cadbury entered into conversations about a tie-up and, in 2010, the Trust prevented Hershey from bidding for Cadbury.

The Pennsylvania AG's office also helped pull the plug on the Wrigley deal in 2002.

The Trust declined to comment on the offer for Mondelez, which would create the world's largest confectioner, but said it hoped to address the AG’s concerns.

"We expect to appropriately resolve outstanding concerns the Attorney General's office has concerning the interpretation of the 1909 deed of Milton S. Hershey, where he outlined his wishes to provide a funding mechanism to provide the Milton Hershey School with sustainable financial resources in perpetuity," said Kent Jarrell, a spokesman for the Hershey Trust board.

A spokeswoman for Kane was not immediately available to comment.

(Corrects gender in paragraph 11.)

(Editing by Carmel Crimmins and Alistair Bell)